How to generate the AWS root account ARN in CloudFormation?

*爱你&永不变心* 提交于 2019-12-08 07:28:52

问题


The situation

I am generating a KMS Key in CloudFormation. According to the KMS policy documentation, it is crucial to create a policy where the Principal is the account itself, in order for IAM policies to be able to grant access to the key.

The question

How can I create the ARN for the account root in CloudFormation?


回答1:


The answer

{  
   "Fn::Join":[  
      ":",
      [  
         "arn:aws:iam:",
         {  
            "Ref":"AWS::AccountId"
         },
         "root"
      ]
   ]
}

Why does this work?

First, let's examine the line, "Ref":"AWS::AccountId". This is a pseudo parameter reference, which is a fancy way of saying that it is a parameter that comes out of the box with CloudFormation. There are many such parameters. This one happens to give us the account ID, which is crucial for constructing the ARN.

Now, the rest is just the creation of an ARN using this account ID. Fn::Join is simply a CloudFormation built-in that allows concatenation of strings. This is crucial when combining references with string constants (or other references) as we are doing here.

The result is something like...

arn:aws:iam::123456789012:root



回答2:


For those who use YAML for their CloudFormation templates:

!Sub arn:aws:iam::${AWS::AccountId}:root


来源:https://stackoverflow.com/questions/50671361/how-to-generate-the-aws-root-account-arn-in-cloudformation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!