![](/img/trans.png)
[英]AWS CloudFormation/API Gateway gives 'Invalid Resource identifier specified'
[英]How to fix Serverless error "Invalid API Key identifier specified" when using 2 stages with api keys on AWS?
我正在使用以下配置将几个prod
函数部署到 AWS 上不同的生产和dev
阶段。 这两个阶段都应使用存储在 SSM 中的 api 密钥进行保护。
无服务器.yml
service: my-service
frameworkVersion: "3"
provider:
name: aws
runtime: nodejs16.x
region: eu-central-1
apiGateway:
apiKeys:
- name: my-apikey
value: ${ssm:my-apikey}
functions:
v1_myfunc:
handler: src/api/myfunc/get_func.get
events:
- http:
path: /v1/myfunc
method: get
private: true
plugins:
- serverless-esbuild
- serverless-offline
- serverless-dotenv-plugin
我的部署脚本如下所示:
package.json
"scripts": {
"deploy:dev": "serverless deploy --stage dev",
"deploy:prod": "serverless deploy --stage prod"
}
问题:
当我部署其中一个阶段时,一切正常。 但是如果我之后部署另一个,我总是会收到以下错误(在这种情况下,我先部署了 prod,然后部署了 dev):
Deploying my-service to stage dev (eu-central-1)
✖ Stack my-service-dev failed to deploy (46s)
Environment: darwin, node 16.15.0, framework 3.23.0, plugin 6.2.2, SDK 4.3.2
Credentials: Local, "default" profile
Error:
Invalid API Key identifier specified
error Command failed with exit code 1.
查看 AWS 控制台,我注意到生成的 api 密钥对于两个堆栈(开发和生产)具有相同的 ID 。 所以,我猜这就是问题所在:两个堆栈共享相同的 api 密钥实例。
因此,我尝试通过为每个阶段设置不同的 api 键名来解决此问题:
- name: my-apikey-${self:provider.stage}
value: ${ssm:my-apikey}
但这并不能解决问题,因为我仍然收到此错误:
Invalid API Key identifier specified
问题:如何更改我的serverless.yml
配置以解决问题?
我认为这里发生的事情是 your.serverless 文件夹包含一些 output,它在阶段之间被重用并且正在破坏你的部署,通常你不会部署两者(所以你会为分支准备阶段,然后将 master 部署到 prod)。
我想要证明删除 the.serverless 文件夹会进行第二次部署吗?
试试这个,好吗? 这有点猜测,因为我今天正在旅行,所以我在手机上无法测试。 基本上它的作用是根据您指定的阶段进行动态配置,我认为这应该会触发 the.serverless 文件夹中输出的 cloudformation 的变化。 (请检查 APIG 部分的缩进,因为您无法在移动设备上使用 Tab 键)
service: my-service
frameworkVersion: "3"
provider:
name: aws
runtime: nodejs16.x
region: eu-central-1
apiGateway:
apiKeys:
${self:custom.apiTest.${sls:stage}}
functions:
v1_myfunc:
handler: src/api/myfunc/get_func.get
events:
- http:
path: /v1/myfunc
method: get
private: true
plugins:
- serverless-esbuild
- serverless-offline
- serverless-dotenv-plugin
custom:
apiTest:
dev:
- name: // the name for dev
value: // the value for dev
prod:
- name: // the name for prod
value: // the value for prod
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.