Skip to content

Commit

Permalink
Merge pull request #174 from CloudBytes-Academy/article
Browse files Browse the repository at this point in the history
ART: Lambda deps using Python Function
  • Loading branch information
rehanhaider authored Nov 5, 2023
2 parents 7013753 + 63d2fe8 commit ddc2fbf
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
2 changes: 2 additions & 0 deletions content/aws/50001000-cdk-fn-create-lambda.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ Or learn some advanced methods on how to create lambda functions:

1. [Import an existing lambda function]({filename}50001010-cdk-fn-import-lambda.md)
2. [Using lambda layers]({filename}50001020-cdk-fn-lambda_layers.md)
3. [Using Lambda PythonFunction to create lambda functions]({filename}50001030-cdk-fn-lambda-python-deps.md)
4. [Running Lambda using AWS provided Docker image]({filename}50001030-cdk-fn-lambda-aws-docker.md)



98 changes: 98 additions & 0 deletions content/aws/50001030-cdk-fn-lambda-python-deps.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
Title: Manage Python dependencies in AWS Lambda using AWS CDK
Date: 2023-11-05
Category: AWS Academy
Series: AWS CDK
series_index: 1030
Tags: aws, cdk, python
Author: Rehan Haider
Summary: We look at how to package, install and manage Python dependencies in AWS Lambda using AWS CDK
Keywords: lambda, cdk, docker, dependencies


We looked at how we can [install Python packages beyond the ones available by default in AWS Lambda]({filename}50001020-cdk-fn-lambda_layers.md#create-a-lambda-layer-in-aws-cdk-using-python-to-handle-dependencies).

But I have always found it a bit cumbersome and unelegant to use Lambda layers to handle dependencies. Intead, I will show you an alternative way to handle Python dependencies in AWS Lambda using AWS CDK using an L2 construct called `PythonFunction`.

This is not availble in the `aws_cdk.aws_lambda` module, so we will have to install it using pip:

```bash
pip install aws-cdk.aws-lambda-python-alpha
```

## Create a lambda function using PythonFunction

```python
# filename: cdk_app/lambda_stack.py
from aws_cdk import (
Stack,
)
# πŸ‘‡πŸ½ import the python_alpha module
from aws_cdk import aws_lambda_python_alpha as _lambda

from constructs import Construct


class LambdaStack(Stack):
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)

# πŸ‘‡πŸ½ create a python lambda function
my_lambda = _lambda.PythonFunction(
self,
id="MyLambda",
entry="cdk_app/lambda", # πŸ‘ˆπŸ½ Required
runtime=_lambda.Runtime.PYTHON_3_10, # πŸ‘ˆπŸ½ Required
index="index.py", # πŸ‘ˆπŸ½ Optional, defaults to index.py
handler="handler", # πŸ‘ˆπŸ½ Optional, defaults to handler
)
```

In the above code we use `PythonFunction` to define a lambda function. It takes the following parameters:

1. `scope`: The scope of the construct. In our case, it is the `LambdaStack` class.
2. `id`: The id of the construct. In our case, it is `MyLambda`, this will be used to refer to the function in CloudFormation templates
3. `entry`: The path to the directory where the lambda function is located. In our case, it is `lambda`
4. `index`: The name of the file that contains the lambda function. In our case, it is `index.py`
5. `handler`: The name of the handler function. In our case, it is `handler`

By default, the Construct will look for a `requirements.txt` file within the `entry` directory and install the dependencies listed in it.

Let's create the `cdk_app/lambda/index.py` file:

```python
# filename: cdk_app/lambda/index.py

import requests

def handler(event, context):
response = requests.get("https://jsonplaceholder.typicode.com/todos/1")
return {
"statusCode": 200,
"body": response.json()
}
```

We put a `requirements.txt` file in the same directory as our `index.py` file with the following contents:

```text
requests
```

Now let's create the `app.py` file:

```python
# filename: app.py

import aws_cdk as cdk
from cdk_app.lambda_stack import LambdaStack

app = cdk.App()

lambda_stack = LambdaStack(app, "LambdaStack")

app.synth()
```

Run `cdk deploy` to deploy the stack.

Using this method, you just need to capture your dependencies in a `requirements.txt` file and the construct will take care of the rest.

0 comments on commit ddc2fbf

Please sign in to comment.