I am trying to define a trust relationship policy document between a role and a user in cloudformation (yaml).
For specifying the ARN of the user in the role's AssumeRolePolicyDocument
, I want to reference the ARN from the actual cloudformation resource, instead of having to construct the ARN string.
But, it doesn't work. When I use !Ref rUser
, I get an error when creating the cloudformation stack "invalid principal in policy".
When I just paste the ARN string as the value, it works. Is it because !Ref rUser
returns a user object type and does not evaluate to a string? If so, how can I reference the ARN from the resource?
Code:
rUser:
Type: "AWS::IAM::User"
Properties:
UserName: "my_user"
rRole:
DependsOn: rRole
Type: "AWS::IAM::Role"
Properties:
RoleName: "my_role"
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Principal:
AWS:
# this does not work, gives error "Invalid Principal in policy"
- !Ref rUser
# this does work (just hard coding the ARN string):
# - "arn:aws:iam::111111111111:user/my_user"
Action:
- "sts:AssumeRole"
Note: To reference a resource in another AWS CloudFormation stack, you must create cross-stack references. To create a cross-stack reference, use the export field to flag the value of a resource output for export.
In order to get the Arn of a resource we have to use the resource-specific resourceNameArn property, for example: bucketArn for an S3 bucket, instantiated via the Bucket construct. tableArn for a Dynamodb table instantiated via the Table construct. functionArn for a Lambda function instantiated via the Function ...
You can use the Ref function to refer to an identifying property of a resource. Frequently, this is the physical name of the resource; however, sometimes it can be an identifier, such as the IP address for an AWS::EC2::EIP resource or an Amazon Resource Name (ARN) for an Amazon SNS topic.
Just figured it out ... quite simple using the GetAtt function:
rRole:
DependsOn: rRole
Type: "AWS::IAM::Role"
Properties:
RoleName: "my_role"
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Principal:
AWS:
- !GetAtt rExternalUser.Arn # use the GetAtt function
Action:
- "sts:AssumeRole"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With