我想在 SAM template.yaml 中为 API Gateway 设置阶段名称。但无论我尝试什么,我都没有成功。无需尝试命名我的阶段,一切都按预期工作,但使用默认阶段名称 Prod 和 Stage。
我的 sam-cli 版本是 0.47.0
我确实在 Stackoverflow 上找到了三个类似的问题,但没有一个答案对我有用。
我总是收到类似这样的错误:
Unresolved resource dependencies [ServerlessRestApi] in the Outputs block of the template
那么我如何获得我自己选择的艺名。我不太关心 Prod 和 Stage 是否与我选择的名称共存。
为了完整起见,我的 template.yaml 文件如下:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
sam-app
Sample SAM Template for sam-app
Globals:
Function:
Timeout: 3
Api:
Cors:
AllowMethods: "'OPTIONS,PUT'"
AllowHeaders: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
AllowOrigin: "'*'"
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello-world/
Handler: app.lambdaHandler
Runtime: nodejs12.x
Events:
HelloWorld:
Type: Api
Properties:
Path: /hello-world
Method: put
Outputs:
HelloWorldApi:
Description: "API Gateway endpoint URL for Prod stage for Hello World function"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/dev/hello-world/"
HelloWorldFunction:
Description: "Hello World Lambda Function ARN"
Value: !GetAtt HelloWorldFunction.Arn
HelloWorldFunctionIamRole:
Description: "Implicit IAM Role created for Hello World function"
Value: !GetAtt HelloWorldFunctionRole.Arn
我可能不明白这背后的预期工作流程。当 API Gateway 指向的 lambda 函数相同时,为什么有 2 个阶段名称?
我将拥有“dev”和“prod”环境,但它们将使用不同的堆栈名称,因此我永远不会混淆不同的环境。
我总是使用 deploy-dev.sh 和 deploy-pod.sh 脚本在实际部署之前检查我是否在开发或主(生产)分支上。因此这些脚本将指向不同的 template.yaml 文件,因为它们是从不同的 git 分支调用的。我已经使用这种方式进行部署很长时间了,它对我来说效果很好。
附带说明:为什么现有的艺名以大写字母开头?它看起来如此丑陋和不寻常。