'run golang gin api project using aws lambda

I am tring to run my go project using amazon lambda, this is my current main.go

https://gist.github.com/krakiun/61e4e4dc5ab91f557e481f0230ed3ba0

I tried several methods, but none worked How i can make run this project in lambda, in this moment if i run using router.Run(cfg.HTTP.ListenAddr) is working without any error,

with this log.Fatal(gateway.ListenAndServe(cfg.HTTP.ListenAddr, router)) is die with this error :

expected AWS Lambda environment variables [_LAMBDA_SERVER_PORT AWS_LAMBDA_RUNTIME_API] are not defined
exit status 1

How i can fix my code to run in aws lambda ?



Solution 1:[1]

You have to separate the environment

the below code for run at local

router.Run(cfg.HTTP.ListenAddr)

the lambda can only run on the AWS Lambda Function. You must deploy it to lambda

lambda.Start(router)

You can check the example at https://maxrohde.com/2021/05/01/lambda-go-starter-project/. And the source code is here: https://github.com/mxro/go-lambda-starter-project/tree/main/packages/lambda-go-gin

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Phat Tran