【问题标题】:sam package is reducing the size of my templatesam 包正在减小我的模板的大小
【发布时间】:2021-02-09 00:09:56
【问题描述】:

我有一个 SAM 模板,用于构建与 API 网关集成的 4 个 lambda 函数。

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: An AWS Serverless Specification template describing your function.

#To avoide 'stage' being created when deploying the Api gateway.
Globals:
  Api:
    OpenApiVersion: 3.0.1


Resources:
  # api gateway model for all user methods 
  ApiGatewayApi:
    Type: AWS::Serverless::Api
    Properties:
      Name: loadeo_user
      StageName: Development
      Cors: "'*'"

  # integrating lambda with api for loadeo_get_all_user function    
  GetAllUser:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: loadeo_get_all_user
      CodeUri: getAllUser/
      Handler: get_all_user.lambda_handler
      Timeout: 5
      Runtime: python3.8
      Role: loadeo_lambda_execution
      MemorySize: 128
      Events:
        GetAllUser:
          Type: Api
          Properties:
            Path: /get-all-user
            Method: get
            RestApiId:
              Ref: ApiGatewayApi

  # integrating lambda with api for loadeo_get_user_profile function             
  GetUserProfile:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: loadeo_get_user_profile
      CodeUri: getUserProfile/
      Handler: get_user_profile.lambda_handler
      Timeout: 5
      Runtime: python3.8
      Role: loadeo_lambda_execution
      MemorySize: 128
      Events:
        GetAllUser:
          Type: Api
          Properties:
            Path: /get-user-profile
            Method: get
            RestApiId:
              Ref: ApiGatewayApi
#  integrating lambda with api for loadeo_update_user_profile function   
  GetUserProfile:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: loadeo_update_user_profile
      CodeUri: updateUserProfile/
      Handler: update_user_profile.lambda_handler
      Timeout: 5
      Runtime: python3.8
      Role: loadeo_lambda_execution
      MemorySize: 128
      Events:
        GetAllUser:
          Type: Api
          Properties:
            Path: /update_user_profile-user
            Method: patch
            RestApiId:
              Ref: ApiGatewayApi 


#  integrating lambda with api for loadeo_create_user function
  GetUserProfile:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: loadeo_create_user
      CodeUri: createUser/
      Handler: create_all_user.lambda_handler
      Timeout: 5
      Runtime: python3.8
      Role: loadeo_lambda_execution
      MemorySize: 128
      Events:
        GetAllUser:
          Type: Api
          Properties:
            Path: /create-user
            Method: put
            RestApiId:
              Ref: ApiGatewayApi             
              

在此之后的下一步是打包和部署代码。

我正在使用aws cloudformation package,因为它类似于sam package,并且我都尝试过。

只要我运行命令,模板文件就会减少。 从 100 行到 50 行。当我观察到中间的两个函数被省略时。

不知道这是什么原因。函数的数量有限制吗?还是我遗漏了什么?

【问题讨论】:

    标签: amazon-web-services aws-lambda aws-api-gateway aws-sam


    【解决方案1】:

    两个函数被省略了。

    函数被跳过,因为它们有同名 GetUserProfile。因此,当您拥有其中三个时,一个会覆盖另一个。您必须重命名它们:

    AWSTemplateFormatVersion: '2010-09-09'
    Transform: AWS::Serverless-2016-10-31
    Description: An AWS Serverless Specification template describing your function.
    
    #To avoide 'stage' being created when deploying the Api gateway.
    Globals:
      Api:
        OpenApiVersion: 3.0.1
    
    
    Resources:
      # api gateway model for all user methods 
      ApiGatewayApi:
        Type: AWS::Serverless::Api
        Properties:
          Name: loadeo_user
          StageName: Development
          Cors: "'*'"
    
      # integrating lambda with api for loadeo_get_all_user function    
      GetAllUser:
        Type: AWS::Serverless::Function
        Properties:
          FunctionName: loadeo_get_all_user
          CodeUri: getAllUser/
          Handler: get_all_user.lambda_handler
          Timeout: 5
          Runtime: python3.8
          Role: loadeo_lambda_execution
          MemorySize: 128
          Events:
            GetAllUser:
              Type: Api
              Properties:
                Path: /get-all-user
                Method: get
                RestApiId:
                  Ref: ApiGatewayApi
    
      # integrating lambda with api for loadeo_get_user_profile function             
      GetUserProfile:
        Type: AWS::Serverless::Function
        Properties:
          FunctionName: loadeo_get_user_profile
          CodeUri: getUserProfile/
          Handler: get_user_profile.lambda_handler
          Timeout: 5
          Runtime: python3.8
          Role: loadeo_lambda_execution
          MemorySize: 128
          Events:
            GetAllUser:
              Type: Api
              Properties:
                Path: /get-user-profile
                Method: get
                RestApiId:
                  Ref: ApiGatewayApi
    #  integrating lambda with api for loadeo_update_user_profile function   
      UpdateUserProfile:
        Type: AWS::Serverless::Function
        Properties:
          FunctionName: loadeo_update_user_profile
          CodeUri: updateUserProfile/
          Handler: update_user_profile.lambda_handler
          Timeout: 5
          Runtime: python3.8
          Role: loadeo_lambda_execution
          MemorySize: 128
          Events:
            GetAllUser:
              Type: Api
              Properties:
                Path: /update_user_profile-user
                Method: patch
                RestApiId:
                  Ref: ApiGatewayApi 
    
    
    #  integrating lambda with api for loadeo_create_user function
      CreateUser:
        Type: AWS::Serverless::Function
        Properties:
          FunctionName: loadeo_create_user
          CodeUri: createUser/
          Handler: create_all_user.lambda_handler
          Timeout: 5
          Runtime: python3.8
          Role: loadeo_lambda_execution
          MemorySize: 128
          Events:
            GetAllUser:
              Type: Api
              Properties:
                Path: /create-user
                Method: put
                RestApiId:
                  Ref: ApiGatewayApi 
    

    【讨论】:

    • 但他们是 get_user_profile 和 update_user_profile
    • @sumanthshetty 我的意思是资源名称,而不是函数名称。在您的原始代码中,SAM 中的资源名称是相同的GetUserProfile。你有三次GetUserProfile
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-01
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多