Dealing with Lambda Layers was never fun, so the latest way to create serverless function in AWS Lambda might be the best way

One of the best things about AWS Lambda is the variety of ways that you can create a serverless function. For example, you can dive right into the console, or use approaches like  Chalice or the  Serverless Framework, to name a few. For me, the latest way, announced at re:Invent 2020, is the most efficient way of testing your serverless function locally and dealing with large or awkward dependencies in your code. As you’d expect, this works perfectly with  AWS SAM. This post will give you a quickstart into what you need to do to build a container-based function using containers & Python.

In my case, I was building a function that would read data from Firestore in Google Cloud, run a data transformation and store the result in S3. I used to swear by Chalice, and built my application but found that it couldn’t bundle the GRPC dependency - I would have to build it myself. While the solution to this is just to build the dependency and package it up in the vendor directory of my Chalice application, I used the opportunity to finally try out container support.

Prerequisites

If you’re reading this, you’re probably using Python already. As well as having Python installed, you’ll need to have  Docker too.

Building a Docker Container With Dependencies

As always, the  AWS documentation will guide you through the basics.

First, you’ll need to install the Python runtime interface client using pip install awslamdaric

Next, create a Dockerfile that references the base image you are using. In the case below, I was using Python 3.8

Dockerfile

FROM public.ecr.aws/lambda/python:3.8

#cloud #python #serverless #aws lambda

Container Images for AWS Lambda With Python
1.40 GEEK