Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

go

aws-lambda

I am trying to create a simple AWS Lambda function with GoLang that performs that performs an HTTP API request (GET) (API Gateway). I have the following functions and code snippets

in my main.go file these functions exist

package main

import (
    "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) {
    log.Printf("hello world")
    response := events.APIGatewayProxyResponse{
        StatusCode: 200,
    Body: "hello world",
    }
    return response, nil
}

type User struct {
    Username string `json:"username"`
    Password string `json:"password"`
}

type ResponseBody struct {
    Message string `json:"username"`
}

With those methods in my **main.go ** file, I created an HTTP API on AWS and upload a built zipped version of my main.go source code after which I obtained the endpoint as well as the route but after testing it on Postman, I got the following response

{
    "message": "Internal Server Error"
}

I double cross-cheked my endpoint and route but got no other response different from that above. I tried too the thunder client postman alternative extension on vscode but got the same response (505)

However, I decided to run my main.go source code using go run main.golocally on my terminal and I got the following error message

2023/04/04 15:08:55 expected AWS Lambda environment variables [_LAMBDA_SERVER_PORT AWS_LAMBDA_RUNTIME_API] are not defined exit status 1

I am missing something here? Please what to do? I am stuck.

I am using Ubuntu by the way (latest of date)

like image 288
Abdou Rahim Avatar asked Jul 31 '26 20:07

Abdou Rahim


1 Answers

It seems that you are having issues both setting up your lambda & api gateway on AWS, and running the code locally. I have setup a git repository where I have tested your code successfully both locally and remotely that you can view here for more details

Let me start by addressing the second one, as, from the title of the question, seems to be the most pressing.

Part 1 - running locally

To run locally a lambda function you have a few alternatives, such as using building a docker container, using AWS SAM CLI, or downloading AWS runtime environment locally and use it to execute. Since the last option is, in my opinion the easiest and least bureaucratic, here is how you can download locally the AWS lambda runtime environment as described in AWS documentation

mkdir -p ~/.aws-lambda-rie && curl -Lo ~/.aws-lambda-rie/aws-lambda-rie \
https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie \
&& chmod +x ~/.aws-lambda-rie/aws-lambda-rie               

(for install instructions by platform see the official repo readme)

To test the lambda function locally run you can then run on a terminal session:

~/.aws-lambda-rie/aws-lambda-rie go run main.go

This will start a server listening on port 8080. To actually trigger the lambda function, you can finally run on another terminal session:

curl -XPOST "http://localhost:8080/2015-03-31/functions/function/invocations" -d '{"Name": "World"}'

You will see that your code works well.

Part 2 - testing in AWS

The first thing that could get you in trouble and make the API Gateway return a 500 would be if your lambda function did not return a status code & a body in the APIGatewayProxyResponse struct. This is not the case of your code. Which means most likely that you have made a mistake configuring the lambda function or the gateway. I've added in the before mentioned sample repo cdk code that deploys your code to AWS with CDK, with all the basic resources and permissions configured. I've tested your lambda code also deployed on AWS and it worked out without any issues; which probably means you made some configuration mistake. My suggestion would be that you can use the code in the repo to deploy things, and then you can compare with what you had configured before to troubleshoot what mistake was done. Hope it helps, good luck!

like image 175
diogoa Avatar answered Aug 02 '26 12:08

diogoa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!