aws-cli icon indicating copy to clipboard operation
aws-cli copied to clipboard

Feature request: Assume role with EC2 instance profile as the source profile

Open ghost opened this issue 11 years ago • 80 comments

Right now you can execute commands using credentials from one of these sources: root credentials, IAM credentials, temporary credentials from an EC2 instance profile, and temporary credentials from assuming a role via IAM credentials. I would like to execute commands by using temporary credentials from assuming a role via the EC2 instance profile.

I need this ability because I'm using two AWS accounts and I'm using an EC2 instance to run AWS CLI commands against both accounts. The EC2 instance profile allows me to perform tasks for one account, but I need to assume a cross-account role to perform tasks for the other account. Unfortunately there is no way to get AWS CLI to assume the cross-account role even though the EC2 instance profile has permissions to assume that role.

I tried removing the source_profile property from my role-based profile in hopes that the source_profile would use the instance profile, but that failed. After looking at AssumeRoleProvider in awscli/customizations/assumerole.py, I see that AWS CLI can only assume a role if the source profile has actual credentials in the config file. So currently that excludes any use of an instance profile to assume a different role.

ghost avatar Jun 15 '15 22:06 ghost

Marking as feature request.

kyleknap avatar Jun 16 '15 17:06 kyleknap

I've implemented a change to assumerole.py that accomplishes the goal in the feature request. In my implementation, AssumeRoleProvider falls back to directly calling InstanceMetadataProvider if the source profile is missing. Although this meets my use-case, I'm not sure if this is the approach that others would be pleased with. For one thing, there is no fallback to environment variables or another role. And the InstanceMetadataProvider created is actually a separate instance from the one created in botocore's create_credential_resolver() function.

I was hoping to implement it in such a way that that roles could be assumed to an arbitrary depth (a role is assumed from a role which is assumed from a role... etc.) and the ending source profile could be any valid credential provider (Environment variables, IAM credentials, instance metadata, etc.). The current credential resolver structure in botocore doesn't make it easy to do this. The create_credential_resolver() function is written with the assumption that you can create the credential resolver with the one profile you've selected and you won't need another.

Should I submit my pull request for the simplified version that just directly calls the InstanceMetadataProvider, or is a more robust implementation required? The better version probably would require some changes to botocore.

ghost avatar Jun 18 '15 17:06 ghost

@pgcp Would you mind submitting your solution? It should get the ball rolling for this issue.

ColinHebert avatar Nov 26 '15 20:11 ColinHebert

Just ran into this myself! Albeit without visible text around 'aws_access_key_id' as seen below in the pip installed AWS CLI 1.9.9 on an Ubuntu 14.04 machine:

$ aws --profile otheraccount ec2 describe-instances

'aws_access_key_id'
$ cat ~/.aws/config
[default]
region=us-east-1

[profile otheraccount]
source_profile = default
role_arn = arn:aws:iam::############:role/my_other_accounts_role

bflad avatar Dec 02 '15 01:12 bflad

@kyleknap, is it possible to consider adding this issue to the roadmap?

ColinHebert avatar Jan 06 '16 06:01 ColinHebert

@ColinHebert This is a feature we would like to add as we have gotten multiple requests for it and it would be useful. However, we do not have a timetable for when it will get done. When we start making progress on this feature, the issue will be updated.

kyleknap avatar Jan 06 '16 17:01 kyleknap

This is just a "me too" comment. Ran into exactly this problem today. Please implement this functionality. The model (at least) seems to suggest it should work this way.

craigday avatar Feb 23 '16 01:02 craigday

+1. Just hit this problem myself. @pgcp please do submit your changes as a pull request. Even if it ultimately gets rejected, it'll help get the correct change made. I looked at assumerole.py, and it wasn't obvious to me what that change would look like.

keith-mason-conning avatar Mar 01 '16 15:03 keith-mason-conning

Echoing s2maki, @pgcp it would be great to see a pull request. Thanks in advance.

josefsachsconning avatar Mar 10 '16 13:03 josefsachsconning

I've ran into this myself. Just my +1 here.

reppard avatar Mar 16 '16 18:03 reppard

+1

inhumantsar avatar Mar 18 '16 23:03 inhumantsar

While you wait for this to be implemented, feel free to use my somewhat odd workaround. It works great for me and unblocks me from waiting on this feature

https://gist.github.com/gene1wood/34b02fa3091e184e1997

# Usage : . $0 ROLE_ARN [PARENT_PROFILE_NAME]
#
# This tool will generate temporary credentials for an assumed role, save
# those ephemeral credentials in the awscli config and set the alias of 
# "aaws" to use this new ephemeral awscli profile
#
# Examples
#   . $0 arn:aws:iam::123456789012:role/ExampleRole
#   aaws ec2 describe-instances
# or
#   . $0 arn:aws:iam::234567890123:role/ExampleRole staging
#   aaws --region us-west-2 ec2 describe-instances

gene1wood avatar Mar 19 '16 06:03 gene1wood

The code for AssumeRoleProvider was moved to botocore since this issue was created. Because of that I've submitted a pull request to that repo instead of this one.

The code in the pull request would change the behavior of AWS CLI so that if a profile had a role_arn but not a source_profile then credentials would be retrieved from the EC2 instance profile (i.e. credentials via instance metadata service). Previously an error was returned if the source_profile was missing.

I'd much prefer having a configuration profile that explicitly declares that its credentials are from the EC2 instance profile and then reference that profile as the source_profile, but perhaps that can be a separate feature request. In the meantime, I'm just trying to fill a gap in functionality that several of us need.

ghost avatar Mar 23 '16 23:03 ghost

We created a gem as a work around for this issue: https://github.com/manheim/awssume

reppard avatar May 06 '16 19:05 reppard

+1

rodlogic avatar Jun 13 '16 22:06 rodlogic

+1

brianantonelli avatar Jul 15 '16 15:07 brianantonelli

Finding myself wanting this, after wanting it in botocore and boto and boto3 too. In general, AssumeRole and friends are hugely powerful tools, but all the APIs and tooling around them feels pretty limited.

copumpkin avatar Aug 04 '16 18:08 copumpkin

Would request that too. Didn't realise it's not supported. When EC2 instance profile was encouraged, such a feature is nearly a must.

homer-editionlingerie avatar Sep 15 '16 08:09 homer-editionlingerie

+1

kachenjr avatar Sep 22 '16 13:09 kachenjr

+1

peter-playbasis avatar Sep 28 '16 04:09 peter-playbasis