代码之家  ›  专栏  ›  技术社区  ›  Justin

AWS云信息:ses角色的环境条件

  •  0
  • Justin  · 技术社区  · 7 年前

    我正在尝试创建一个可重用的CloudFormation模板,并希望做一些条件设置,如果环境参数为“test”(或“prod”以外的任何其他环境),则只向gmail帐户(即公司帐户)发送SES电子邮件,但对于“prod”,则将SES电子邮件发送到任何地方。我是否必须扮演两个不同的角色,并且每个角色都有条件?或者,是否有一种方法可以在下面的一个角色中实现这一点?谢谢你的帮助!

    Parameters: 
    
      Environment:
        Description: Environment, which can be "test", "stage", "prod", etc.
        Type: String
    
     Resources:
    
       Role: 
        Type: AWS::IAM::Role
        Properties: 
        RoleName: myRole
        Path: /
        AssumeRolePolicyDocument: 
           Version: "2012-10-17"
           Statement:
            - 
              Effect: "Allow"
              Principal: 
                Service: 
                  - "ecs.amazonaws.com"
              Action: 
                - "sts:AssumeRole" 
        Policies: 
          - 
            PolicyName: "ses-policy"
            PolicyDocument:
              Version: "2012-10-17"
              Statement: 
                -
                  Effect: "Allow"
                  Action: 
                    - "ses:SendEmail"
                    - "ses:SendRawEmail"
                  Resource: "*"
                  Condition:
                    "ForAllValues:StringLike": 
                      "ses:Recipients": 
                        - "*@gmail.com"
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   wjordan    7 年前

    Conditions 非常适合将此类条件逻辑添加到CloudFormation资源属性。在您的示例中,可以使用 Fn::If 包含现有 Policy Condition (不要与云形成条件混淆!)如果环境不是 prod AWS::NoValue 否则(当环境为 ):

    Parameters:
      Environment:
        Description: Environment, which can be "test", "stage", "prod", etc.
        Type: String
        AllowedValues: [test, stage, prod]
    Conditions:
      IsProdEnvironment: !Equals [ !Ref Environment, prod ]
    Resources:
      Role:
        Type: AWS::IAM::Role
        Properties:
          RoleName: myRole
          Path: /
          AssumeRolePolicyDocument:
             Version: "2012-10-17"
             Statement:
              -
                Effect: "Allow"
                Principal:
                  Service:
                    - "ecs.amazonaws.com"
                Action:
                  - "sts:AssumeRole"
          Policies:
            -
              PolicyName: "ses-policy"
              PolicyDocument:
                Version: "2012-10-17"
                Statement:
                  -
                    Effect: "Allow"
                    Action:
                      - "ses:SendEmail"
                      - "ses:SendRawEmail"
                    Resource: "*"
                    Condition: !If
                    - IsProdEnvironment
                    - !Ref AWS::NoValue
                    - "ForAllValues:StringLike":
                        "ses:Recipients":
                          - "*@gmail.com"