How To Add Golang AWS Lambda For Who Don’t Know Anything About AWS

Yunus Kılıç
4 min readFeb 28, 2019

--

Serverless development is very popular nowadays. Especially Amazon, Microsoft, Google are competing for market share. There are some comparison articles [1] [2].

Golang “is a statically typed, compiled programming language designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson. Go is syntactically similar to C, but with the added benefits of memory safety, garbage collection, structural typing, and CSP-style concurrency” [3], very popular nowadays. Golang is supported by AWS Lambda a while.

If you are interested in serverless and golang, don’t know about any information like me, I will share my experience when I tried to first deploy my golang function to AWS Lambda. There are some other tutorials but they assumed that you know something about AWS before the start. So I will try to state steps which require AWS knowledge.

1-) First of all, there are two approaches to deploy your function to AWS Lambda. One is using AWS Console other is aws-cli. I am going to use aws-cli which requires some extra steps.

2-) Download aws-cli from https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html

3-) After install aws-cli, you need to configure aws-cli. Run

$aws configure
AWS Access Key ID [None]: accesskey
AWS Secret Access Key [None]: secretkey
Default region name [None]: eu-central-1
Default output format [None]: json

So, what is access key, secret, region, output? If your first time, you could get in trouble. In order to handle this, you have to create a user and group. You haven’t probably heard about AWS IAM(Identity and Access Management). You can create a user and group from aws-cli. But I prefer AWS Console.

  • Click services
  • Select IAM
  • Select Users
  • Click Add User
  • Enter username
  • Select AWS Management Console access
  • Select and enter custom password
  • Disable User must create a new password at next sign-in
  • Click Next: Permissions
  • Create group
  • Enter group name
  • Select AdministratorAccess policy
  • Create group
  • Next & Next & Create User

User created, now you can get access key and secret.

At IAM dashboard find your user and click on it.

  • At summary screen, click Security Credentials and Create Access Key
  • Your access key and secret are ready.

The region is preferred zone, you can set default output format as JSON. You can complete configuration.

Furthermore, in order to create a function from aws-cli, you need a role. So let’s create a role from the AWS Console.

  • At IAM dashboard click role, then create role
  • Select AWS Service and Lambda, Next: Permissions
  • Select AWSLambdaBasicExecutionRole is enough for the first step. Give a name for the role. Then create it.
  • Click roles to see the new role, click on it.
  • On the summary screen, you can see Role ARN at the top. Please copy this field to deployment operation described below.

Example role arn

arn:aws:iam::XXXXXXX:role/service-role/serviceRole

Golang Part

package mainimport (
"context"
"fmt"
"github.com/aws/aws-lambda-go/lambda"
)
type MyEvent struct {
Message string `json:"message"`
}
func HandleRequest(ctx context.Context, message MyEvent) (string, error) {
return fmt.Sprintf("Hello world! My message is :%s!", message.Message), nil
}
func main() {
lambda.Start(HandleRequest)
}

Go requires “github.com/aws/aws-lambda-go/lambda” for AWS Lambda.

(go get github.com/aws/aws-lambda-go/lambda)

Build with and zip output file because golang functions can be deployed as zip files.

$go build -o main
$zip -j godeployment.zip main

Zip main file and call godeployment.zip then Deploy with below command.

  • Function name: A name as you wish
  • Zip file: Path of your output zip file
  • Handler: Handler function
  • Runtime: go1.x for golang
  • Role: Previously we created a role then copied its arn, paste it here (arn:aws:iam::XXXXXXX:role/service-role/serviceRole)
$aws lambda create-function --function-name goFunc --zip-file fileb://godeployment.zip --handler main --runtime go
1.x --role arn:aws:iam::XXXXXXX:role/service-role/serviceRole
{
"FunctionName": "goFunc",
"FunctionArn": "arn:aws:lambda:eu-central-1:148647902568:function:goFunc",
"Runtime": "go1.x",
"Role": "arn:aws:iam::XXXXXXX:role/service-role/serviceRole",
"Handler": "main",
"CodeSize": 3014390,
"Description": "",
"Timeout": 3,
"MemorySize": 128,
"LastModified": "2019-02-28T10:40:42.582+0000",,
"Version": "$LATEST",
"TracingConfig": {
"Mode": "PassThrough"
},
"RevisionId": "eed6928a-ed1a-49c0-9050-0e6c558b73d8"
}

Completed :)) Let’s test it.

  • Click services then lambda
  • Find goFunc and click on it
  • Click test
  • Enter the name and change JSON like below
{
"message": "hello from AWS Lambda"
}
  • Create and test it. Then see log details by clicking details
"Hello world! My message is :hello from AWS Lambda!"

Cites:

https://www.infoworld.com/article/3265750/serverless-in-the-cloud-aws-vs-google-cloud-vs-microsoft-azure.html

https://www.itworld.com/article/3265750/serverless-in-the-cloud-aws-vs-google-cloud-vs-microsoft-azure.html?page=2

http://www.wikizero.biz/index.php?q=aHR0cHM6Ly9lbi53aWtpcGVkaWEub3JnL3dpa2kvR29fKHByb2dyYW1taW5nX2xhbmd1YWdlKQ

--

--

Yunus Kılıç
Yunus Kılıç

Written by Yunus Kılıç

I have 10 years of experience in high-quality software application development, implementation, and integration.

No responses yet