The first thing we need to have is access to AWS. You will have to sign up and provide your credit card details for account creation. AWS Lambda is essentially a language-specific environment that runs in an execution environment called runtime. This runtime passes events, context information and responses between other lambdas as well.
Install Python & PIP
Python should be most probably installed on your computer. You can test by running
python --version
or python3 --version
. Or, you can install Python from here.
Try running pip --version
if it's installed, if not then you can install it from here.
Installing Docker
I installed Docker on my iMac using the GUI package as it would handle setting all the configurations and paths correctly. If the GUI installer doesn't work then try the CLI installer. Download Docker here.
Installing AWS CLI
We would need the AWS CLI to push the docker container to the AWS ECR which is just a registry or repository for storing the images. Download the GUI installer from here. This completes our setup.
Building the App
Create a Dockerfile
Create a Lambda Function
We will create a simple function that returns the parameter it receives. This way we can know the information our lambda function receives when it is invoked. Also, we create a requirements.txt and add jsonpickle
as a dependency.
- Event is a dict that contains the parameters which are sent when the function is invoked
- Context param is the context in which the function is called
Running Lambda Container Locally
Build
e.g. if we name our container raring-rusty then the cmd will look like this:
After the build is complete, we can quickly verify by hitting the following command.
It should output something like this:
Before we can run the docker, we have to install a Runtime Interface Client which defines an HTTP interface for runtimes to receive invocation events from Lambda and respond.
Testing Lambda Invocation
Install RIC
We need this package to allow your code to interact with the Lambda service.
Now, let's run the docker image locally to verify if it is working ok.
Using Postman
You can install Postman(a dev tool for making HTTP requests) from here and test the lambda function before deploying it to AWS ECR.
URL
Method
Body -
- Select
raw
as the body type andjson
as the format
Authorization
Using CURL
Result
At this point, you should get a response back from your lambda function. Mine looked like the below:
Pushing to ECR
Pushing to ECR takes a few steps, I usually have static parts of the commands in my zsh_aliases
file to speed up the dev. Because some of the docker commands are quite lengthy.
Build the Docker Image
Authenticate ECR
Replace the aws_account_id with your account ID and region. A simpler way is to go to the AWS ECR page and create a new repository, then copy the URL from the URI column in the table.
If this runs well, you should see a response of Login Succeeded
Tag the Docker Image
In your ECR repository, the image tag is a good way to identify what change you have probably pushed or you can use the tag name in all sorts of creative ways too. When I am collaborating with someone on the same repository, I tag the image with my initials to know which ones contain my changes. Or you can use tag names to individually identify different components you are pushing, e.g. you have multiple lambdas etc.
After running this, when you run docker image
you should see another image showing up the URL as the image name with the tag you just created.
Final Push
We now push the docker image using the docker push
command. Some basic fails on this command arise from the repository not being created or having incorrect AWS credentials in your ~/.aws/credentials file. And lastly, you can verify the upload by going to the AWS ECR portal.