How to provision Lambda and Lambda Layer using CDK
Tags: aws, serverless, cdk, python
Published: 2021-03-18 01:32:18

### Why using CDK?
CDK let you provision all your aws service using the programming language you use. 

In this example, I will define a Lambda and Lambda Layers using CDK python. 

First, make a new project 

`cdk init my-sample-app --language python`

Then activate the virtual env 

`.env\Scripts\activate.bat`


Set up the project structure 

```
my-sample-app
|-lambda
|--boto3Folder
|--MyFirstLambdaFunction
|---app.py
|--All the lambda folder here
|-MySampleAppStack
|--my_sample_stack.py
```

**What is Lambda Layer?**
*In my own words:*
Lambda Layers is the place you install your library in cloud. Normally you just `pip install some-library` for your python project right? Lambda Layers is just do that, you install a library inside a local folder, then CDK help you package all file inside that folder and upload it to cloud, then you can use it elsewhere. 

First install boto3 library inside `lambda/boto3Folder`

```python
pip install --target=d:\yourLocalPath\my-sample-app\lambda\boto3Folder boto3
```

By this, you install `boto3` library into `my-sample-app/lambda/boto3Folder`, then you should see some file and folder inside `boto3Folder`

Then inside `my_sample_stack.py`, define the Lambda Layer as follow:

```python
from aws_cdk.aws_lambda_python import PythonFunction, PythonLayerVersion


class MySampleStack(core.Stack):

        def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
           super().__init__(scope, id, **kwargs)
        
           # All the aws resources define here

            # Here define a Lambda Layer 
            boto3_lambda_layer = PythonLayerVersion(
               self, 'Boto3LambdaLayer',
               entry='lambda/boto3Folder',
               compatible_runtimes=[lambda_.Runtime.PYTHON_3_8],
               description='Boto3 Library',
               layer_version_name='Whatever name you want'
           )
    
```

In this few lines of code, you defined a Lambda Layer, tell CDK where is the folder in `entry`, in our case is `lambda/boto3Folder` and other necessary information. 

So now you have a Lambda layers, now we want to use this Lambda Layers, inside a Lambda function. 

In `MySampleStack` above add this to define a Lambda Function: 

```python
my_first_lambda_function=PythonFunction(
         self, 'MyFirstLambda',
         entry='lambda/MyFirstLambdaFunction',
         index='app.py',
         runtime=lambda_.Runtime.PYTHON_3_8,
         layers=[boto3_lambda_layer], # here you use the lambda layer above 
         handler='MyFirstLambdaHandler',
         timeout=core.Duration.seconds(10)
)

```

This you define a Lambda function that will use the Lambda layers. In your `MyFirstLambdaFunction/app.py` you can use `boto3` as usual. 

Inside MyFirstLambdaFunction/app.py

```python
import boto3

def MyFirstLambdaHandler(event, context):
    client = boto3.client('dynamodb')

    # do all the stuff with the client
```

```
cdk deploy
```
All the resources will start provision in the cloud. 

**Why using Lambda Layers?**
**1) Avoid install the same library over and over again.**
Lets say you have 10 Lambda function all will using 2 same library, then you will need to install 10 separate times inside 10 Lambda. Use Lambda Layers, install 1 time, you it by 10 different Lambda.

**2) Reduce size of your Lambda function.**
If your Lambda function is more that 10MB, you wont able to edit it in Aws Lambda console. 

**Summary**
In this article, you learned 
- How to create a Lambda Layer and Lambda function
- How to use Lambda Layer inside a Lambda
- Why you need to use a Lambda Layer? 

I hope you learned something. Have a good day. 

In next part of series, I will talk about set up environment variable for a Lambda function using CDK.

**Before you go,** if you like this series or find this useful consider to buy me a coffee 😊🤞 for 5 USD or more. 
I will prepare a GitHub repo for this whole tutorial series and arrange into separate commit for each part. 
This will only available for my supporter cause I spent a lot of time to prepare this. Anyway, I appreciate you here. Have a good day.

<a href="https://www.buymeacoffee.com/kenchoong" target="_blank">
![bmaco](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ap0mog1ue623vr1zbo36.jpg)
</a>

Shout out to me on Twitter: [@upupkenchoong](https://twitter.com/upupkenchoong)