Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

get aws credentials from ec2 metadata services in Go

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading