我想实现的目标是编写一个SAM模板,以获取由Api Gateway事件触发的Lambda函数。我希望有多个API阶段(例如“dev”,“testing”,“prod”),我希望每个阶段都映射到具有相同名称的Lambda别名。我更喜欢不在每次部署时生成新的lambda版本,并手动设置要由每个lambda别名使用的lambda版本。当然,“dev”别名应指向$LATEST代码版本。
我修改了经典的“hello_world”模板如下。现在,当第一次部署(比如到“dev”)时,一切似乎按预期工作。但是,如果我尝试部署到“测试”阶段,则此新阶段上的API会响应,而“dev”阶段上的API会停止响应。
我漏掉了什么?
我修改了经典的“hello_world”模板如下。现在,当第一次部署(比如到“dev”)时,一切似乎按预期工作。但是,如果我尝试部署到“测试”阶段,则此新阶段上的API会响应,而“dev”阶段上的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