Skip to content
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 Session Token not working #3243

Closed
cdorros opened this issue Sep 15, 2015 · 14 comments
Closed

AWS Session Token not working #3243

cdorros opened this issue Sep 15, 2015 · 14 comments

Comments

@cdorros
Copy link

cdorros commented Sep 15, 2015

Experiencing an issue in which AWS Session Token (STS) works using the aws cli but not via Terraform.

This works:

$ AWS_ACCESS_KEY_ID='mykey' AWS_SECRET_ACCESS_KEY='mysecretkey' AWS_SESSION_TOKEN='mysessiontoken' aws ec2 describe-instances

This doesn't:

$ AWS_ACCESS_KEY_ID='mykey' AWS_SECRET_ACCESS_KEY='mysecretkey' AWS_SESSION_TOKEN='mysessiontoken' terraform plan
Error refreshing state: 1 error(s) occurred:

* 1 error(s) occurred:

* InvalidClientTokenId: The security token included in the request is invalid
    status code: 403, request id: [xxxxxxx]

I've verified it isn't pulling creds from any other variables (like tfvars or ~/.aws), and that the account/region matches. I've also tried exporting the env vars first instead of inline.

$ terraform version
Terraform v0.6.3

$ aws --version
aws-cli/1.8.3 Python/2.7.6 Linux/3.13.0-62-generic
@pearcec
Copy link
Contributor

pearcec commented Sep 23, 2015

I had a similar issue. When I dropped the quotes it worked. Which shouldn't be IMO, but that was the case. Try that to see if it fixes it.

@sherabi
Copy link

sherabi commented Nov 3, 2015

I tried with quotes, without quotes and single quotes. Still doesn't work. Using 0.6.6

@sherabi
Copy link

sherabi commented Nov 3, 2015

Use AWS_SECURITY_TOKEN and NOT AWS_SESSION_TOKEN

@artburkart
Copy link
Contributor

I am experiencing the same bug. Terraform reads my terraform.tfvars file on my mac, but not on an ubuntu ec2 instance. I have to manually feed the values into my command.

This doesn't work on ubuntu ec2 instance:
terraform plan
terraform plan -var-file="terraform.tfvars"

This does work:
AWS_ACCESS_KEY_ID=<aws_access_key_id/> AWS_SECRET_ACCESS_KEY=<aws_secret_access_key/> terraform plan

All of the above works on my osx box.

Versions:

$ terraform -v
Terraform v0.6.3

Your version of Terraform is out of date! The latest version
is 0.6.6. You can update by downloading from www.terraform.io

$ aws --version
aws-cli/1.2.9 Python/3.4.0 Linux/3.13.0-48-generic

@artburkart
Copy link
Contributor

If there is a bit of code someone could point me to, I'd me more than happy to attempt a PR. This bug is kind of a bummer.

@sherabi
Copy link

sherabi commented Nov 20, 2015

Ok so after trying a lot here is the conclusion I have reached...
If you are using tokens do not rely on your tfvars file, instead export all the environment variables without quotes as such....

export AWS_ACCESS_KEY_ID=SOMETHING-WITHOUT-QUOTES
export AWS_SECRET_ACCESS_KEY=SOMETHING-ELSE-WITHOUT-QUOTES
export AWS_SESSION_TOKEN=LONG-TOKEN-WITHOUT-QUOTES

This seems to work on Macs and Linux with terraform any version.

@phinze
Copy link
Contributor

phinze commented Dec 3, 2015

Going to merge this back down with #2693 - we'll get this looked at soon.

@gtmtech
Copy link

gtmtech commented Dec 22, 2015

In case anybody comes across this, and has a similar situation to me, I find the above language very very confusing, and specifically:

A "session token" is that given out by STS in response to an AssumeRole() call. It can be used within Terraform by defining AWS_SESSION_TOKEN environment variable beforehand:

export AWS_SESSION_TOKEN=<TOKENDATA>

or better still... defining aws_session_token within $HOME/.aws/credentials, using an aws configure line like:

aws configure --profile=someprofilename set aws_session_token <TOKENDATA>

The boto framework seems to use an incorrect terminology (in case anyone is using that in conjunction with terraform like me, and calls it an aws_security_token instead). This can exist in $HOME/.aws/credentials as aws_security_token, but you cannot set it using aws configure as above, because it is not an AWS approved variable-name. Boto seems to be basically wrong in this case (or maybe out of date). You can get it working with the boto framework by setting AWS_SECURITY_TOKEN

export AWS_SECURITY_TOKEN=$( aws configure --profile=someprofilename get aws_session_token )

(yes I really did mean to set AWS_SECURITY_TOKEN to the value of aws_session_token, thats the massive confusion from the boto framework.

And just to confuse matters, Terraform implements its own AWS_SECURITY_TOKEN, which is something else entirely - it is the MFA token you might present if your user login requires an MFA token.

I hope this clears up the naming around these variables for someone else. I found it all very confusing..

@catsby
Copy link
Contributor

catsby commented Jan 4, 2016

Thank you for the clarification here @gtmtech , hopefully #4254 provides some fixes here too

@MrAtheist
Copy link

I'm having the exact same problem in 0.6.9. But funny that it wasnt a problem in 0.6.8 or 0.6.7. Anyone else experience this? I'm exporting the following and somehow the new release broke it...

export AWS_SECRET_ACCESS_KEY=hello
export AWS_ACCESS_KEY_ID=itsme
export AWS_SESSION_TOKEN=IwaswonderingifafteralltheseyearsYou'dliketomeettogoovereverything
export TF_VAR_secret_key=$AWS_SECRET_ACCESS_KEY
export TF_VAR_access_key=$AWS_ACCESS_KEY_ID

All i get back is the InvalidClientTokenId error. Any clue?

@jonathanallen
Copy link

At some point a feature was added that makes the IAM GetUser call to test for valid credentials. If you are calling terraform with dynamic credentials generated by IAM GetSessionToken, those credentials cannot be used to make IAM calls unless you are using MFA. The AWS docs say: "Cannot call IAM APIs unless MFA information is included with the request."

CLI call confirms it:

$ aws iam get-user

A client error (InvalidClientTokenId) occurred when calling the GetUser operation: The security token included in the request is invalid

It would be nice to be able to session tokens if you weren't using terraform to manage AWS IAM resources. I am guessing the appropriate way to address this would be to use AssumeRole and use those credentials.

@bvinayak
Copy link

Hi,

It worked for me when i triggered "aws configure" and setting all the values again.

@hpganesh1
Copy link

it did not work when i saved the creds directly in the config file. But when i set it through the cli using aws configure it started working.

@ghost
Copy link

ghost commented Apr 7, 2020

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.

@ghost ghost locked and limited conversation to collaborators Apr 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests