【问题标题】:AWS SAM Rust - Deploy multiple lambda functions from same crateAWS SAM Rust - 从同一个箱子部署多个 lambda 函数
【发布时间】:2023-02-02 20:10:59
【问题描述】:

我正在尝试部署一个包含两个 Rust lambda 函数的基本无服务器应用程序。我正在使用 SAM 部署应用程序。

问题是如何让 SAM 选择正确的“引导程序”文件。因为这两个函数都构建在相同的CodeUri 路径中,所以 SAM 不会同时执行这两个 Make 命令。相反,它只是将 Function1 的输出复制到 Function2(这似乎是 SAM 中的设计缺陷?)。因此,这两个 lambda 目前都使用相同的代码进行部署。

我的构建目录是

myapp/
- src/
  - bin/
    - function1.rs   (note: function1 & 2 depend on lib.rs)
    - function2.rs
  - lib.rs
- Cargo.toml
- Makefile
- template.yaml

template.yaml 文件:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Globals:
  Function:
    Handler: bootstrap.is.the.handler
    Runtime: provided.al2
    Architectures:
      - x86_64

Resources:
  Function1:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: .
  
  Function2:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: .

生成文件是:

build-Function1:
    cargo lambda build
    cp ./target/lambda/function1/bootstrap $(ARTIFACTS_DIR)

build-Function2: # This never gets run!
    cargo lambda build
    cp ./target/lambda/function2/bootstrap $(ARTIFACTS_DIR)

构建/部署的命令

sam build
sam deploy

我对其他构建结构持开放态度。我也尝试过使用 Rust 工作区构建项目。但是,因为 SAM 将构建源复制到一个单独的目录,所以我找不到添加模块依赖项的方法。

【问题讨论】:

    标签: amazon-web-services rust aws-lambda aws-sam


    【解决方案1】:

    经过一番努力,我想出了一个 hacky 解决方案,我确信这不是推荐的方法。

    1. 使用 Rust 工作区,因此文件夹结构为:
      root/
        common/
          lib.rs
          Cargo.toml
        function1/
          main.rs
          Cargo.toml
          Makefile
        function2/
          main.rs
          Cargo.toml
          Makefile
        Cargo.toml
        template.yaml
      
      1. 设置template.yaml文件使用不同的codeURIs
      AWSTemplateFormatVersion: '2010-09-09'
      Transform: AWS::Serverless-2016-10-31
      Globals:
        Function:
          Handler: bootstrap.is.the.handler
          Runtime: provided.al2
          Architectures:
            - x86_64
      
      Resources:
        Function1:
          Type: AWS::Serverless::Function
          Properties:
            CodeUri: function1
        
        Function2:
          Type: AWS::Serverless::Function
          Properties:
            CodeUri: function2
      
      1. (破解步骤)在每个 Makefile 中,cp 回到源目录,以便它使用所有源构建。 (这增加了在目标之间共享构建缓存的好处)
      build-Function1:
          cd $(PWD); cargo lambda build
          cp $(PWD)/target/lambda/function1/bootstrap $(ARTIFACTS_DIR)
      

      此解决方案与cargo lambda watchsam build/sam deploy 命令兼容。

      二进制文件很大,因为所有 lambda 都复制了基础库。我不确定这是否可以通过 Rust 避免。

      我还没有通过 CI 服务器的部署来试用它。

    【讨论】:

      猜你喜欢
      • 2023-03-09
      • 2019-01-13
      • 2018-09-07
      • 1970-01-01
      • 1970-01-01
      • 2019-04-15
      • 2021-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多