Skip to content

Instantly share code, notes, and snippets.

@AminuIsrael
Last active January 29, 2021 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AminuIsrael/fdf575f9a88c14fca93aa7e4c804fe98 to your computer and use it in GitHub Desktop.
Save AminuIsrael/fdf575f9a88c14fca93aa7e4c804fe98 to your computer and use it in GitHub Desktop.
Cross Lambda Invocation between AWS Lambda

How to perfrom Cross Invocations between Lambda Functions

  • Create a Lambda function, which will receive the lambda invocation, choose python 3.7 or upper as your runtime, you can use the AWS default role or choose your predefined role and lastly copy the sample function (child_lambda.py) below.
  • Copy the function's ARN then head over to AWS IAM service and navigate to policies
  • Click on Create policy
  • Click the JSON icon, copy and paste the policy.json below
  • Replace the "Resource" value in the policy.json with the ARN of the Lambda function
  • Click on Review policy and give a name and description to your policy and then create policy
  • Head over to IAM and click on Roles, create a new role, click on Lambda as the AWS service then click on Permissions and tick the AWSLambdaBasicExecutionRole and also tick the new policy role you just created and lastly click on Tags
  • Give the new role you created a name and description.
  • Head over to AWS Lambda and create a new lambda which will send the invocations to the function you've created previously. Select your prefered runtime and choose the role you created in the step above.
  • You can copy the code in the parent_lambda.py and add your enviromental variable(AWS account id, function name and region) in the lambda function.
  • And if you run the function you'll see the child function will be invoked and also the response of the function you just invoked in the parent's function.

**Note: This can only happen across regions**
import json
def lambda_handler(event, context):
#Collect data sent by the parent lambda
amount = event['Amount']
quantity = event['Quantity']
#Implement Logic Here
result = int(amount) * int(quantity)
#return the result
return result
import json
import boto3
# Define the client to interact with AWS Lambda
client = boto3.client('lambda')
#Get the credentials from environmental variables
adw_accout_id = os.environ['AccountId']
adw_function_name = os.environ['FunctionName']
region_name = os.environ['Region']
def lambda_handler(event,context):
#Define the payload that will be passed to the child function
payload = {"Amount": "1000",
"Quantity": "2"}
#Invoke Child's Function with payload
response = client.invoke(
FunctionName = f'arn:aws:lambda:{region_name}:{adw_accout_id}:function:{adw_function_name}', #Child's ARN
InvocationType = 'RequestResponse',
Payload = json.dumps(payload)
)
#After child's execution has been executed you can get the child's response of the lambda
responseFromChild = json.load(response['Payload'])
return responseFromChild
{
"Version":"2012-10-17",
"Statement":[
{
"Sid":"VisualEditor0",
"Effect":"Allow",
"Action":[
"lambda:InvokeFunction",
"lambda:InvokeAsync"
],
"Resource":"ARN-of-lambda-you-want-to-invoke"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment