AWS: Creating a Lambda using GoLang and deploying it using Terraform
Quick tutorial on how to create AWS Lambda using GoLang, compiling and deploying it using Terraform
Reference of my previous video that explains how to configure your CLI and create your AWS role: • AWS: Creating a Step F...
-----
Topics:
- Create GoLang lambda
- Compile for AWS expected binary
- Create Terraform for your lambda
- Deploy and invoke lambda
-----
Commands used:
go get github.com/aws/aws-lambda-go/lambda
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o target/golambdabin -ldflags '-w' src/golambda.go
zip -j target/golambda.zip target/golambdabin
terraform init
terraform apply
aws --profile dev-01 lambda invoke --function-name dev-golambda --payload '{"action":"process", "data":"123"}' response.json
-----
Code:
package main
import (
"fmt"
"github.com/aws/aws-lambda-go/lambda"
)
type MyInput struct {
Action string `json:"action"`
Data string `json:"data"`
}
type MyResponse struct {
StatusCode string `json:"statusCode"`
StatusMessage string `json:"statusMessage"`
}
func handle(event MyInput) (MyResponse, error) {
fmt.Printf("%v
", event)
return MyResponse{StatusCode: "200", StatusMessage: "Success"}, nil
}
func main() {
lambda.Start(handle)
}
-----
Terraform:
provider "aws" {
region = "us-east-2"
profile = "dev-01"
}
resource "aws_lambda_function" "dev-golambda" {
function_name = "dev-golambda"
handler = "golambdabin"
runtime = "go1.x"
role = "YOUR ARN ROLE HERE"
filename = "../target/golambda.zip"
source_code_hash = "${base64sha256(file("../target/golambda.zip"))}"
memory_size = 128
timeout = 10
}
-----
Thank you so much for watching! If you liked, please come back soon for more examples shortly.
In the future I will also add SNS, Elastic Search, Kibana and other examples.
If there is another AWS serverless topic you are interested, please add a comment and I will do my best to show an example using it.
See you soon!
-- Contents of this video --
0:00 Intro
0:31 Create GoLang lambda
1:57 Compile for AWS expected binary
3:00 Create Terraform for your lambda
4:35 Deploy and invoke lambda
Негізгі бет AWS: Creating a Lambda using GoLang and deploying it using Terraform
Пікірлер: 9