![](/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.