AWS Session in AWS SDK for GO

How to establish AWS Session
Share this page:

About Sessions

In the AWS SDK for Go, a session is an object that contains configuration information for service clients (Service Client provides an abstraction for each AWS Service). which you use to interact with AWS services. For example, sessions can include information about the region where requests will be sent, which credentials to use, or additional request handlers. Whenever you create a service client, you must specify a session.

Package session provides configuration for the SDK’s service clients. Sessions can be shared across service clients that share the same base configuration.

In Golang, there are various sessions we need to establish from our code:

  • AWS Region Session: One per each AWS Region our resources are.
  • Service Session: One per each AWS Service we’re “consuming”.

If you’ve just landed here, we’re doing a “Become a Cloud Architect” Unicorn Workshop by building a Unicorn Pursuit Web App step by step, and you’re more then welcome to join!

Code

Since all the functions are called from routes.go package, thats where all the sessions are. This way, we can pass the session as a variable to get function executed, when route is “called”. In routes.go we firs have the imports:

import "github.com/aws/aws-sdk-go/aws"
import "github.com/aws/aws-sdk-go/aws/session"

And then within the function func initializeRoutes(), which is executed only once, so we only need to authenticate once with AWS, we have the code to create the session with Ireland region, and “store” it in a sess variable.

// Create AWS Session
conf := &aws.Config{Region: aws.String("eu-west-1")}
sess, err := session.NewSession(conf)
if err != nil {
 panic(err)
}

Where to find more info




Last modified June 7, 2020: Golang4DockerAdded (ad39767)