Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EC2 instance with a cross account IAM role

I've created a cross account IAM role in one of my accounts(say account A) and would like to attach that role to an ec2 instance in another account(account B).

I tried creating a new role in account B with sts:AssumeRole pointing to the role in A and attached it to an ec2 instance in B. Doesn't seem to be working.

How can the ec2 instance assume the cross account role in A?

like image 287
ebnius Avatar asked May 03 '17 12:05

ebnius


People also ask

Can an EC2 instance assume a role in another account?

You cannot attach a cross-account IAM role to an EC2 instance directly. And having the sts:AssumeRole permissions does not automatically make the one role assume into the other. Instead: Create your cross-account role in Account A.

Are IAM roles Cross-account?

A Cross-account IAM Role is used to define access to resources in a single account, but it isn't restricted to users in a single account. For example: The EC2 servers in your staging environment can safely get access to an S3 bucket in production by using a properly defined role to do so.

How do you assume IAM a cross-account?

In Prod account, set up the Prod-Xacc-Access role which will be a cross-account role. Navigate to IAM > Roles and click on Create New Role. Select Another AWS account, and provide Account ID, and click on Next:Permissions. Enter the AWS account ID of the AWS account which can assume this role.

Can I attach IAM role to EC2 instance?

To attach an IAM role to an instanceOpen the Amazon EC2 console at https://console.aws.amazon.com/ec2/ . In the navigation pane, choose Instances. Select the instance, choose Actions, Security, Modify IAM role. Select the IAM role to attach to your instance, and choose Save.


1 Answers

You cannot attach a cross-account IAM role to an EC2 instance directly. And having the sts:AssumeRole permissions does not automatically make the one role assume into the other.

Instead:

  1. Create your cross-account role in Account A.
  2. Create an IAM role for EC2 instances in Account B. Give this role permissions to execute sts:AssumeRole.
  3. Assign the IAM role from #2 to your EC2 instance.

Then, when you want to access the AWS API from your EC2 instance:

  1. Execute sts:AssumeRole to assume the cross-account role for Account A, to obtain temporary credentials.
  2. Use those temporary credentials to execute the rest of your API methods.
like image 162
Matt Houser Avatar answered Oct 14 '22 07:10

Matt Houser