-
Notifications
You must be signed in to change notification settings - Fork 9.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AWS assume role not working #11270
Comments
I am facing the same issue. Below is my configuration for the aws provider. provider "aws" {
} Like @hkalyana I can assume the same role and create resources from the AWS GUI. The aws_*_keys are correct. |
I found a way round it. The terraform documentation on this issue is unclear. follow: http://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp_request.html remove the assume_role block; it's useless, for now. the aws provider required attributes should be populated with the corresponding outputs you get when temporary credentials are requested from sts.amazonaws.com. there are various ways you can make the call to sts. Anisble has a cool module for this specific purpose hope it helps you resolve your issue. |
Having exactly the same problem here. Role delegation is working fine in both AWS Web Interface and aws CLI but not with terraform. @aaomoware could you provide more details on your workaround ? If I get your post right, looks like you have to use an external tool to get temporary credentials and then feed them to terraform. |
I use vault to store my credentials....hence im writing to vault. Terraform supports reading/writing from and to vault. You can use the aws api, wrapped in a script, to accomplish the above steps; i use ansible because it's easier, works well, and it's already in use in my project to configure instances brought up by terraform You can add the ansible run to a cron job, every 1hr. The max duration for the temporary keys is 3600(1hr) Notes: |
Has anyone actually gotten assume_role to work? I'm trying to use it and have been seeing the same error as above. Would love a working example of the feature |
I have a working solution with Terraform and AWS with assume role. Note that I'm not using Terraform to get a session token or to assume the role. First you want to have a session, especialy if you have MFA activated like me.
Note that you can skip --serial-number and --token-code if you are not using a MFA. This command will return to you some tokens: You have to save them and source them in order to use them. The easy way is to store them in a file like this and the "source" the file in question in order to have those variables set in your environment (this is for Linux):
Then you can call the assume role:
This command will return to you the same tokens but with new values: Like the previous ones, you have to save them and source them in order to use them. The easy way is to store them in a file and the "source" the file in question in order to have those variables set in your environment. Note that you should save those credentials in a separate file than the get-session-token credentials. Your Terraform provider configuration should look this this:
You have to populate the variables using the assume-role environment variables AWS_ACCESS_KEY_ID for access_key, AWS_SECRET_ACCESS_KEY for secret_key and AWS_SESSION_TOKEN for token. Now you should be able to lauch Terraform without errors. The reason you have to save those assume-role and get-session-token credentials separately is that if your get-session-token is still valid, you can directly source the get-session-token variables and call assume-role with them. Hope I was clear enough ;) |
@jfriis In case you were still looking for confirmation, I was able to successfully use the assume role functionality with new 0.9 release. I have a single entry in the aws credentials file which contains a personal IAM key from a Jump style account (no other AWS resources are created or allowed here except IAM resources).
In my terraform project, I created a backend.tf file which contained the new remote backend config and the AWS provider config. The S3 bucket is created in the destination AWS account and all resources specified in the project were properly created.
This setup allowed me to |
@david-kli I have been able to successfully do this with 0.9 (great), but cannot do this if the user's access keys belong to Account A and the role to be assumed is in Account B I receive the following (these are 'bona fide' accounts and roles - which allow the role to be assumed successfully via the AWS Console - switch roles functionality)
My use case is that I have a hub and spoke scenario where I want to use the hub account to build out infrastructure on spoke accounts using a user from the hub account who has permissions to assume a role in each spoke account. Would appreciate if you or anyone can confirm if you can / cannot do this across account. |
@agrmckinnon I use a similar hub and spoke setup which requires a minimal amount of terraforming using the root account during initial account setup. After that, the build of the build out is accomplished with assumed roles. The aws jump account in my setup only contains IAM users and group definitions. I create one group here per unique remote account & role combination. The group policies contain the sts:assumeRole for the destination account/role arn. Each remote account has the roles defined with a trust relationship defined back to the jump account. Your error message makes me wonder if something is getting lost with the provider usage inside a module. You mentioned you can assume the role with the AWS console. Can you also perform similar actions with the aws cli? It might also be worthwhile to work up a test which does not use a provider inside in a module. |
@david-kli In my case we also had the role available to the user for console access and I had added the condition to the trust policy
This enforced that the user had to have MFA'd via the console before assuming the role. This had the detrimental effect of receiving the error, as defined in the previous post from me, when assuming the role via terraform. After removing the condition from the trust policy I can successfully assume the role on the account on the spoke account via Terraform. Our goal is to have our production activities undertaken by an automation tool using instance profiles and assuming roles. For development there will be an element of running terraform with assume roles from an engineers own credentials. In the short/medium term I plan to separate the engineers user into two users to avoid future confusion
Thanks Again |
I'm having problem getting assume role to work on v0.9.4: ~/.aws/configDoesn't exist - but I'm hoping to put only the region here and get rid of it from the *.tf files ~/.aws/credentials:
provider.tf
remote.tf
The following shell commands work just fine:
So the However,
For testing, I have disabled the MultiFactorAuthPresent requirement in |
i'm getting what @FransUrbo is getting in tf 9.4....is MFA supposed to work (will TF prompt my for my MFA token?) |
@drdamour No, at current it will not :( |
For me, it doesn't work even if i disable MFA everywhere... |
is the problem people are running into possibly that you can't assume the role if you are already in the role? that's what we run into from time to time. I can assume roles from my main session...and thus assume_role works fine...but if i use aws cli to assume the role then running terraform fails on teh assume_role call...as my new role does not have permissions to assume itself. seems like terraform should check if the session role is already the role defined in assume_role before it tries to assume it...however i don't know if the aws api allows you to get your current role..and i don't know go so can't really test...anyone got some skills? looks like you CAN get the current role http://docs.aws.amazon.com/cli/latest/reference/iam/get-role.html |
@drdamour If you look at my code at the top, you'll see that's not what's happening for me. At least, I don't think so. I have my core account, which then assumes a role in the other account. |
In case anyone comes across this, I've adapted a work around from Save the following to a file called Then make it executable with Then put it somewhere in your Then call #!/usr/bin/env bash
# From https://github.com/kubernetes/AWS/issues/226#issuecomment-278879348
# and https://github.com/kubernetes/AWS/blob/master/docs/mfa.md
# set -euo pipefail
main() {
local role_arn="${AWS_MFA_ROLE_ARN:-}"
local serial_number="${AWS_MFA_ARN:-}"
local token_code
if [ -z "${role_arn}" ]; then
echo "Set the AWS_MFA_ROLE_ARN environment variable" 1>&2
return 1
fi
if [ -z "${serial_number}" ]; then
echo "Set the AWS_MFA_ARN environment variable" 1>&2
return 1
fi
echo -n "Enter MFA Code: "
read -s token_code
# NOTE: The keys should not be exported as AWS_ACCESS_KEY_ID
# or AWS_SECRET_ACCESS_KEY_ID. This will not work. They
# should be exported as other names which can be used below. This prevents
# them from incorrectly being picked up from libraries or commands.
temporary_credentials="$(aws \
sts assume-role \
--role-arn="${role_arn}" \
--serial-number="${serial_number}" \
--token-code="${token_code}" \
--role-session-name="terraform-access"
)"
unset AWS_PROFILE
export "AWS_ACCESS_KEY_ID=$(echo "${temporary_credentials}" | jq -re '.Credentials.AccessKeyId')"
export "AWS_SECRET_ACCESS_KEY=$(echo "${temporary_credentials}" | jq -re '.Credentials.SecretAccessKey')"
export "AWS_SESSION_TOKEN=$(echo "${temporary_credentials}" | jq -re '.Credentials.SessionToken')"
exec terraform "$@"
}
main "$@" |
Hey all – this issue has been migrated to the new AWS provider repo, here: hashicorp/terraform-provider-aws#472 I've posted a follow up comment there with an example config, if anyone can take a look, thanks! |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further. |
Hi there,
Thank you for opening an issue. Please note that we try to keep the Terraform issue tracker reserved for bug reports and feature requests. For general usage questions, please see: https://www.terraform.io/community.html.
Terraform Version -0.8.0
Run
terraform -v
to show the version. If you are not running the latest version of Terraform, please upgrade because your issue may have already been fixed.Affected Resource(s)
Please list the resources as a list, for example:
Copy-paste your Terraform configurations here - for large Terraform configs,
Expected Behavior
Expected the resources to be created on the trusting account
Actual Behavior
Received error The role "arn:aws:iam:::role/trustedrole" cannot be assumed.
Steps to Reproduce
Please list the steps required to reproduce the issue, for example:
3)Attached the policy to the user
4)Used the same role ARN of the trusting account in the terraform assume role
Important Factoids
The procedure I followed is working for me when I try to switch the roles in AWS GUI and I am able to create resources on the trusting account .
References
The text was updated successfully, but these errors were encountered: