【发布时间】:2021-04-19 19:24:36
【问题描述】:
我的目标 我正在尝试编写一个(单个)SAM 模板来获取由 Api Gateway 事件触发的 Lambda 函数。
我想要拥有多个 API 阶段(例如“dev”、“testing”、“prod”),并且我希望将它们中的每一个都映射到具有相同名称的 Lambda 别名。
我不希望在每次部署时都生成新的 lambda 版本,我喜欢手动设置每个 lambda 别名使用的 lambda 版本。当然,“dev”别名意味着指向 $LATEST 代码版本。
我尝试了什么,我得到了什么
我将经典的“hello_world”模板修改如下。 现在,当第一次部署(比如,到“开发”)时,一切似乎都按预期工作。 但是如果我尝试部署到“测试”阶段,新阶段的 API 会响应,而“开发”阶段的 API 会停止响应。
我错过了什么?
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
zzzz
Sample SAM Template for zzzz
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 3
Parameters:
StageAliasName:
Description: stage / alias name to be used in this deploy
Type: String
AllowedValues:
- prod
- stage
- dev
- v1
Default: dev
Resources:
ApiGatewayApi:
Type: AWS::Serverless::Api
Properties:
StageName: !Ref StageAliasName
HelloWorldFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: hello_world/
Handler: app.lambda_handler
Runtime: python3.8
AutoPublishAlias: !Ref StageAliasName
FunctionName: zzz
Environment:
Variables:
stage: !Ref StageAliasName
Events:
HelloWorld:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
RestApiId: !Ref ApiGatewayApi
Path: /hello
Method: GET
# Auth:
# ApiKeyRequired: true
Outputs:
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
# Find out more about other implicit resources you can reference within SAM
# https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
# HelloWorldApi:
# Description: "API Gateway endpoint URL for Prod stage for Hello World function"
# Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
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
【问题讨论】:
-
您好,您是否找到了无需使用其他工具即可解决此问题的解决方案?
-
嗨,还没有。将删除策略设置为“保留”可能有效,但我还没有尝试过。
-
我觉得很遗憾没有明显的解决方案。到目前为止,我想我只是复制堆栈并将堆栈名称作为参数传递以选择 dev 或 prod。
标签: amazon-web-services aws-lambda aws-serverless aws-sam aws-sam-cli