Deploying a Golang package to AWS Lambda in 5 minutes

Image for post
Image for post

Last year, Lambda added Go as a supported runtime for Lambda functions. In the article, I’ll explain the process as to how to build/deploy a Go project on to Lambda.

Step 1: Install the Go Programming Language
NOTE: To run Go using AWS Lambda, you will need to compile any Go binaries on Amazon Linux to ensure compatibility.

In your Amazon Linux environment, install Go with the following commands:

sudo yum -y update
sudo yum -y install go

Once installed, configure your $GOPATH environment variable. $GOPATH is used by Go to determine where dependencies are located. To set up $GOPATH, create a “go” folder in your home directory:

mkdir ~/go

Edit your shell's profile to include the location of $GO_PATH:

echo "export GOPATH=\$HOME/go" >> ~/.bash_profile
source ~/.bash_profile

Step 2: Create your Go Project
Create a directory to house your Go project:
mkdir ~/go_helloworld
cd ~/go_helloworld

Once the directory has been made, we can create our Go file. Create a text file called “helloworld.go” and write the following script:

From this script, we can see that we are declaring a package called “main”. In the main function, we are calling “lambda.Start”. This method is referenced in the import statement, and is called for subsequent invocations of the Lambda function.

In order for this script to be “buildable” by Go, we need to source the dependancies of the script. As we only have one dependancy, we can get it using the following command:

go get ""

This dependancy will be saved locally in the $GOPATH, so that it does not need to be downloaded again. Once done, we can next build the project. To build the Go binary, run:

go build helloworld.go

Go will quietly create a binary file within the same directory. This is the file that Lambda will use to execute. Next, archive the file into a .zip for deployment to Lambda:

zip helloworld

Step 3: Create an IAM Role for your Lambda function (Optional)

This is only necessary if you don’t already have an IAM Role created that grants Lambda the ability to assume it.

If we don’t already have an IAM role for Lambda, we’ll need to create one in order for the Lambda function to be able to log outputs to Cloudwatch. By default, it is required that Lambda functions have a valid IAM role configured.

Create a file called “lambda-trust-policy.json” and paste the contents of the cat statement into the file. Once done, use the following commands to create an IAM role for Lambda and attach some basic permissions to the role.

$ cat lambda-trust-policy.json
"Version": "2012-10-17",
"Statement": {
"Effect": "Allow",
"Principal": {
"Service": ""
"Action": "sts:AssumeRole"

$ aws iam create-role --role-name lambda-basic-execution--assume-role-policy-document file://lambda-trust-policy.json

$ aws iam attach-role-policy --role-name lambda-basic-execution --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

Once done, we should have a new IAM role created called “lambda-basic-execution”. We can use the same CLI to verify that the role was created by doing a GetRole command:

$ aws iam get-role --role-name lambda-basic-execution

Note the “Arn” returned in the response. The ARN is the Amazon Resource Name that identifies the role in your AWS account. We’ll need this to create the function in the next step.

Step 4: Create your Lambda function (via CLI or AWS Console)
The ZIP file that we created in Step 2 can be used to deploy the Go program to Lambda. The name of the file “helloworld” will be used as the Lambda handler.

To create a function via the AWS CLI, you can use the following command:

aws lambda create-function \
--function-name helloworld_go \
--zip-file fileb:// \
--handler helloworld \
--runtime go1.x \
--role "arn:aws:iam::<YOUR_ACCOUNT_ID>:role/lambda-basic-execution"

Note that the above execution role needs to be set to a valid role that is assumable by Lambda. To invoke the function via the AWS CLI, you can use the following command:

aws lambda invoke \
--function-name helloworld_go \
--invocation-type "RequestResponse" \

A new file called “response.txt” should be created in the directory. Reading the file, should contain “Hello from Go!”. This process can also be done via the Console by creating a Go Lambda function and uploading the ZIP as a deployment package.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store