简体   繁体   中英

Terraform: Credential error when importing aws resources - error calling sts:GetCallerIdentity: ExpiredToken

I have some aws resources that I want to import into my terraform state.

I have set the aws credentials in ~/.aws/credentials file:

[my-profile]
aws_access_key_id=<ACCESS_KEY_ID>
aws_secret_access_key=<SECRET_ACCESS_KEY>
aws_session_token=<SESSION_TOKEN>

terraform init ran successfully, but when I try to import the resources with:

terraform import -var-file=config/us-west-2/default.tfvars -var-file=variables.tfvars -var-file=../globals.tfvars -var profile=my-profile -var region=us-west-2 -var tfstate_bucket=<MY_TF_BUCKET> -no-color <RESOURCE_NAME> <RESOURCE_ID>

I am getting following error even when my credentials have not expired:

Error: error using credentials to get account ID: error calling sts:GetCallerIdentity: ExpiredToken: The security token included in the request is expired
    status code: 403, request id: 1684ef71-ecea-4fed-bcf5-a32eca688ab4

Please Note that I have ran similar imports in another non-production aws account, but I get this error when I try to run it against production account. I do have power-user access to the Production account though.

Terraform version:

Terraform v0.12.29
+ provider.aws v3.14.1
+ provider.null v2.1.2
+ provider.random v2.3.1
+ provider.template v2.1.2

EDIT: Printing environment variables on my mac

$ env
TERM_PROGRAM=Apple_Terminal
TERM=xterm-256color
SHELL=/bin/bash
TMPDIR=/var/folders/jz/w997kd9x4sb983kdyj8wjlkc0000gp/T/
GRADLE_HOME=/Users/john/.sdkman/candidates/gradle/current
Apple_PubSub_Socket_Render=/private/tmp/com.apple.launchd.k5lBL3QJ9R/Render
TERM_PROGRAM_VERSION=421.2
TERM_SESSION_ID=D2EB66E5-2DD7-456A-8E16-A0E00CE15112
SDKMAN_PLATFORM=Darwin
USER=john
SDKMAN_CANDIDATES_API=https://api.sdkman.io/2
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.GG8D8SvuqA/Listeners
PATH=/Users/john/.sdkman/candidates/gradle/current/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
PWD=/Users/john/MyProjects/tf/foobar
LANG=en_US.UTF-8
SDKMAN_VERSION=5.7.4+362
XPC_FLAGS=0x0
XPC_SERVICE_NAME=0
HOME=/Users/john
SHLVL=1
LOGNAME=john
SDKMAN_DIR=/Users/john/.sdkman
SDKMAN_CANDIDATES_DIR=/Users/john/.sdkman/candidates
_=/usr/bin/env

EDIT 2: When I cleaned up all the old profiles and credentials so that ~/.aws/credentials and ~/.aws/config only has entry for my-profile and nothing else.

Also all related environment variables:

for var in AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN AWS_SECURITY_TOKEN ; do eval unset $var ; done

So now I am getting:

Error: No valid credential sources found for AWS Provider.
    Please see https://terraform.io/docs/providers/aws/index.html for more information on
    providing credentials for the AWS Provider

As discussed in the comments, the most likely answer is that you have some old credentials "stored" somewhere:

  • either still in your current terminal session's environment variables
  • or in one of the .tfvars file your command mentions

I suggest to clean them up. Get new credentials. And try again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM