Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use AssumeRole from another AWS account in a CloudFormation template?

I am trying to work out the logic flow for an AWS CloudFormation template that will assume an IAM role that can pull files from a S3 bucket in another AWS account.

What I have so far is:

  1. accountA has a roleA
  2. roleA has policy that allows sts:AssumeRole for a role in accountB :arn:aws:iam::11122233444:role/AllowPullS3
  3. accountB has role(AllowPullS3) with
  4. policy allow:s3 listBucket + get,put,delete
  5. trust relationship for accountA :Action:"sts:AssumeRole"

If I create an EC2 instance manually with the IAM:roleA and then use the CLI to get the assume-role credentials, I can then pull the files from the other account's S3 bucket as expected.

But what do I need to put where in my accountA CF template that will allow the EC2 instance to assume roleB and pull the file from the accountB S3 bucket as part of the formation?

I have tried following a lot of tutorials such as this cfn-iam:init tutorial but still can not fully grasp what goes where.

Thanks for your advice.

Art

like image 623
art vanderlay Avatar asked May 14 '14 19:05

art vanderlay


People also ask

How do I transfer resources from one AWS account to another?

You can set up a trust relationship with an IAM role in another AWS account to access their resources. For example, from the source account you want to access the destination account. You can assume the IAM role from the source to destination account by providing your IAM user permission for the AssumeRole API.

How do I access resources in another AWS account using AWS IAM?

Step 1: Create a role in the Production Account. You can allow users from one AWS account to access resources in another AWS account. To do this, create a role that defines who can access it and what permissions it grants to users that switch to it.

How do you assume an AWS role from another AWS role?

You can assume a role by calling an AWS CLI or API operation or by using a custom URL. The method that you use determines who can assume the role and how long the role session can last. When using AssumeRole* API operations, the IAM role that you assume is the resource.


1 Answers

It is not possible to tell CloudFormation to assume another role.

However, if you have a CLI script/command that works on the Amazon EC2 instance, then just pass that script as User Data. The script will run when your instance starts. User Data can be passed in your CloudFormation template, where the EC2 instance is defined.

See: Running Commands on Your Linux Instance at Launch

like image 97
John Rotenstein Avatar answered Oct 17 '22 03:10

John Rotenstein