Dennis O'Keeffe
3 min readSep 29, 2021
Heading image

This combined is Day 26 of the #100DaysOfPython challenge and Day 8 of the Melbourne lockdown bonus series on the AWS CDK for TypeScript.

This post will demonstrate how we can deploy a sample Python lambda to the AWS Lambda service on LocalStack using the TypeScript AWS CDK.

Source code can be found here

Prerequisites

  1. Basic familiarity with AWS CDK for TypeScript.

Getting started

We will clone the template repo to get started:

At this stage, we will have the app in a basic working state.

In order to create the CDN stack, we will require the following AWS CDK libraries:

We can install these prior to doing any work:

The repository that we cloned already has a upgrade script supplied. We can use this to ensure our CDK packages are at parity:

We are now at a stage add a pattern construct to a stack.

Adding a pattern construct

We want to create a re-useable construct that will allow us to deploy a basic Python lambda functions.

We are also going to store the function inside of a S3 bucket using the assets library.

Let’s get to writing the code. Inside of lib/construct-python-lambda-fn/index.ts, add the following:

In this construct, we are inverting control over the lambda props be setting healthy defaults but allowing them to be overridden by any props that adhere to the interface.

We also require a path to our function folder that will be passed for the asset.

Making the attributes public for an instance allows us to access required properties in the stack that the constructs are used (which can be for added/distributing permissions to other constructs).

Now we are ready to create a simple function.

Creating the function

Inside of functions/hello-python-lambda/index.py, add the following code:

This is a super simple function, but the import part is that the file is name index.py and the function is named handler.

This relates to our base construct properties, that have handler: "index.handler" in them.

The index refers to the file name and the handler refers to the function name.

What we need to do now is use the construct in our stack and pass the functions/hello-python-lambda folder.

Adding the construct to our stack

Inside of lib/aws-cdk-with-typescript-foundations-stack.ts, add the following code:

Here we are creating a ConstructPythonLambdaFn construct and passing in the functions/hello-python-lambda folder.

That folder itself is an absolute path that is resolved using path.resolve and the available __dirname variable to resolve the folder that the stack is defined in with the relative path to functions/hello-python-lambda.

At this stage, we are ready to deploy our stack to LocalStack and test the function.

Deploying to LocalStack

This will follow on from what was demonstrated in the “Using The AWS CDK With LocalStack And aws-cdk-local” post.

From the project root, we need to synthesize the stack, bootstrap the stack (as we use assets) and deploy the stack:

At this stage, the function has now been deployed to LocalStack and we are ready to run our function.

Invoking the function

If we now check our LocalStack logs in the terminal where we have Docker running, we can see the following:

The return value will also be written out into lambda_output.txt:

Tear down

Finally, we can tear down the stack:

Summary

Today’s post demonstrated how to deploy a basic Python lambda function to LocalStack using the TypeScript AWS CDK.

Moving on from here, we can begin looking at how to add dependencies via Lambda layers in future posts.

Resources and further reading

  1. AWS CDK for TypeScript
  2. Project code

Photo credit: jdent

Originally posted on my blog. To see new posts without delay, read the posts there and subscribe to my newsletter.

I write content for AWS, Kubernetes, Python, JavaScript and more. To view all the latest content, be sure to visit my blog and subscribe to my newsletter. Follow me on Twitter.