【发布时间】:2016-08-21 00:46:48
【问题描述】:
我想使用无服务器创建一个安全的 APIG,在我当前的“s-fction.json”中我已经有了:
"apiKeyRequired": true,
在我的“s-resources-cf.json”中我已经有了:
"AWSApiKey": {
"Type": "AWS::ApiGateway::ApiKey",
"Properties" : {
"Description" : "ApiKey for secure the connections to the xxx API",
"Enabled" : true
}
}
它正确地创建了所有,一个 Lambda,一个用于该 lambda(包括 CORS)的 APIG 和 API 密钥,但我需要手动将密钥“分配”到生成的 APIG-Stage,你有什么想法可以我使用无服务器自动执行此操作?
我从这里阅读了有关我想要的功能的 AWS 文档(而且似乎是可能的):AWS CloudFormation API Key
文档显示可以通过以下方式完成:
"ApiKey": {
"Type": "AWS::ApiGateway::ApiKey",
"DependsOn": ["TestAPIDeployment", "Test"],
"Properties": {
"Name": "TestApiKey",
"Description": "CloudFormation API Key V1",
"Enabled": "true",
"StageKeys": [{
"RestApiId": { "Ref": "RestApi" },
"StageName": "Test"
}]
}
}
但我不知道如何添加对serverless自动创建的APIG的引用以及如何等待该APIG被创建。
【问题讨论】:
-
研究后的一些附加信息:Serverless 0.5 在 Lambda 函数和 API 网关 API 之前创建 cloudformation 堆栈。因此,提前知道 RestApiId 似乎非常困难(阅读:不可能)。 api key 需要在 API 存在后创建。 Serverless 不使用 Cloudformation 来部署 API 或 Lambda,因此这些不能用作依赖项或引用。在无服务器部署之后,可以通过提供名称、阶段和 api id 作为参数,使用 Cloudformation 创建 api 密钥。一个适当的解决方案会很好。
-
这个问题是指无服务器框架的一个过时和停产的版本。在最新版本中,可以轻松创建 API 密钥,请参阅:serverless.com/framework/docs/providers/aws/events/apigateway/…
标签: amazon-web-services aws-api-gateway serverless-framework