-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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 #472
Comments
Hello – I'm attempting to reproduce this and confirm if it's a Terraform issue or policy issue. Right now I have a configuration that demonstrates Terraform is successfully able to assume a role and create a resource, so I was hoping someone could examine my configuration and see if I've misconfigured it and not correctly reproducing your situation. Below is my config. I have IAM admin credentials exported in my environment, but they are not AWS Root credentials. Role # Grab the ARN of the current logged in user
data "aws_caller_identity" "current" {}
# create a role which allows the current user to assume it
resource "aws_iam_role" "terraform_11270" {
name = "terraform_11270"
path = "/test/"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
},
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"AWS": "${data.aws_caller_identity.current.arn}"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}
resource "aws_iam_role_policy" "terraform_11270" {
name = "terraform_11270"
role = "${aws_iam_role.terraform_11270.id}"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:*",
"ec2:*"
],
"Resource": "*"
}
]
}
EOF
}
# configure this provider alias to only use the IAM Role created above
provider "aws" {
alias = "iamrole"
assume_role {
role_arn = "${aws_iam_role.terraform_11270.arn}"
}
}
resource "aws_security_group" "primary" {
name = "primary"
}
# Create a security group with the above IAM Role assumed
resource "aws_security_group" "secondary" {
provider = "aws.iamrole"
name = "secondary"
}
With this configuration I was able to create the security group |
I'm going to go ahead and close this issue for now. With the config above I've demonstrated that If you feel I've demonstrated the working behavior incorrectly, please let me know and we'll reopen for another look! |
It works for non-MFA roles, yes. For roles enforcing MFA-protected access something like hashicorp/terraform#11734 (which hasn't been migrated?) is needed. |
This doesn't work at all for me. Files~/.aws/credentials
The remote.tf
provider.tf
The second provider will then be the "default" one and the first being referenced in certain places as This gives me:
I had wanted this to work:~/.aws/credentials
but that don't work either... Not even if I disable MFA and remove the |
Some small change. If I duplicate the
Neither of these reasons seems valid, because the ARN to the role was copy-and-pasted from the IAM console, I can successfully go from my root account (as in, not root credentials, but the account I've labeled "root") to this test account. And I disabled MFA requirements on the role.. |
@catsby I think the problem with your example is
But where is that SG created? In another account? Or the same one? Could the problem I'm having is that I have problem with some cross-account-assume-role thingiemajig?? |
Hello @catsby, |
Same issue here. Our IAM users are managed through a central account, requiring MFA. Users have barely any permissions in the central account, but can assume roles in other accounts managed through AWS Organisations. I think this is a pretty common setup, especially at larger companies, and is the recommended best practice by AWS themselves. Even if I avoid using the aws credentials and config files by embedding my credentials straight into the tf file like this:
I get the following error:
My guess is that this is because the MFA code isn't being requested and sent before trying to assume the role (the trust policy on the role requires that the user is MFA authenticated). This is a real blocker for us. Let me know if there's anything else I can provide to help. |
@DrSolaris Could you, temporarily, disable MFA on the satellite account role and see if that makes any difference? The reason for this, is that I suspect that there's TWO problems here - TF can't assume remote roles AND it have problems with MFA. But I'm not sure... |
I'm running into this case now as well, I have multiple satellite accounts, ones with MFA fail to work with TF whereas ones without MFA security are able to be provisioned with TF. This is currently requiring the use of kludgy wrapper scripts to make work. |
The terraform files in my opinion should not be specifying anything to do with authentication. Role names are different and if it's an admin with an admin role or a developer with a developer role running a terraform file they should not have to edit it to specify the profile/role/etc to use. This should be input to terraform. Currently, the only way I can, as an admin with MFA on my role/profile, give credentials properly to a terraform file is to have an external script issue sts assume role calls and then set environment variables to the temporary access key and secret. This is not an efficient way to pass in credentials. The GO SDK fully supports MFA + assume role style profiles (with role_arn and source_profile) so terraform should delegate authentication calls to the GO SDK. I hoped there would be a In my opinion. |
@catsby Could you please reopen this again? It's clearly not fixed (satisfactory)... |
This is clearly not fixed as other mentioned. Does not work at all with MFA! |
@FransUrbo, one quick correction of the ~/.aws/credentials file you posted under "I had wanted this to work". Those [test] profile parameters should be in the ~/.aws/config file instead, and the profile name should be prefixed with "profile" (this prefix is only needed in the ~/.aws/config file), e.g. : ~/.aws/config:
This is how my config/credentials files are configured, and role switching is working correctly in the AWS CLI and Terraform, but then again I'm not using MFA. |
@JoshCooley What's the difference? The |
So even profiles without MFA that are using source profile don't work with AWS_PROFILE=<profile name> The Go SDK fully supports source profile/role arn profiles and MFA. Hashicorp, what needs to be done to support this in terraform? |
@et304383 @FransUrbo #1608 might be of interest. If my understanding of the latest comments is correct, this will require a release of the provider for aws resources but also a release of terraform for remote state kept in aws s3. |
@choppedpork so how do we get this to happen? |
Happy New Year! Anyone.. anyone.. Bueller? |
I spent some time trying to get Terraform to assume roles in a cross-account setup, trying various workarounds. The real solution in the end was to use Terragrunt, which worked first time using this feature - Terragrunt does the assume-role, bypassing the Terraform issues in this area - just give Terragrunt a role ARN. |
Hello – This issue has gone several ways since it opened, I think. I was able to create an EC2 instance with a profile (named
Terraform correctly used the IAM profile creds to assume the role. The key bits:
{
"Version": "2008-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {"AWS": "*"},
"Effect": "Allow",
"Sid": ""
}
]
}
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::XXXXXX:role/cts_base_instance_role"
},
"Action": "sts:AssumeRole"
}
]
} This does not demonstrate cross account role. I does demonstrate Terraform picking up EC2 role creds and then using those creds to assume the role. I realize this isn't explicitly what others have reported, but I believe it was one instance and it demonstrates If you have another auth issue that is not covered by what I describe above, please open a new issue and detail what we're missing or where a bug happens and we'll take a fresh 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 feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks! |
This issue was originally opened by @hkalyana as hashicorp/terraform#11270. It was migrated here as part of the provider split. The original body of the issue is below.
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: