Skip to main content

How to Create Golang Lambda with Serverless Framework



 This is a beginner friendly step by step guide developers to create a Lambda function with Golang. We gonna use Serverless Framework. 

First, create the project directory and setting up the Go Project

go mod init github.com/<username>/go-serverless-lambda

Open in your favourite IDE, create the first file, main.go

package mainimport "fmt"func main() {
fmt.Println("Hello world!")
}

then run it:

go run .

Next step, install and setting up the serverless framework:

npm init

npm install -g serverless

then, create a new file called serverless.yml:

service: goserverlesslambda
provider:
  name: aws
  runtime: go1.x
  region: {your_region}
package:
  patterns:
    - "!./**"
    - "./bin/**"
functions:
  goserverlesslambda:
    handler: bin/goserverlesslambda
    events:
      - http:
          path: /
          method: post
          cors: true
          private: false

To read and store apps configuration, we can use .env files. So, we've to install dotenv packages.

npm i cross-env env-cmd serverless-dotenv-plugin -D

add in your package.yml files

...plugins:
- serverless-dotenv-plugin
useDotenv: true...

then, edit the .env file:

AWS_TOKEN={YOUR_AWS_TOKEN}
AWS_SECRET={YOUR_AWS_SECRET}
AWS_PROFILE={THE AWS PROFILE YOU WISH TO USE}
AWS_REGION={YOUR TARGET AWS REGION}

add the builder script like this:

"scripts": {
"setup:dev": "env-cmd -f .env cross-env-shell serverless config credentials --provider aws -o --key '$AWS_TOKEN' --secret '$AWS_SECRET' --profile '$AWS_PROFILE'",
"build": "env GOOS=linux GOARCH=amd64 go build -o bin/goserverlesslambda .",
"deploy:dev": "env-cmd -f .env cross-env-shell serverless deploy --stage development --aws-profile '$AWS_PROFILE' --region '$AWS_REGION'",
},

run the setup development:

npm run setup:dev

Begin Using Lambda

Install Go Lambda package:

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

on the main.go file add this script:

package main
import (
"context"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
func GenerateResponse(Body string, Code int) events.APIGatewayProxyResponse {
return events.APIGatewayProxyResponse{Body: Body, StatusCode: Code, }
}
func HandleRequest(_ context.Context, request events.LambdaFunctionURLRequest) (events.APIGatewayProxyResponse, error) {
return GenerateResponse("Hello World", 200), nil
}
func main() {
lambda.Start(HandleRequest)
}

save and build the go apps:

npm run build
npm run deploy:dev

Comments

Popular posts from this blog

Cara Disable Antimalware Service Executable di Windows 10

Disadari atau tidak, Windows 10 (dan juga windows-windows lainnya) hadir dengan banyak sekali aplikasi bloatware (aplikasi yang tidak perlu-perlu amat dimiliki oleh end user). Contohnya, adalah aplikasi yang seharusnya sudah tergantikan fungsinya oleh antivirus, seperti Antimalware Service Executable . Aplikasi ini dicurigai membuat Windows 10 mengalami inefisiensi memori/RAM, memakan resource yang tinggi, dengan Load yang tinggi (tanpa limit terkadang). Nah, berikut adalah cara men-disable nya: Tekan tombol Windows + I untuk membuka apliaksi Windows Setting. Pilih icon menu Update and Security Pilih lagi menu disamping kiri Windows Security Pada jendela baru yang muncul, ada pilihan Virus & Threat protection Klik ini Lalu matikan proses Real-time protection tersebut. Dengan Regedit. Buka dialog regedit, Windows + R dan ketik ‘regedit’ Cari Folder regedit ini HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows Defender Buat sebuah DWORD baru dengan klik kanan

Setup Debian 11 Official Repository In sources.list (/etc/apt/sources.list)

When you install Debian 11 using a DVD, the OS installer sets the DVD as the source for getting packages for your system if you didn't choose to scan for network mirrors. Due to this reason, the system would ask you to insert a DVD if the disc is not present in the DVD drive when you try to install any software.  Also, the packages on DVD may not be the latest release.  So, we need to get the packages from the Internet. Media change: please insert the disc labeled 'Debian GNU/Linux 11.0.0 _Bullseye_ - Official amd64 DVD Binary-1 20210814-10:04' in the drive '/media/cdrom/' and press [Enter] The /etc/apt/sources.list file with DVD as a source would look something like below. # deb cdrom:[Debian GNU/Linux 11.0.0 _Bullseye_ - Official amd64 DVD Binary-1 20210814-10:04]/ bullseye contrib main deb cdrom:[Debian GNU/Linux 11.0.0 _Bullseye_ - Official amd64 DVD Binary-1 20210814-10:04]/ bullseye contrib main deb http://security.debian.org/debian-security bullseye-security

How to Install Traefik in Debian (Without Docker)

 Download the suitable version of Traefik for your system from here: https://github.com/traefik/traefik/releases as an example, we'll be download the linux 64 bit version: wget https://github.com/traefik/traefik/releases/download/v2.8.7/traefik_v2.8.7_linux_amd64.tar.gz extract those package: tar -xzvf traefik_v2.8.7_linux_amd64.tar.gz set the traefik as executable and move the traefik binary to linux bin folder