英文:
expected AWS Lambda environment variables [_LAMBDA_SERVER_PORT AWS_LAMBDA_RUNTIME_API] are not defined
问题
我正在尝试使用GoLang创建一个简单的AWS Lambda函数,该函数执行HTTP API请求(GET)(API Gateway)。我在main.go文件中有以下函数和代码片段:
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"`
}
使用这些方法在main.go文件中,我在AWS上创建了一个HTTP API,并上传了一个已构建的main.go源代码的压缩版本,然后我获得了端点和路由,但在Postman上测试后,我得到了以下响应:
{
"message": "Internal Server Error"
}
我仔细检查了我的端点和路由,但没有得到与上述不同的其他响应。我还尝试了在vscode上使用Thunder Client Postman替代扩展,但得到了相同的响应(505)。
然而,我决定在我的终端上使用go run main.go
命令运行我的main.go源代码,然后我得到了以下错误消息:
2023/04/04 15:08:55 expected AWS Lambda environment variables [_LAMBDA_SERVER_PORT AWS_LAMBDA_RUNTIME_API] are not defined exit status 1
我在这里漏掉了什么?请问该怎么办?我被卡住了。
顺便说一下,我正在使用Ubuntu(最新日期)。
英文:
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.go
locally 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)
答案1
得分: 1
看起来你在设置AWS上的lambda和api gateway时遇到了问题,并且在本地运行代码也有问题。我已经在这个git仓库中成功地测试了你的代码,无论是在本地还是远程上。
让我首先解决第二个问题,因为从问题的标题来看,这似乎是最紧迫的。
第一部分 - 本地运行
要在本地运行lambda函数,你有几种选择,比如使用构建docker容器,使用AWS SAM CLI,或者在本地下载AWS运行环境并使用它来执行。由于最后一种选项在我看来是最简单和最不繁琐的,下面是你可以按照AWS文档中描述的方式在本地下载AWS lambda运行环境的方法:
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
要在本地测试lambda函数,你可以在终端会话中运行以下命令:
~/.aws-lambda-rie/aws-lambda-rie go run main.go
这将启动一个监听端口8080的服务器。要实际触发lambda函数,你可以在另一个终端会话中运行以下命令:
curl -XPOST "http://localhost:8080/2015-03-31/functions/function/invocations" -d '{"Name": "World"}'
你会看到你的代码正常工作。
第二部分 - 在AWS上测试
导致API Gateway返回500错误的第一件事可能是你的lambda函数在APIGatewayProxyResponse
结构中没有返回状态码和响应体。但你的代码并不是这种情况。这很可能意味着你在配置lambda函数或网关时犯了一个错误。
我在之前提到的示例仓库CDK代码中添加了部署你的代码到AWS的代码,其中配置了所有基本资源和权限。我还测试了你的lambda代码在AWS上的部署,没有任何问题;这可能意味着你做了一些配置错误。
我的建议是你可以使用仓库中的代码来部署,然后与之前配置的内容进行比较,以排查错误。希望能帮到你,祝好运!
英文:
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
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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论