If you want an official reference from AWS, you can refer to it here: https://docs.aws.amazon.com/cdk/latest/guide/serverless_example.html

Remember our original high-level diagram here:

We want to be able to put files into our S3 bucket so that it could automatically send a notification to the AWS Lambda, and the Lambda would perform an ETL process into an AuroraDB table.

However, for simplicity sake, we want to deploy a basic Lambda function and make sure it works first before connecting the bucket and function together for a seamless integration

Install the relevant npm libraries

First, install the aws-lambda package: npm install @aws-cdk/aws-lambda
Make sure this package’s version is in sync with the other AWS CDK libraries, or it will fail to deploy!

Create your Lambda code for deployment

Prepare some basic Python lambda code for deployment. In my Github repo, it is stored in: src/lambda/basic_lambda/lambda_function.py

def lambda_handler(event, context):
    return {
        "statusCode":200
    }

# For direct invocation and testing on the local machine
if __name__ == '__main__':
    print(lambda_handler(None,None))

This function only outputs statusCode:200 as part of the payload. We don’t need anything complicated here as we want something simple to test if this block of code is executable.

Building the basic Lambda Stack

Now that you have some code to deploy, we move on to prepare AWS CDK to deploy our Lambda function. As an example, I created a basic stack configuration here within the repo: lib/basic_lambda_stack.ts

Here we deploy the above function with some basic configurations. I’m not going through the VPC, security, and subnet configurations as these settings can be found in the official AWS docs (https://docs.aws.amazon.com/cdk/api/latest/docs/aws-lambda-readme.html), and it will differ from mine because our environments are different. However, we are going through some foundational code here:

import * as cdk from '@aws-cdk/core';

import * as lambda from '@aws-cdk/aws-lambda';


export class basicLambdaStack extends cdk.Stack{

  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {

    super(scope, id, props);

    const function_name = 'gefyra-basic-lambda'; // Replace with your desired function name

    const lambda_path = 'src/lambda/basic_lambda'; // Replace with your desired path

    const handler = new lambda.Function(this, function_name, {

        functionName: function_name,

        runtime: lambda.Runtime.PYTHON_3_8,

        code: lambda.Code.fromAsset(lambda_path),

        handler: "lambda_function.lambda_handler"

    });

  }

}

Let’s break it down:

  • Declare the aws-lambda library as shown. Make sure to update all your libraries or your deployment will fail if they aren’t in sync!
  • basicLambdaStack is the class name for this app. Feel free to change it to anything you want.
  • I prepared both function_name and lambda_path for quick reference. You can do what you need as per your Python style guide.
  • Within the handler, provide the
    • functionName
    • runtime
    • code (based on the structure of the Git repo)
    • handler (This is the file itself and the function to invoke within the file)

Initialize the stack through the app

We want to create the stack from our bin file, and in my Github repo, it is bin/gefyra-cdk-demo.ts:

As shown above, I initialized the stack by referencing the library file and creating an object of the class.
AWS CDK is now ready to deploy your new stack!

npm run build && cdk synth

cdk deploy basicLambdaStack // this is what I named the stack within the bin/gefyra-cdk-demo.ts file

You shouldn’t see any errors at this stage.

What is happening under the hood?

  • AWS CDK will create a new IAM role that can execute the lambda instance
  • It will also create the necessary privileges required
  • You can add additional policies to this role, but it is another subject altogether. This is a basic tutorial for how it works.

AWS CDK will proceed to deploy if you allow the changes, and you should be able to see it on the console after it finished:

I went ahead and tested my deployment to receive a successful result.

3 3 votes
Article Rating