amazon-web-servicesgoaws-sdkaws-sdk-go

How to run AWS SDK with credentials from variables?


I used environment variables before and it worked fine.

Now I am migrating my config variables into a single file and I have AWS_SECRET_ACCESS_KEY and AWS_ACCESS_KEY_ID variables containing respective values that are loaded from this file.

I tried this code but receiving an error:

creds := credentials.NewStaticCredentials("123", conf.AWS_SECRET_ACCESS_KEY, conf.AWS_ACCESS_KEY_ID)
sess, err := session.NewSession(&aws.Config{Credentials: creds})

Here is the error

InvalidClientTokenId: The security token included in the request is invalid.

How do I properly inject my keys into the aws sdk calls?


Solution

  • Try re-ordering your args so that ACCESS_KEY is the 1st param and SECRET_KEY is the second:

    creds := credentials.NewStaticCredentials(conf.AWS_ACCESS_KEY_ID, conf.AWS_SECRET_ACCESS_KEY, "")
    

    Try adding the region as well:

    sess, err := session.NewSession(&aws.Config{
        Region:      aws.String("us-west-2"),
        Credentials: credentials.NewStaticCredentials(conf.AWS_ACCESS_KEY_ID, conf.AWS_SECRET_ACCESS_KEY, ""),
    })