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
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.
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.
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.
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 .
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:
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With