4 min read

Setting Up a Go Project with the Serverless Framework

Learn how to set up a Go project with the Serverless Framework, enabling you to build scalable and efficient serverless applications. Follow the step-by-step guide and leverage the power of Go and the flexibility of the Serverless Framework.


Welcome to my simple guide on setting up a Go project with the Serverless Framework. If you’re ready to unlock the potential of Go and leverage the scalability and flexibility of the Serverless Framework, you’re in the right place! In this guide, we’ll take you through a step-by-step journey to configure and deploy your Go project. Let’s dive in and build high-performance serverless applications with ease.

Note: The aws-go-mod template used here is compatible with Serverless Framework v3. If you’re on v4, check the official v4 migration guide as template names and CLI syntax have changed.

Install Go

Before we begin, make sure you have Go installed on your system. Head over to golang.org/dl and follow the instructions for your OS. Once it’s done, verify with:

go version

Create a New Go Project

Open up your terminal, navigate to where you want your project to live, and initialise a new Go module:

go mod init github.com/your-username/my-go-project

You’ve just set the foundation for your Go project.

Configure AWS Credentials

Before deploying, you’ll need AWS credentials in place — the Serverless Framework won’t get far without them. The quickest way is:

aws configure

This walks you through entering your AWS Access Key ID, Secret Access Key, default region, and output format. If you’d rather not use the CLI wizard, export them as environment variables instead:

export AWS_ACCESS_KEY_ID=your-key
export AWS_SECRET_ACCESS_KEY=your-secret
export AWS_DEFAULT_REGION=eu-west-1

Install the Serverless Framework

To truly unleash the power of your Go project, we’ll integrate it with the Serverless Framework. First, install it globally:

npm install -g serverless

Now initialise a new project using the AWS Go template:

serverless create --template aws-go-mod --path my-go-project
cd my-go-project

The Serverless Framework creates a basic project structure in the my-go-project directory — a great starting point.

Configure serverless.yml

Open up the serverless.yml file and configure your service. Here’s a minimal working example to get you going:

service: my-go-project

provider:
  name: aws
  runtime: provided.al2
  region: eu-west-1

package:
  patterns:
    - '!./**'
    - ./bin/**

functions:
  hello:
    handler: bin/main
    events:
      - httpApi:
          path: /hello
          method: get

This is where you tailor the framework to your specific needs — add more functions, events, or environment variables as your project grows.

Build and Deploy the Serverless Application

Time to witness your Go project in action! One thing to be careful about here: Go compiles to a native binary, but Lambda runs on Linux. If you’re developing on macOS or Windows you must cross-compile, otherwise the binary will fail silently on Lambda:

GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o bin/main ./main.go

Now deploy your serverless application:

serverless deploy

The Serverless Framework packages your Go binary, uploads it to S3, and creates the Lambda function and API Gateway endpoint. On success it prints the live endpoint URL — that’s your function running in the cloud.

Test Your Serverless Go Application

Invoke your function directly through the framework:

serverless invoke -f hello

Or hit the HTTP endpoint printed after deployment with curl or your browser. Either way, you should see your function execute and return a response.

Conclusion

You did it! By following this guide, you’ve set up a Go project with the Serverless Framework and deployed it to AWS Lambda. With the cross-compilation flags in place, a proper serverless.yml, and credentials configured, the whole process takes only a few minutes. From here, explore adding DynamoDB access, SQS triggers, or more HTTP routes — the serverless Go world is yours to build on. Happy coding!

kalidass ~ zsh