Using The Go SDK

Using Environment Settings With v1

There’s a version 2 of the AWS SDK for Go, but sometimes you have no choice but to stay on version 1. You might be using an alternative DynamoDB client that has a nicer API that the one provided by AWS.

Now, let’s say you want have the client read the API keys from environment variables. You may be setting up the client in a way similar to this:

sess := session.Must(session.NewSession())
db := dynamo.New(sess, &aws.Config{
    Region: aws.String("ap-southeast-2"),
})

Running this will give you this error:

NoCredentialProviders: no valid providers in chain. Deprecated.
    For verbose messaging see aws.Config.CredentialsChainVerboseErrors

Now, it’s possible to get the API keys from the environment with the session configured this way. What you’ll need to do is set the environment variable AWS_SDK_LOAD_CONFIG to true. Doing so should actually clear this error and your client will start working as expected.

If you don’t want to do that, and just use the API keys provided in the environment, you can explicitly enable using shared configuration:

sess := session.Must(session.NewSessionWithOptions(session.Options{
	SharedConfigState: session.SharedConfigEnable,
}))
db := dynamo.New(sess, &aws.Config{
	Region: aws.String("ap-southeast-2"),
})

This bypasses the AWS_SDK_LOAD_CONFIG setting completely.

Last updated