Blog

Go AWS SDK – Integrating with the AWS cloud

25 Nov, 2018
Xebia Background Header Wave

In previous blogs about Go we have looked at the Go programming language, how to run go in AWS Lambda and how to cross compile Go code so that it runs on Linux, Mac and Windows platforms. In this blog we will take a closer look at the AWS SDK for Go that makes it easy to integrate Go applications with the full suite of AWS services including Amazon S3, Amazon DynamoDB, Amazon SQS, and more.

Installing the SDK

We have to add the AWS SDK for Go to the Go workspace. To install the AWS SDK for Go type:

$ go get -u github.com/aws/aws-sdk-go/...

Configuring the SDK

The SDK provides service clients that interact with AWS services. The service clients collect AWS settings from the environment, validate request messages and sign and secure the interaction with AWS. The service clients can be configured by setting the log-level, number of retries, the region, credentials or profile to use. Most of these settings are configurable by means of environment variables.

Hello AWS SDK

The ‘hello world’ of the AWS SDK is executing the GetCallerIdentity that returns details about the IAM identity that was used to call AWS. To create services we have to do the following:

  • create a session
  • create a service
  • construct a request
  • call the service operation with the request
  • handle the response
package main

import (
    "fmt"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/sts"
)

func main() {
    // create a session
   sess, _ := session.NewSession()
    // create a service client
   stsClient := sts.New(sess)
    // create a request
   request := sts.GetCallerIdentityInput{}
    // call the service operation
   id, _ := stsClient.GetCallerIdentity(&request)
    // handle the response
   fmt.Printf("AccountId: %s, Arn: %s, UserId: %s", *id.Account, *id.Arn, *id.UserId)
}

To run the example type:

$ go run helloworld.go

$ AWS_PROFILE=default go run helloworld.go

$ AWS_PROFILE=binx AWS_REGION=us-east-1 go run helloworld.go

Listing S3 buckets

The following example shows how to list all buckets in an account.

package main

import (
    "fmt"
    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/s3"
)

func main() {
    sess, _ := session.NewSession()
    svc := s3.New(sess, aws.NewConfig().WithRegion("eu-west-1"))
    request := s3.ListBucketsInput{}
    resp, _ := svc.ListBuckets(&request)
    for _, bucket := range resp.Buckets {
        fmt.Printf("BucketName: %s, CreationDate: %sn", *bucket.Name, *bucket.CreationDate)
    }
}

Conclusion

AWS has done a good job creating and documenting an SDK that is very Go idiomatic. The basic workflow of working with AWS services is to first create a session, and then creating and/or configuring an AWS service client that you want to interact with. Then create a request, making the service call and handling the response. Apart from interacting with AWS services, the SDK also provides SDK utilities for making interacting with services more friendly. For example an Amazon DynamoDB Attributes Converter for converting Amazon DynamoDB attribute values to and from concrete Go types or Amazon S3 Transfer Managers for transferring and resuming uploads to S3.

Questions?

Get in touch with us to learn more about the subject and related solutions

Explore related posts