如何在 SAM 模板中添加自定义名称 Lambda 函数、API 网关和阶段名称

sum*_*tty 5 amazon-web-services aws-lambda aws-api-gateway aws-sam

我正在尝试 SAM 模板。在这里,我试图弄清楚如何在模板中添加自定义名称。当我打包并部署模板时,它会创建带有添加的字母数字值的 lambda 函数。API网关名称将是堆栈名称,它将部署在API网关中的“Prod”和“Stage”阶段。

有没有办法拥有自己的自定义名称。

以下是 SAM 模板的示例代码:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: An AWS Serverless Specification template describing your function.
Resources:
  GetAllUser:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: code/
      Handler: getAllUser.lambda_handler
      Timeout: 5
      Runtime: python3.8
      Role: lambda_execution
      MemorySize: 128
      Events:
        GetAllUser:
          Type: Api
          Properties:
            Path: /get-all-user
            Method: get
            
Run Code Online (Sandbox Code Playgroud)

有人可以帮我解决这个问题吗?

sam*_*ler 5

要为 lambda 创建名称,您可以直接指定AWS::Serverless::Function

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: An AWS Serverless Specification template describing your function.
Resources:
  GetAllUser:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: "MyFunctionName"
      CodeUri: code/
      Handler: getAllUser.lambda_handler
      Timeout: 5
      Runtime: python3.8
      Role: lambda_execution
      MemorySize: 128
      Events:
        GetAllUser:
          Type: Api
          Properties:
            Path: /get-all-user
            Method: get
Run Code Online (Sandbox Code Playgroud)

至于其他名称,例如StageNameApiGateway Name,您需要使用 AWS::Serverless::Api

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: AWS SAM template with a simple API definition
Resources:
  ApiGatewayApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: prod
      Name: myapi
  ApiFunction: # Adds a GET api endpoint at "/" to the ApiGatewayApi via an Api event
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: myfunction
      Events:
        ApiEvent:
          Type: Api
          Properties:
            Path: /
            Method: get
            RestApiId:
              Ref: ApiGatewayApi
      Runtime: python3.7
      Handler: index.handler
      InlineCode: |
        def handler(event, context):
            return {'body': 'Hello World!', 'statusCode': 200}
Run Code Online (Sandbox Code Playgroud)