terraform-provider-aws: AssumeRoleTokenProviderNotSetError when using assume_role with mfa enabled
Community Note
- Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
- Please do not leave “+1” or “me too” comments, they generate extra noise for issue followers and do not help prioritize the request
- If you are interested in working on this issue or have submitted a pull request, please leave a comment
Terraform Version
Terraform version: 0.12.10 Go runtime version: go1.13.1 provider.aws ~> 2.32.0
Affected Resource(s)
- provider “aws”
Terraform Configuration Files
env
AWS_SDK_LOAD_CONFIG=1
AWS_PROFILE=bar-admin
~/.aws/config
[default]
region = eu-west-1
output = json
[profile bar-default]
aws_access_key_id=<key>
aws_secret_access_key=<secret>
[profile bar-admin]
role_arn=arn:aws:iam::<account_id>:role/admin
source_profile=bar-default
mfa_serial=arn:aws:iam::<account_id>:mfa/ch
resource "aws_iam_group" "admins" {
name = "admins"
}
resource "aws_iam_role" "admin_mfa_role" {
name = "admin"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"
},
"Action": "sts:AssumeRole",
"Condition": {
"Bool": {
"aws:MultiFactorAuthPresent": "true"
}
}
}
]
}
EOF
}
resource "aws_iam_group_policy_attachment" "admins_assume_role" {
group = "${aws_iam_group.admins.name}"
policy_arn = "${aws_iam_policy.assume_role.arn}"
}
resource "aws_iam_policy" "assume_role" {
name = "assume_role"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": {
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Resource": "arn:aws:iam::*:role/*"
}
}
EOF
}
resource "aws_iam_user" "ch" {
name = "ch"
}
resource "aws_iam_user_login_profile" "ch_login" {
user = "${aws_iam_user.ch.name}"
pgp_key = "keybase:chrishowell"
}
resource "aws_iam_user_group_membership" "ch_groups" {
user = "${aws_iam_user.ch.name}"
groups = [
"${aws_iam_group.admins.name}"
]
}
Debug Output
https://gist.github.com/chrishowell/ddd169c24ba4f0fcaba70a3e2f624a5a
Panic Output
N/A
Expected Behavior
As of terraform-provider-aws_v2.32.0 I believe assume_role with mfa enabled should work.
Actual Behavior
Error: error creating EC2 Metadata session: AssumeRoleTokenProviderNotSetError: assume role with MFA enabled, but AssumeRoleTokenProvider session option not set.
Steps to Reproduce
terraform plan
Important Factoids
Running locally on Mac OSX Catalina
Brew install of Terraform
No ~/.aws/configuration file present
References
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 123
- Comments: 17 (3 by maintainers)
Any update on this issue? I am having the exactly same problem. I think that using assume_role with MFA is probably the most common way of access control in big organizations. In my opinion, specifying profile should be enough for Terraform to recognize the use of assume_role with MFA since the configuration is already in ~/.aws/config and ~/.aws/credentials.
To add (for
aws-vaultusers) there’s alsoinclude_profileso you can do this:This way
aws-vault execwill know about themfa_serialand prompt for MFA accordingly, and Terraform won’t see themfa_serialinassumed_roleand letsaws-vaultdo the work.As a workaround for anyone running into this when using aws-vault (maybe others?) it seems you can remove mfa_serial from the role profile while having it still in the base profile and it will work.
So for example:
Note that the assumed_role does not have a mfa_serial option set.
And then for your provider something like this should work:
One problem is this role doesn’t seem to work with aws-vault now. To fix that you will likely want to make an assumed_role profile and an assumed_role_tf profile to support both.
It almost seems like in this specific case if an error was not raised, this would simply work.
Note, I finally read https://registry.terraform.io/providers/hashicorp/aws/latest/docs/guides/version-4-upgrade#changes-to-authentication and it gave me a pretty big clue. Removing the
profileattribute from theprovider "aws" {}block allows me to run terraform in an assumed AWS role usingaws-vault.I use the following bash function to get around this issue:
This is based on this StackOverflow answer: https://stackoverflow.com/a/53199639.
I’m on Ubuntu and I believe
osascriptis macOS specific. So, it doesn’t sound like a solution I could use.Anyway, this works for me but I’m not proud of it:
Works as before, minus
not a ttyfiles created by Terraform Language Server.Hello, everyone. The issue #2420 also addresses the lack of support for MFA tokens in the AWS Provider. So that any future discussion will be in one place, I’m going to close this issue.
See https://github.com/hashicorp/terraform-provider-aws/issues/2420#issuecomment-1449084088 for the current status of the issue
This workaround works well but isn’t flawless. When using VS Code with the Terraform plugin containing Terraform Language Server for example, the language server will perform Terraform commands in the background. While it doesn’t output any error message, it tries to call
aws-vaultwhich in turn ends up writing a filenot a ttyin the project directory, since the language server doesn’t provide a tty. I have yet to find a solution to that other than addingnot a ttyto.gitignorewhich seems like a pretty crappy solution.