Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 230
credizian Avatar asked Mar 25 '20 05:03

credizian


People also ask

How do you call one Lambda function from another?

In order to allow the ParentFunction to call the ChildFunction, we need to provide the ParentFunction with specific rights to call another lambda function. This can be done by adding specific policies to a role and then assign that role to the lambda function.

How do I invoke Lambda function from CloudFormation template?

AWS CloudFormation invokes your Lambda function asynchronously with an event that includes a callback URL. The function is responsible for returning a response to the callback URL that indicates success or failure. For the full response syntax, see Custom resource response objects.

Can a Lambda function invoke another Lambda?

We can use the AWS SDK to invoke another lambda function and get the execution response. When we have multiple lambda functions which are dependent on one another then we may need the execution output of another lambda function in the current lambda function inorder to process it.

Is it possible to trigger a Lambda on creation from CloudFormation template?

Yes, it is possible. Here are a few options: Manually create an SNS Topic. Add an AWS::SNS::Subscription to your stack with the lambda function as the Endpoint and the SNS topic as the TopicArn .


1 Answers

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!

like image 161
starpebble Avatar answered Oct 12 '22 15:10

starpebble