Wednesday 10 May 2023

Uploading and Running Lambda function in AWS

Main.go
package main

import (
"fmt"
"encoding/json"
"log"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)


func main() {
lambda.Start(handler)
}

func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
var person Person
//json request body se nikaal ke person var me rakh denge
err := json.Unmarshal([]byte(request.Body), &person)
if err !=nil {
return events.APIGatewayProxyResponse{}, err
}

//ab person se firstname aur lastname nikaal ke msg bana lenge
msg := fmt.Sprintf("Hello %v %v", *person.FirstName, *person.LastName)
responseBody := ResponseBody{
Message: &msg,
}
jbytes, err := json.Marshal(responseBody)
if err !=nil {
return events.APIGatewayProxyResponse{}, err
}

log.Println("hello world Nihal")

response := events.APIGatewayProxyResponse{
StatusCode: 200,
Body: string(jbytes),
}

return response, nil
}

type Person struct {
FirstName *string `json:"firstName"`
LastName *string `json:"lastName"`
}

type ResponseBody struct {
Message *string `json:"message"`
}
Run command : GOARCH=amd64 GOOS=linux go build main.go 

(This command will set the OS as linux and run build which will generate main file) 

 Once the main file is created and we can create a zip of this main and upload it to lambda. 

 Configure API-gateway with the above lambda. 

 Through Postman send the post request. { "firstName" : "Nikhillll2", "lastName" : "Nihallll2" }

Uploading and Running Lambda function in AWS

Main.go package main import ( "fmt" "encoding/json" "log" "github.com/aws/aws-lambda-g...