我正在GO中构建一个AWS Lambda函数,该函数应该通过API Gateway端点触发.我面临的问题是,通过POST请求调用端点会导致502错误.我把我的项目建立在YouTube tutorial的基础上,但似乎有些不对劲.
为了以编程方式编译GO函数,我使用一个Makefile在每个函数的目录中生成一个main
可执行文件.以下是我的Makefile
条中的build
条:
build:
@echo "Building functions..."
@$(foreach func, $(FUNCTIONS), \
go build -o $(FUNCTIONS_DIR)/$(func)/$(BINARY_NAME) $(FUNCTIONS_DIR)/$(func)/main.go && \
echo "Built $(func)/$(BINARY_NAME)"; \
)
# Deploy stack with AWS CDK
.PHONY: deploy
deploy:
echo "Deploying stack..."
cdk deploy --profile $(AWS_PROFILE)
我使用命令make deploy
部署我的堆栈.
我的love-go-serverless-stack.ts
文件的相关部分看起来像这样:
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import { RestApi, LambdaIntegration } from 'aws-cdk-lib/aws-apigateway';
export class LoveGoServerlessStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// Hello Lambda function
const helloFunction = new lambda.Function(this, 'HelloFunction', {
code: lambda.Code.fromAsset('src/functions/hello'),
handler: 'main',
runtime: lambda.Runtime.PROVIDED_AL2023,
});
// The API Gateway
const gateway = new RestApi(this, 'MyGateway', {
defaultCorsPreflightOptions: {
allowOrigins: ['*'],
allowMethods: ['GET', 'POST'],
},
});
// The Lambda integration
const integration = new LambdaIntegration(helloFunction);
// Creating the '/hello' resource
const helloResource = gateway.root.addResource('hello');
helloResource.addMethod('POST', integration); // POST method for the 'hello' resource
}
}
当我向API Gateway提供的CloudFront URL发出POST请求时,收到502错误.CloudWatch日志(log)指示与Lambda函数的入口点相关的反复出现的错误:
RequestId: xxxxxxx-xxxx-xxxx-xxxx-xxxxxxx Error: Couldn't find valid bootstrap(s): [/var/task/bootstrap /opt/bootstrap]
Runtime.InvalidEntrypoint
以及:
INIT_REPORT Init Duration: 0.27 ms Phase: invoke Status: error Error Type: Runtime.InvalidEntrypoint
我对"invalid entrypoint"错误感到困惑,因为我已经在CDK堆栈(love-go-serverless-stack.ts
)中配置了/hello
路由.这可能与Go二进制的命名或包装方式有关吗?任何见解或建议,可能是什么导致这一问题和如何解决它将不胜感激.
My environment:
- Windows 11
- 1.21.5
- node 18.18.2
更新
- 在切换到Mac并修复了Makefile中的缩进后,我能够运行
make build
.我仍然无法在Windows 11上运行我的代码.