在SAM模板中定义一个AWS::Serverless::Function时,指定多个API阶段和Lambda别名。

12
我想实现的目标是编写一个SAM模板,以获取由Api Gateway事件触发的Lambda函数。我希望有多个API阶段(例如“dev”,“testing”,“prod”),我希望每个阶段都映射到具有相同名称的Lambda别名。我更喜欢不在每次部署时生成新的lambda版本,并手动设置要由每个lambda别名使用的lambda版本。当然,“dev”别名应指向$LATEST代码版本。
我修改了经典的“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


1
你好, 你是否找到了解决这个问题的方法,而不用另一个工具? - RaKoDev
1
嗨,还没有。将删除策略设置为“保留”可能有效,但我还没有尝试过。 - cionzo
1
我认为没有明显的解决方案很遗憾。到目前为止,我想我只会复制堆栈并将堆栈名称作为参数传递,以选择开发或生产环境。 - Codigo Morsa
1个回答

0

1
谢谢 @ident 的回复。我知道可以使用其他工具来完成它,但我认为在模板中定义所有这些的方法必须存在:这就是我正在寻找的。 - cionzo
1
我认为可以使用普通的Cloudformation来实现您想要的功能,以下链接提供了解决方案:https://stackoverflow.com/a/58725869/6398014 - Codigo Morsa
1
谢谢@CodigoMorsa,您认为将删除策略设置为“保留”会有帮助吗? - cionzo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接