【问题标题】:How can I invoke another lambda function also defined in AWS SAM template?如何调用也在 AWS SAM 模板中定义的另一个 lambda 函数?
【发布时间】:2026-01-05 16:50:01
【问题描述】:

我知道如何调用我已命名且已作为 Lambda 函数存在的 lambda 函数,但我如何让 FunctionA 调用我在 AWS SAM 模板中一起定义的 FunctionB,并且事先不知道名称,也就是动态的。

有没有办法在创建之前将 FunctionB 的名称作为 SAM 模板的一部分传递,以便模板在创建 FunctionA 之前知道 FunctionB 的全名?

我看到很多关于仅在本地测试的问题

【问题讨论】:

  • 您是否尝试过将其 ARN 或名称作为环境变量传递?
  • 你只是在FunctionA中引用它?你能粘贴模板的相关部分吗?不知道是什么问题。
  • @kichik 这就是我需要做的。我没有意识到环境变量路径。谢谢!如果您将其发布为答案,我将标记为答案

标签: amazon-web-services aws-lambda amazon-cloudformation boto3 aws-sam-cli


【解决方案1】:

SAM 不同于 CloudFormation。 SAM 有一个快捷方式。 SAM 资源类型AWS::Serverless::Function 简化了这一点。

在此示例 SAM 模板中,示例资源“CallerFunction”具有:

  1. 调用函数“微服务”的作用域策略
  2. 具有函数名称的环境变量
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  A SAM template where lambda function caller invokes lambda function microservice.

Resources:
  CallerFunction:
    Type: AWS::Serverless::Function 
    Properties:
      Description: 'A lambda that invokes the function microservice'
      CodeUri: caller/
      Handler: app.handler
      Runtime: nodejs10.x
      Policies: 
        - LambdaInvokePolicy:
            FunctionName:
              !Ref MicroserviceFunction
      Environment:
        Variables:
          MICROSERVICE_FUNCTION: !Ref MicroserviceFunction
  MicroserviceFunction:
    Type: AWS::Serverless::Function 
    Properties:
      Description: 'A microservice lambda'
      CodeUri: microservice/
      Handler: index.handler
      Runtime: nodejs10.x
      Policies: CloudWatchLogsFullAccess

享受无服务器的乐趣!

【讨论】:

    【解决方案2】:

    您可以将其他函数的名称或 ARN 作为环境变量传递。例如:

    Resources:
      FunctionA:
        Type: AWS::Lambda::Function
        Properties:
          Handler: index.handler
          Runtime: python3.6
          Role: !Sub ${FunctionRole.Arn}
          Environment:
            Variables:
              # pass FunctionB ARN as environment variable
              FUNCTION_B_ARN: !Sub ${FunctionB.Arn}
          Code:
            ZipFile: |
              import os
              def handler(event, context):
                # use environment variable
                print(os.getenv("FUNCTION_B_ARN"))
      FunctionB:
        Type: AWS::Lambda::Function
        Properties:
          Handler: index.handler
          Runtime: python3.6
          Role: !Sub ${FunctionRole.Arn}
          Code:
            ZipFile: |
              def handler(event, context):
                print("hello world")
      FunctionRole:
        Type: AWS::IAM::Role
        Properties:
          AssumeRolePolicyDocument:
            Statement:
              - Action:
                  - sts:AssumeRole
                Effect: Allow
                Principal:
                  Service:
                    - lambda.amazonaws.com
            Version: '2012-10-17'
          ManagedPolicyArns:
            - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
    

    【讨论】:

    • +1。作为旁注,您还可以在 FunctionB 代码块中引用它:${FunctionB.Arn} 当您在 ZipFile 中使用 !Sub | 时。
    • 我使用 !GetAtt 而不是 !Sub - 不确定是否更高效
    最近更新 更多