amazon-web-services - CloudFormation YAML - 带有条件语句的 IAM 策略

标签 amazon-web-services yaml aws-cloudformation amazon-iam

我有一个 IAM 策略,该策略是在 AWS 控制台中创建另一个资源时自动创建的。我正在尝试将其添加到现有的 CloudFormation 堆栈中。

该策略具有以下语句,其中包含“条件”属性:

{
    "Sid": "DecryptSecretValue",
    "Action": [
        "kms:Decrypt"
    ],
    "Effect": "Allow",
    "Resource": [
        "arn:aws:kms:MyRegion:MyAccountId:key/4f402c6e-9624-40a4-8d4d-c0f2efe88602"
    ],
    "Condition": {
        "StringEquals": {
            "kms:ViaService": "secretsmanager.eu-west-1.amazonaws.com"
        }
    }
}

应如何在 CloudFormation YAML 模板中构建此结构?到目前为止我已经:

Statement:
  - Effect: Allow
    Action: kms:DecryptSecretValue
    Resource: arn:aws:kms:MyRegion:MyAccountId:key/4f402c6e-9624-40a4-8d4d-c0f2efe88602

但我不知道如何包含“条件”属性。有什么想法吗?

最佳答案

对于 json 策略的每个级别,您都在 yaml 上添加缩进。
因此,ConditionEffectResourceAction 处于同一级别。
StringEquals 是从 Conditions 缩进的。
kms:ViaService 是从 StringEquals 缩进的。

由于 kms:ViaService 名称中包含冒号 (:),因此您需要将其添加在引号之间。

Statement:
  - Effect: Allow
    Action: "kms:DecryptSecretValue"
    Resource: "arn:aws:kms:MyRegion:MyAccountId:key/4f402c6e-9624-40a4-8d4d-c0f2efe88602"
    Condition:
      StringEquals:
        "kms:ViaService": "secretsmanager.eu-west-1.amazonaws.com"

关于amazon-web-services - CloudFormation YAML - 带有条件语句的 IAM 策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67471033/

相关文章:

amazon-web-services - 如何配置无服务器 S3 存储桶资源以使用设置为其函数的 http 端点的 CORS AllowOrigin

amazon-web-services - 使用 aws cdk 创建 sam canary 部署

amazon-web-services - 连接到Elasticsearch-Amazon Elasticsearch Service-IAM用户

php - 除 EC2 实例 AWS 上的 Laravel 5.1 之外的所有其他路由均出现 "Not Found"错误

go - 在 Go 中加载动态 yaml 结构

python - 如何为 PyYAML 编写代表?

kubernetes - 我的 initContainers 的 "alpine"图像有问题

amazon-web-services - 如何获取 Fargate ecs 容器的 TaskId

git - 无法将主机添加到已知主机列表 - 意外删除了我的 .ssh 文件夹中的所有文件

amazon-web-services - AWS secret 轮换计划 lambda VpcSecurityGroupIds 的目的