2017-01-19 23 views
0

私は再利用可能なCloudFormationテンプレートを作成しようとしています。環境変数が "test"(または "prod"以外の環境)の場合、 SESメールをgmailアカウント(企業アカウントなど)のみに送信しますが、「prod」の場合はSESメールをどこにでも送信します。私は2つの異なる役割を果たし、それぞれに条件を持たなければならないでしょうか?または、これを下の役割の中で行う方法はありますか?助けてくれてありがとう!AWS CloudFormation環境条件付き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" 

答えて

2

Conditions CloudFormationリソースプロパティに条件ロジックのこの種を追加するための最適です。あなたの例では、環境がprodでない場合、Policy Condition(CloudFormation条件と混同しないでください!)とAWS::NoValue(環境がprodの場合は完全にポリシー条件を削除します)を組み込むためにFn::If組み込み関数を使用することができます。

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"