将 API 网关与 SAM 模板集成以具有自定义名称和阶段

Integrating API gateway with SAM template to have custom name and stage

我正在尝试为通过 SAM 模板创建的 API 网关指定一个客户名称。 并且还限制在部署时只创建一个阶段。

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: An AWS Serverless Specification template describing your function.
Resources:
  ApiGatewayApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: Development
      Cors: "'*'"
  GetAllUser:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: loadeo_get_all_user
      CodeUri: code/
      Handler: get_all_user.lambda_handler
      Timeout: 5
      Runtime: python3.8
      Role: lambda_execution
      MemorySize: 128
      Events:
        GetAllUser:
          Type: Api
          Properties:
            Path: /get-all-user
            Method: get
            RestApiId:
              Ref: ApiGatewayApi

一切如我所愿,但

  1. 它正在用堆栈的名称创建 API(我想给一个自定义名称)
  2. 除了“开发”之外,它还在部署时添加了“阶段”。

如何实现这两种情况?

要为您的 ApiGatewayApi 指定 Name,您必须使用 Name 属性:

A name for the API Gateway RestApi resource

因此您的 ApiGatewayApi 将是:

  ApiGatewayApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: Development
      Name: MyApiName
      Cors: "'*'"

我不确定我是否理解第二个问题,因此现在无法对此发表评论。

here 所述,您可以添加以下内容来修复 Stage 问题:

Globals:
  Api:
    OpenApiVersion: 3.0.1