Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

600
Visualizações
How can I invoke another lambda function also defined in AWS SAM template?

I know how to invoke lambda functions that I've named and already exist as Lambda Functions but how can I have a FunctionA invoke FunctionB that I'm defining in AWS SAM template together, and won't know the name beforehand, aka dynamically.

Is there a way to pass the name of FunctionB as part of the SAM template before it gets created so the template would know the name of the full name of FunctionB, before creating FunctionA?

I see a lot of questions about testing this locally only

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

SAM is different than CloudFormation. SAM has a shortcut. The SAM resource type AWS::Serverless::Function simplifies this.

In this example SAM template, the example resource 'CallerFunction' has:

  1. A scoped policy to invoke the function 'microservice'
  2. An environment variable with the function name
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

Have fun with serverless!

over 4 years ago · Santiago Trujillo Relatório

0

You can pass the other function's name or ARN as an environment variable. For example:

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
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda