Home > Blockchain >  get aws credentials from ec2 metadata services in Go
get aws credentials from ec2 metadata services in Go

Time:12-11

How can I make the GO SDK fetch the access keys for AWS from the Instance Metadata Service (169.254.169.254) provided by AWS.

I checked the official AWS SDK for go documentation and there seems to be only ways of fetching the access keys from environment variables, but no credentials retriever from IMS.

How is this done in go?

CodePudding user response:

I checked the official AWS SDK for go documentation and there seems to be only ways of fetching the access keys from environment variables, but no credentials retriever from IMS.

You just missed it. The Go SDK supports the instance metadata service as well as every other common credentials provider.

From https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html:

If you have configured your instance to use IAM roles, the SDK uses these credentials for your application automatically.

You don't have to do anything to configure this. It should just work. If you're having problems, make sure that you're not manually configuring some other credentials source.

Usually you don't have to do anything more than something like:

  sess := session.Must(
    session.NewSessionWithOptions(session.Options{
      SharedConfigState: session.SharedConfigEnable,
    }),
  )

And with or without CLI configuration, metadata service, or environment variables, it should just work wherever you run it.

  • Related