DEV Community

Hai Tran
Hai Tran

Posted on

CDK to deploy a Lambda function

There are may options to deploy a Lambda fuction:

  • zip the code and upload to S3
  • share large dependencies via EFS
  • inline funtion with CloudFormation

In this note, I would like to share three ways to deploy a Lambda function using AWS CDK, and integrate multiple Lambda functions with a API Gateway for testing.

1. Deploy a lambda function by files

install dependencies

python -m pip install --target path-to-lambda numpy 
Enter fullscreen mode Exit fullscreen mode

aws_lambda.Function

handler_file = aws_lambda.Function(
            self,
            id="lambda-handler-wo-dependencies",
            code=aws_lambda.Code.from_asset(path.join(dirname, "lambda")),
            handler="handler.handler_file",
            runtime=aws_lambda.Runtime.PYTHON_3_8,
            memory_size=512,
            timeout=Duration.seconds(90)
        )
Enter fullscreen mode Exit fullscreen mode

2. Deploy a lambda function by ecr image

Note that the docker image is built from the local machine in this case. Here is the project structure

- aws_devops
    - lambda
        - Dockerfile
        - .dockerignore
        - handler.py
        - requirements.txt
     -aws_devops_stack.py
Enter fullscreen mode Exit fullscreen mode
handler_ecr = aws_lambda.Function(
            self,
            id="lambda-ecr-build-local",
            code=aws_lambda.EcrImageCode.from_asset_image(
                directory=path.join(dirname, "lambda")
            ),
            handler=aws_lambda.Handler.FROM_IMAGE,
            runtime=aws_lambda.Runtime.FROM_IMAGE,
            memory_size=512,
            timeout=Duration.seconds(90)
        )
Enter fullscreen mode Exit fullscreen mode

3. Deploy a lambda function with an existring ecr image

handler = aws_lambda.Function(
            self,
            id="EcrImageId",
            code=aws_lambda.EcrImageCode.from_ecr_image(
                repository=aws_ecr.Repository.from_repository_name(
                    self,
                    id="EcrImageId",
                    repository_name="EcrRepositoryName"
                )
            ),
            architecture=aws_lambda.Architecture.ARM_64,
            handler=aws_lambda.Handler.FROM_IMAGE,
            runtime=aws_lambda.Runtime.FROM_IMAGE,
            memory_size=512,
            timeout=Duration.seconds(90),
        )
Enter fullscreen mode Exit fullscreen mode

4. Integrate an API Gateway with multiple lambdas

create an api gateway

api_gw = aws_apigateway.RestApi(
            self,
            id="ApiGatewayLambdaDeployOptions",
            rest_api_name="api-lambda-deploy-options"
        )
Enter fullscreen mode Exit fullscreen mode

create api resource

api_file_resource = api_gw.root.add_resource(
            path_part="file"
        )
Enter fullscreen mode Exit fullscreen mode

create lambda integration

 api_file_intetgration = aws_apigateway.LambdaIntegration(
            handler=handler_file
        )
Enter fullscreen mode Exit fullscreen mode

add method to the resource

 api_file_resource.add_method(
            http_method="GET",
            integration=api_file_intetgration
        )
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
hoanphamduy profile image
hoan-pham-duy

Tested this for my personal projects.
Thank you for your post.