-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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 (regression?) #6566
Comments
For other's running into the same issue I worked around this by using an external data provider to supply STS credentials: #!/usr/bin/env python3
import json
import os
import select
import sys
from time import sleep
import boto3
import botocore.exceptions
def error(message):
"""
Errors must create non-zero status codes and human-readable, ideally one-line, messages on stderr.
"""
print(message, file=sys.stderr)
sys.exit(1)
def validate(data):
"""
Query data and result data must have keys who's values are strings.
"""
if not isinstance(data, dict):
error('Data must be a dictionary.')
for value in data.values():
if not isinstance(value, str):
error('Values must be strings.')
def assume_role():
if not select.select([sys.stdin,], [], [], 0.0)[0]:
error("No stdin data.")
query = json.loads(sys.stdin.read())
if not isinstance(query, dict):
error("Data must be a dictionary.")
validate(query)
if "role_arn" not in query:
error("Data parameter must define 'role_arn'.")
session = boto3.Session()
if "access_key" in query and "secret_key" in query:
session = boto3.Session(
aws_access_key_id=query["access_key"],
aws_secret_access_key=query["secret_key"],
)
if "wait" in query:
sleep(int(query["wait"]))
sts = session.client("sts")
response = {}
try:
response = sts.assume_role(RoleArn=query["role_arn"], RoleSessionName=os.path.basename(sys.argv[0]))
except botocore.exceptions.ClientError as e:
error(f"Error from AWS API: {e.response['Error']['Message']}")
sys.stdout.write(json.dumps({
"access_key": response["Credentials"]["AccessKeyId"],
"secret_key": response["Credentials"]["SecretAccessKey"],
"token": response["Credentials"]["SessionToken"],
}))
if __name__ == '__main__':
assume_role() And the following HCL configuration data "external" "aws_assume_role" {
program = ["python3", "terraform_aws_assume_role.py"]
query {
role_arn = "${aws_iam_role.terraform_11270.arn}"
wait = 10
}
depends_on = ["aws_iam_role.terraform_11270", "aws_iam_role_policy.terraform_11270"]
}
# configure this provider alias to only use the IAM Role created above
provider "aws" {
alias = "iamrole"
access_key = "${data.external.aws_assume_role.result["access_key"]}"
secret_key = "${data.external.aws_assume_role.result["secret_key"]}"
token = "${data.external.aws_assume_role.result["token"]}"
} |
I met the same issue under version: /terraform-plan/dev/application # terraform -v
but i could not see the behavior like "Replaying the plan (after ~10 seconds) succeeds in creating the security group:", the error exists always. |
I believe this is resulting from the same bug addressed here: hashicorp/aws-sdk-go-base#5 |
I have had success using the python program provided by @markchalloner - thank you :) I use profiles to choose which user to assume role as so I added the following check for a
Seems to work for me with the following HCL configuration:
|
@aeschright @bflad I've reproduced this issue. It results from eventual consistency. After the creation of a role, it cannot be assumed for 10-30 seconds. I messed with a wait state for this (see my branch) but the IAM role goes through 2 states before being ready. For 10-20 seconds, the API returns @markchalloner An easy, ugly workaround for this is to use a resource "aws_iam_role" "tf-test-6d3868d9bed3" {
name = var.role_name
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
provisioner "local-exec" {
command = "sleep 30"
}
} |
I've created a repo with tests to easily reproduce credential-related issues. Visit and contribute. The test to reproduce this issue is here: https://github.com/YakDriver/terraform-cred-tests/tree/master/tests/assume_after_create |
Is this still an issue? Do you have a link to the Python program provided by Mark. It would be of great use! Thank you. |
It's higher up in the comments 😂 Unsure if it's still an issue |
Sorry so it is haha! Anyway yes it appears to be an issue for me. |
I'm experiencing a similar issue of assuming roles, but with the
I initially tried a version >= 3.35.0 (specifically 3.47.0), which includes a fix for the read-after create eventual consistency:
However that didn't fix the issue. Though the fix proposed by @YakDriver works as expected: #6566 (comment). So I guess this issue (#6566) it is still relevant(?). Edit: Leaving this comment in case someone else goes through the same troubleshooting path. |
I seem to be unable to assume a role with the following config:
So this does appear to still be an issue. What's odd is that I'm able to run plan, just not apply. I'm thinking the issue is the role is assumed during the plan stage, and then during apply it's already been assumed and so cannot assume itself. |
Same issue occurring for IoT rules that need to assume the rule. The workaround from @YakDriver seems to be working well but as already discussed kinda "hacky". |
Community Note
Terraform Version
Affected Resource(s)
Terraform Configuration Files
Sourced from #472 (comment)
Expected Behavior
Security group
secondary
should have been created.Actual Behavior
Error thrown when trying to assume created role:
Replaying the plan (after ~10 seconds) succeeds in creating the security group:
Steps to Reproduce
terraform apply
Important Factoids
References
The text was updated successfully, but these errors were encountered: