template.yml如果有帮助。我正在-6ren">
gpt4 book ai didi

amazon-web-services - AWS SAM 启动本地 api 返回 "Function name is required"错误

转载 作者:行者123 更新时间:2023-12-04 09:02:21 28 4
gpt4 key购买 nike

我们正在使用 CDK 来构建我们的基础设施配置。此外,我创建了我的 template.yml SAM 用 cdk synth <stack_name> --no-staging > template.yml如果有帮助。我正在使用 AWS Toolkit 在 Intellij 上调用/调试我的 lambda 函数,它工作正常。但是,如果我运行 sam local start-api在终端上并向我的一个函数发送请求,然后它返回一个带有堆栈跟踪的错误;

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"
}
}

我也在 macOS 操作系统上运行 Docker Desktop。
编辑:在这里您可以找到只有一个端点(一个函数定义)的简化 template.yml,用于 tenantGetV1Function54F63CB9功能。它将映射到 GET/v1 端点。我不想包含 4 个函数的整个模板,这些模板大约有 1000 行 .yml 代码。
https://gist.github.com/flexelem/d887136484d508e313e0a745c30a2d97

最佳答案

如果我创建 LambdaIntegration 问题解决了通过 Function实例而不是它的 Alias CDK 中的实例。因此,我们正在创建 lambda 和别名。然后,我们将别名传递给它们关联的 Resource来自 Api 网关的实例。
这就是创造的方式;

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);
如果我替换 tenantGetV1AliastenantGetV1Function然后 sam build命令成功构建了所有函数,这将使 sam local start-api旋转它们。
Resource v1Resource = v1Resource.addResource("{tenantId}");
v1Resource.addMethod("GET", LambdaIntegration.Builder.create(tenantGetV1Function).build(), options);
不知何故,如果我们分配别名,SAM 无法从 CloudFormation 模板获取函数名称属性。

关于amazon-web-services - AWS SAM 启动本地 api 返回 "Function name is required"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63536861/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com