CodeNewbie Community 🌱

Cover image for How to create your first Golang Lambda With Serverless Framework
Albert Colom
Albert Colom

Posted on • Originally published at Medium on

How to create your first Golang Lambda With Serverless Framework

This article explains step-by-step how to implement a simple AWS Lambda with Serverless Framework.

Prerequisites to follow this article

Why use a AWS lambda?

AWS Lambda is a serverless computing service provided by Amazon Web Services (AWS). It allows you to run your code in response to events without needing to provision or manage servers. With AWS Lambda, you can focus solely on writing your application code while AWS takes care of the infrastructure and scaling for you.

Pros

  • Cost: You only pay for the compute time your functions consume. There’s no need to pay for idle server resources, making it cost-effective for applications with varying workloads.
  • Auto-Scaling : Lambda automatically scales your code to handle incoming requests or events. It can run thousands of instances of your function in parallel.
  • Stateless : Lambda functions are designed to be stateless, meaning they don’t retain memory between invocations. You can use external storage services for persistent data.

Const

  • Cold Start Latency : Cold starts occur when a function is invoked after being idle for a period of time. Cold starts can introduce latency in your application, which might not be suitable for all use cases.
  • Limitations: Lambda functions have a maximum execution time limit and have predefined resource limits (CPU, memory) that might not be sufficient for certain applications.

Why use a AWS Serverless Framework?

The Serverless Framework is a tool written in Node that simplifies the deployment and management of serverless applications and functions. It abstracts away many of the complexities involved in setting up and configuring serverless services like AWS Lambda, API Gateway, and others.

Get Started

Step 1: Install Serverless Framework

Open your terminal and run the following command to install the Serverless Framework globally:

npm install -g serverless
Enter fullscreen mode Exit fullscreen mode

NOTE : If you type serverless in the terminal you can choose a template to generate a skeleton of the project.


Serverless crete skeleton project

Step 2: Create simple Health Checks in Go

Create a directory from the project and initialize a Golang project.

NOTE : Feel free to change the name of the project.

mkdir GoServerless

cd GoServerless

go mod init go-lambda-serverless
Enter fullscreen mode Exit fullscreen mode

Finally, create your main.go with a simple health check.

package main

import (
 "context"
 "github.com/aws/aws-lambda-go/events"
 "github.com/aws/aws-lambda-go/lambda"
)

type Response events.APIGatewayProxyResponse

func Handler(ctx context.Context, request events.LambdaFunctionURLRequest) (Response, error) {
 return Response{Body: "It works!", StatusCode: 200}, nil
}

func main() {
 lambda.Start(Handler)
}
Enter fullscreen mode Exit fullscreen mode

Then install all dependencies

go mod tidy
Enter fullscreen mode Exit fullscreen mode

Step 3: Compile de project

env GOARCH=amd64 GOOS=linux go build -ldflags="-s -w" -o bin/health main.go
Enter fullscreen mode Exit fullscreen mode

Step 4: Create a Serverless config file

Create serverless.yml on the root project with the content:

service: go-serverless # Define your service name

provider:
  name: aws
  runtime: go1.x
  region: eu-central-1 # Put your AWS region

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

functions:
  health:
    handler: bin/health # route to binary
    events:
      - http:
          path: /health
          method: get
          cors: true
          private: false
Enter fullscreen mode Exit fullscreen mode

NOTE : You can find the all information on the oficial documentation: https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml

Step 5: Deploy!

Thanks to the Serverless framework it is very easy to deploy any application, just serverless deploy on the console and the magic happens :-)

serverless deploy
Enter fullscreen mode Exit fullscreen mode

NOTE : If you get more information about the deploy you can add --verbose parameter.


Serverless deploy output

And thats all! the framework takes the responsibility to create the necessary infrastructure on AWS and return the URL to the defined endpoint.

You can try the endpoint by CURL

curl -X GET https://jqlar1r8pd.execute-api.eu-central-1.amazonaws.com/dev/health
Enter fullscreen mode Exit fullscreen mode

Or if you prefer you can try via Postman


Try the endpoint by postman

You can also use the same Serverless Framework

serverless invoke -f health
Enter fullscreen mode Exit fullscreen mode


Try the service by Serverless Framework

Bonus

You can remove all the application including the infrastructure by de Serverless Framework.

serverless remove
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this article we have been able to see step by step how to create a very simple application with GO and deploy it using Serverless Framework, a great tool that makes our life much easier.

I did not pretend to make a very detailed article, I just wanted to show how to make a simple application from zero.

Top comments (0)