amazon-web-services - Cloudformation 更新堆栈名称或环境的堆栈策略条件

标签 amazon-web-services aws-cloudformation amazon-iam serverless policy

我的 CloudFormation 堆栈附加了一个策略:

{
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": "*",
      "Action": [
        "Update:*"
      ],
      "Resource": "*"
    },
    {
      "Effect": "Deny",
      "Principal": "*",
      "Resource": "*",
      "Action": [
        "Update:Replace",
        "Update:Delete"
      ],
      "Condition": {
        "StringEquals": {
          "ResourceType": [
            "AWS::SNS::Topic",
            "AWS::SQS::Queue"
          ]
        }
      }
    }
  ]
}

该策略可防止意外删除 SNS/SQS 资源。我希望在dev环境中使政策更加自由。如何有条件地禁用 Deny 语句,例如,如果我的 CF (cloudformation) 堆栈名称是 my-app-dev 或 CF 堆栈具有标签 STAGE 等于 dev

顺便说一句,策略是由无服务器框架生成的,因此我必须将其写入 serverless.yml

最佳答案

这可以通过使用无服务器框架的环境变量来完成。

无服务器.yml

service: sample

provider:
  name: aws
  stage: ${opt:stage,"dev"}
  region: ap-northeast-1

custom:
  policyChange: 
    prd: Deny
    dev: Allow

resources:
  - ${file(iam.yml)}

iam.yml

Resources:
  SampleRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: SampleRole
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
        - Effect: Allow
          Principal:
            Service:
            - lambda.amazonaws.com
          Action:
          - sts:AssumeRole
      Path: "/"
      Policies:
        - PolicyName: SamplePolicy
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
            - Effect: '${self:custom.policyChange.${self:provider.stage}}'
              Resource: "*"
              Action:
              - sqs:*

关于amazon-web-services - Cloudformation 更新堆栈名称或环境的堆栈策略条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67124744/

相关文章:

amazon-web-services - 在 Cloudwatch 事件规则上调用 AWS lambda 的并发实例

node.js - 如何在 NodeJS 上使用 S3 SSE C(使用客户端提供的 key 的服务器端加密)

unix - Amazon IAM 可以用作主机的身份验证方法吗?

amazon-web-services - 保护 AWS API 网关

amazon-web-services - Terraform 无效配置请求 : Access denied for S3 bucket

aws-cloudformation - 无法使用 CloudFormation 删除 Amazon ECS 集群

amazon-web-services - 在 AWS 云形成中承担的角色

aws-lambda - 如何使用 boto3 部署 SAM 模板

amazon-web-services - 亚马逊 AWS : DataPipelineDefaultRole/EDPSession not authorized to perform iam:ListRolePolicies

amazon-web-services - 将 aws WebSocket api 连接到自定义域名