In this tutorial I’ll show you how to configure GO in an AWS EC2 instance and use the AWS GO SDK.

Configuring GO

Connect to your EC2 instance

ssh ec2-user@PUBLIC_IP -i yourpemfile.pem

Execute the following commands to install & configure GO

sudo su
mkdir /downloads
cd downloads
wget https://storage.googleapis.com/golang/go1.6.2.linux-amd64.tar.gz
tar xvfz go1.6.2.linux-amd64.tar.gz -C /usr/local
cd /usr/local/go
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
cd $HOME
mkdir work
export GOPATH=$HOME/work
cd work
mkdir bin | mkdir pkg | mkdir src

You should put the export commands in your shell startup script ($HOME/.bashrc)

vi $HOME/.bashrc

export GOPATH=$HOME/work
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin

You now have GO configured in your EC2 instance. You can test that it’s working by executing the following commands

cd src | mkdir aws-sdk-go
cd aws-sdk-go

vi main.go
package main

import "fmt"

func main() {
	 fmt.Printf("hello, world\n")

go run main.go

You should see the text “hello, world” when running the go command.

AWS credentials

To access AWS services you need to set your account credentials. You have three valid methods:

  1. Environment variables

  2. Shared credential files

    Edit ~/.aws/credentials
    aws_access_key_id = ACCESS_KEY_ID
    aws_secret_access_key = SECRET_ACCESS_KEY_ID
  3. EC2 instance profile credentials (IAM roles) This is the preferred method if running on EC2
    A) Create an IAM Role S3 Access
    B) Assign the role to the EC2 instance

You should never hardcode your credentials in your code. It makes it difficult to maintain (if keys are changed for example) and they might be exposed by mistake.

Let’s download the AWS SDK and create our S3 test app.

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

Edit the main.go file:

package main

import (

func main() {
        client := s3.New(session.New(),&aws.Config{Region: aws.String("eu-west-1")})
        result, err := client.PutObject(&s3.PutObjectInput{
                Bucket: aws.String("yourbucketname"),
                Key:    aws.String("testkey"),
                Body:   strings.NewReader("Hello"),
        if err != nil {

You should change yourbucketname with a bucket that exists in your AWS account. You should also set the region bucket, changing eu-west-1 with your desired region.

Time to test your first GO AWS SDK app:

go run main.go

You should get a response similar to this:

[root@ip-10-0-1-39 ec2-user]# ./main 
  ETag: "\"8b1a9953c4611296a827abf8c47804d7\""

If your credentials are incorrect you will get a response similar to this:

[root@ip-10-0-1-39 ec2-user]# ./main 
InvalidAccessKeyId: The AWS Access Key Id you provided does not exist in our records.
	status code: 403, request id: C2A2BA80682D8ABB


In this short tutorial you’ve learned how to configure GO to access AWS services using Amazon’s SDK for GO. If you want to learn more about the AWS SDK for GO: http://docs.aws.amazon.com/sdk-for-go/latest/v1/developerguide/sdkforgo-dg.pdf