How to add Multiple Lambdas in One AWS CDK

Hello Tech Lovers, I hope you are doing well. Do you want to add multiple Lambdas Functions in one AWS stack of CDK script?

Well, you are at the right place. Today I am going to show you how you can add various lambdas to one CDK Stack. You can add groups of lambda functions in an AWS CDK script.

Must Read: How to use existing IAM.Role in AWS CDK?

I will provide complete code and guide you on making folders and Docker files to add more than one Lambda in one CDK Stack.

CDK.py File to add Multiple Lambdas in One AWS CDK:

import aws_cdk as cdk
from aws_cdk import aws_lambda
from constructs import Construct
from aws_cdk import aws_iam as iam

HTTP_TIMEOUT=cdk.Duration.seconds(700)
NAME_LAMBDA_1="lambda-one-name"
NAME_LAMBDA_2="lambda-two-name"
ROLE_NAME = "IAM-ROLE"
ACC_ID = "your account ID"
REGION = "Your region like us-east-1"
ENV_VARS = {
    "NAME": "EHTISHAM",
    "IP": "192.168.0.1"
}

class MultipleLambdasStack(cdk.Stack):
    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)
        lambda_role = iam.Role(self, f"{ROLE_NAME}-Role", 
                    assumed_by=iam.ServicePrincipal('lambda.amazonaws.com'),
                managed_policies=[
                    iam.ManagedPolicy.from_aws_managed_policy_name('AmazonDynamoDBFullAccess'),
                    iam.ManagedPolicy.from_aws_managed_policy_name('AWSLambda_FullAccess')
                ])
        first_lambda = aws_lambda.DockerImageFunction(
            self, 
            NAME_LAMBDA_1,
            function_name=NAME_LAMBDA_1,
            code=aws_lambda.DockerImageCode.from_image_asset("./Lambdas/one/path/"),
            timeout=HTTP_TIMEOUT,
            environment=ENV_VARS,
            role=lambda_role
        )
        second_lambda = aws_lambda.DockerImageFunction(
            self, 
            NAME_LAMBDA_2,
            function_name=NAME_LAMBDA_2,
            code=aws_lambda.DockerImageCode.from_image_asset("./Lambdas/two/path/"),
            timeout=HTTP_TIMEOUT,
            environment=ENV_VARS,
            role=lambda_role
        )
        cdk.CfnOutput(
            self,
            f"{NAME_LAMBDA_1}-arn",
            value=first_lambda.function_arn,
            export_name=f"{NAME_LAMBDA_1}-arn",
        )
        cdk.CfnOutput(
            self,
            f"{NAME_LAMBDA_2}-arn",
            value=second_lambda.function_arn,
            export_name=f"{NAME_LAMBDA_2}-arn",
        )
app = cdk.App()
MultipleLambdasStack(app, "MultipleLambdasStack",
    env=cdk.Environment(account=ACC_ID, region=REGION),
    )
app.synth()

This is the CDK.py file code now let me explain how you can add create folder and Docker files. Now you have one file CDK.py.

Let’s say you have two Lambda_One and Lambda_Two. You will need to create two folders like Lambda_One_Folder and Lambda_Two_Folder. Now copy your two lambda files into these folders.

Must Read: How to get Relationship properties values in Neo4J?

Each folder should have its own DOCKER file and requirements.txt file. You will need to provide the path of the folders in your CDK.py file.

Now run your CDK Synth command in your Command Prompt/Terminal like:

sudo cdk synth

Now the last step is the Deployment you can easily deploy your CDK script using:

sudo cdk deploy

If you face any issues let me know in the comment section I will try to solve your issues. You can also Hire me for your AWS CDK work.

Leave a Reply