ruk·si

AWS
Setup EB + Go

Updated at 2015-10-05 22:40

This is a simple guide how to create a Go application in Elastic Beanstalk, behind load balancer and optional auto-scaling.

You need to have Go, Python and AWS command line tools installed..

pip install awscli awsebcli

# list available elastic beanstalk platform in a region
eb platform list -r us-west-2
# we are going to use the go-1.4

Write the Application

mkdir monar # example project name is `monar`
cd monar
touch application.go
// application.go
package main

import (
    "fmt"
    "net/http"
    "time"
    "os"
)

func main() {
    // EB provides this environmental variable.
    port := os.Getenv("PORT")
    if len(port) == 0 {
        port = "3000" // for local
    }
    port = ":" + port
    http.HandleFunc("/", indexHandler)
    fmt.Printf("Starting server at %s\n", port)
    err := http.ListenAndServe(port, nil)
    if err != nil {
        panic(err)
    }
}

func indexHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Time was %s.", time.Now())
}
go run application.go
# http://localhost:3000/

Defining the Elastic Beanstalk Application

Create new key pair. EC2 > Key Pairs > Create Key Pair. Name it eb-monar. Move it to ~/.ssh and execute chmod 0600 eb-monar.pem on it.

Create a new role eb-service-role-monar. This is assigned to the Elastic Beanstalk manager. Use the managed policy: AWSElasticBeanstalkFullAccess and add the following trust relationship to the role.

{
    "Version": "2012-10-17",
    "Statement": [
      {
        "Sid": "1",
        "Effect": "Allow",
        "Principal": {
          "Service": "elasticbeanstalk.amazonaws.com"
        },
        "Action": "sts:AssumeRole",
        "Condition": {
          "StringEquals": {
            "sts:ExternalId": "elasticbeanstalk"
          }
        }
      }
    ]
}

Create a new role eb-instance-role-monar. This is assigned to each individual instance.

# creates the app specification to AWS and `.elasticbeanstalk` directory
# note that these are just defaults, `create` can overwrite any of these
eb init \
    --platform go-1.4 \
    --region us-west-2 \
    --keyname eb-monar

# each app has multiple app environments e.g. staging and prodution
# WebServer-Standard or Worker, serving or consumes queues?
# create the role first
# instance name is the same as environment name so include the app name
eb create \
    --service-role eb-service-role-monar \
    --instance_type t1.micro \
    --tier "WebServer-Standard" \
    --cname monar-staging-sapphire \
    --scale 1 \
    monar-staging-sapphire

Deployment

eb deploy monar-staging-sapphire

Sources