Deploying an AWS Lambda
In order to deploy an AWS lambda you must do the following:
- Create a deployment package
- Upload the deployment package to S3
- Upload deployment package to lambda
Prior to AWS’s announcement at re:Invent 2018, developers were unable to test their code locally. Therefore, you had to repeat these steps in order to test each iteration of your code. In the early stages of your development repeating these steps can be exhausting. We suggest deploying your lambdas using Jenkins or other build automation tools.
Creating and uploading a lambda deployment package
As discussed in our post about resolving an invalid elf header, the best way to create a deployment package is to spin up a docker container. Using a docker container prevents us from running into trivial errors when invoking our lambda, because we are installing our dependencies on the same system as which it runs. We should create a dockerfile to build our container and an entrypoint script to upload our deployment package. Prior to uploading the deployment package to S3, our entrypoint script should install any dependencies and zip up our source code.
See our lambda deploy GitHub repo for an example of how we create our dockerfile and entrypoint script.
Creating a Jenkins Pipeline
A Jenkins Pipeline is a continuous delivery (CD) pipeline that enables software to be deployed from your source control to your customers in seconds. With that being said, we want to create a Jenkins Pipeline to grab our source code from our repo and deploy it to AWS. In order to accomplish this, our pipeline must do the following:
- Grab our source code
- Spin up a docker container
- Install lambda dependencies
- Create deployment package
- Upload deployment package to S3
- Use Terraform or some other infrastructure as code (IaC) software to deploy our lambda
Visit our GitHub repo to see how we created a Jenkinsfile to deploy an AWS lambda.
Conclusion
Setting up your infrastructure to deploy an AWS lambda can take some time, but once in place it will simplify your deployment process and speed up your delivery. If you have questions with setting up your pipeline, please do not hesitate to contact us.