AWS SAM 启动本地 api returns "Function name is required" 错误
AWS SAM starting local api returns "Function name is required" error
我们正在使用 CDK 来构建我们的基础架构配置。此外,如果有帮助,我会使用 cdk synth <stack_name> --no-staging > template.yml
为 SAM 创建 template.yml
。我正在使用 AWS Toolkit invoke/debug 我在 Intellij 上的 lambda 函数,它工作正常。但是,如果我 运行 sam local start-api
在终端上向我的一个函数发送请求,那么它 returns 堆栈跟踪错误;
Traceback (most recent call last):
File "/usr/local/Cellar/aws-sam-cli/0.53.0/libexec/lib/python3.7/site-packages/flask/app.py", line 2317, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/Cellar/aws-sam-cli/0.53.0/libexec/lib/python3.7/site-packages/flask/app.py", line 1840, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/Cellar/aws-sam-cli/0.53.0/libexec/lib/python3.7/site-packages/flask/app.py", line 1743, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/Cellar/aws-sam-cli/0.53.0/libexec/lib/python3.7/site-packages/flask/_compat.py", line 36, in reraise
raise value
File "/usr/local/Cellar/aws-sam-cli/0.53.0/libexec/lib/python3.7/site-packages/flask/app.py", line 1838, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/Cellar/aws-sam-cli/0.53.0/libexec/lib/python3.7/site-packages/flask/app.py", line 1824, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/local/Cellar/aws-sam-cli/0.53.0/libexec/lib/python3.7/site-packages/samcli/local/apigw/local_apigw_service.py", line 203, in _request_handler
self.lambda_runner.invoke(route.function_name, event, stdout=stdout_stream_writer, stderr=self.stderr)
File "/usr/local/Cellar/aws-sam-cli/0.53.0/libexec/lib/python3.7/site-packages/samcli/commands/local/lib/local_lambda.py", line 84, in invoke
function = self.provider.get(function_name)
File "/usr/local/Cellar/aws-sam-cli/0.53.0/libexec/lib/python3.7/site-packages/samcli/lib/providers/sam_function_provider.py", line 65, in get
raise ValueError("Function name is required")
ValueError: Function name is required
这是我的命令 运行
sam local start-api --env-vars env.json --docker-network test
给出输出
Mounting None at http://127.0.0.1:3000/v1 [GET, OPTIONS, POST]
Mounting None at http://127.0.0.1:3000/v1/user [GET, OPTIONS, POST]
You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. You only need to restart SAM CLI if you update your AWS SAM template
2020-08-22 16:32:46 * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)
2020-08-22 16:33:03 Exception on /v1/user [OPTIONS]
这里是 env.json 我用作函数的环境变量
{
"tenantGetV1Function54F63CB9": {
"db": "alpha",
"connectionString": "mongodb://mongo"
},
"tenantPostV1FunctionA56822D0": {
"db": "alpha",
"connectionString": "mongodb://mongo"
},
"userGetV1Function7E6E55C2": {
"db": "alpha",
"connectionString": "mongodb://mongo"
},
"userPostV1FunctionEB035EB0": {
"db": "alpha",
"connectionString": "mongodb://mongo"
}
}
我也在 运行ning Docker macOS 操作系统上的桌面。
编辑:在这里您可以找到简化的 template.yml,只有一个端点(一个函数定义)用于 tenantGetV1Function54F63CB9
函数。它将映射到 GET /v1 端点。我不希望包括 4 个函数的整个模板,这使大约一千行 .yml 代码。
https://gist.github.com/flexelem/d887136484d508e313e0a745c30a2d97
如果我通过在 CDK 中传递 Function
实例而不是它的 Alias
实例来创建 LambdaIntegration
,问题就解决了。因此,我们正在创建带有别名的 lambda。然后,我们将别名从 Api 网关传递给它们关联的 Resource
实例。
这就是创作的方式;
Function tenantGetV1Function = Function.Builder.create(this, "tenantGetV1Function")
.role(roleLambda)
.runtime(Runtime.JAVA_8)
.code(lambdaCode)
.handler("com.yolda.tenant.lambda.GetTenantHandler::handleRequest")
.memorySize(512)
.timeout(Duration.minutes(1))
.environment(environment)
.description(Instant.now().toString())
.build();
Alias tenantGetV1Alias = Alias.Builder.create(this, "tenantGetV1Alias")
.aliasName("live")
.version(tenantAdminGetV1Function.getCurrentVersion())
.provisionedConcurrentExecutions(provisionedConcurrency)
.build();
Resource v1Resource = v1Resource.addResource("{tenantId}");
v1Resource.addMethod("GET", LambdaIntegration.Builder.create(tenantGetV1Alias).build(), options);
如果我将 tenantGetV1Alias
替换为 tenantGetV1Function
,则 sam build
命令会成功构建所有函数,这些函数将使 sam local start-api
启动它们。
Resource v1Resource = v1Resource.addResource("{tenantId}");
v1Resource.addMethod("GET", LambdaIntegration.Builder.create(tenantGetV1Function).build(), options);
如果我们分配别名,SAM 无法从 CloudFormation 模板中获取函数名称 属性。
我们正在使用 CDK 来构建我们的基础架构配置。此外,如果有帮助,我会使用 cdk synth <stack_name> --no-staging > template.yml
为 SAM 创建 template.yml
。我正在使用 AWS Toolkit invoke/debug 我在 Intellij 上的 lambda 函数,它工作正常。但是,如果我 运行 sam local start-api
在终端上向我的一个函数发送请求,那么它 returns 堆栈跟踪错误;
Traceback (most recent call last):
File "/usr/local/Cellar/aws-sam-cli/0.53.0/libexec/lib/python3.7/site-packages/flask/app.py", line 2317, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/Cellar/aws-sam-cli/0.53.0/libexec/lib/python3.7/site-packages/flask/app.py", line 1840, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/Cellar/aws-sam-cli/0.53.0/libexec/lib/python3.7/site-packages/flask/app.py", line 1743, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/Cellar/aws-sam-cli/0.53.0/libexec/lib/python3.7/site-packages/flask/_compat.py", line 36, in reraise
raise value
File "/usr/local/Cellar/aws-sam-cli/0.53.0/libexec/lib/python3.7/site-packages/flask/app.py", line 1838, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/Cellar/aws-sam-cli/0.53.0/libexec/lib/python3.7/site-packages/flask/app.py", line 1824, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/local/Cellar/aws-sam-cli/0.53.0/libexec/lib/python3.7/site-packages/samcli/local/apigw/local_apigw_service.py", line 203, in _request_handler
self.lambda_runner.invoke(route.function_name, event, stdout=stdout_stream_writer, stderr=self.stderr)
File "/usr/local/Cellar/aws-sam-cli/0.53.0/libexec/lib/python3.7/site-packages/samcli/commands/local/lib/local_lambda.py", line 84, in invoke
function = self.provider.get(function_name)
File "/usr/local/Cellar/aws-sam-cli/0.53.0/libexec/lib/python3.7/site-packages/samcli/lib/providers/sam_function_provider.py", line 65, in get
raise ValueError("Function name is required")
ValueError: Function name is required
这是我的命令 运行
sam local start-api --env-vars env.json --docker-network test
给出输出
Mounting None at http://127.0.0.1:3000/v1 [GET, OPTIONS, POST]
Mounting None at http://127.0.0.1:3000/v1/user [GET, OPTIONS, POST]
You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. You only need to restart SAM CLI if you update your AWS SAM template
2020-08-22 16:32:46 * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)
2020-08-22 16:33:03 Exception on /v1/user [OPTIONS]
这里是 env.json 我用作函数的环境变量
{
"tenantGetV1Function54F63CB9": {
"db": "alpha",
"connectionString": "mongodb://mongo"
},
"tenantPostV1FunctionA56822D0": {
"db": "alpha",
"connectionString": "mongodb://mongo"
},
"userGetV1Function7E6E55C2": {
"db": "alpha",
"connectionString": "mongodb://mongo"
},
"userPostV1FunctionEB035EB0": {
"db": "alpha",
"connectionString": "mongodb://mongo"
}
}
我也在 运行ning Docker macOS 操作系统上的桌面。
编辑:在这里您可以找到简化的 template.yml,只有一个端点(一个函数定义)用于 tenantGetV1Function54F63CB9
函数。它将映射到 GET /v1 端点。我不希望包括 4 个函数的整个模板,这使大约一千行 .yml 代码。
https://gist.github.com/flexelem/d887136484d508e313e0a745c30a2d97
如果我通过在 CDK 中传递 Function
实例而不是它的 Alias
实例来创建 LambdaIntegration
,问题就解决了。因此,我们正在创建带有别名的 lambda。然后,我们将别名从 Api 网关传递给它们关联的 Resource
实例。
这就是创作的方式;
Function tenantGetV1Function = Function.Builder.create(this, "tenantGetV1Function")
.role(roleLambda)
.runtime(Runtime.JAVA_8)
.code(lambdaCode)
.handler("com.yolda.tenant.lambda.GetTenantHandler::handleRequest")
.memorySize(512)
.timeout(Duration.minutes(1))
.environment(environment)
.description(Instant.now().toString())
.build();
Alias tenantGetV1Alias = Alias.Builder.create(this, "tenantGetV1Alias")
.aliasName("live")
.version(tenantAdminGetV1Function.getCurrentVersion())
.provisionedConcurrentExecutions(provisionedConcurrency)
.build();
Resource v1Resource = v1Resource.addResource("{tenantId}");
v1Resource.addMethod("GET", LambdaIntegration.Builder.create(tenantGetV1Alias).build(), options);
如果我将 tenantGetV1Alias
替换为 tenantGetV1Function
,则 sam build
命令会成功构建所有函数,这些函数将使 sam local start-api
启动它们。
Resource v1Resource = v1Resource.addResource("{tenantId}");
v1Resource.addMethod("GET", LambdaIntegration.Builder.create(tenantGetV1Function).build(), options);
如果我们分配别名,SAM 无法从 CloudFormation 模板中获取函数名称 属性。