In this POC, I will walk you through building a simple workflow with CI/CD (GitHub Actions) and IAC (Terraform).The end goal is to create and deploy a Lambda code in Python that will extract data from an API on a daily basis, and also to be able to modify the Lambda code and continuously integrate the modifications with our CI/CD system.
Architecture

Infrastructure As Code
As we want to create a Lambda function, here are a few things we need to consider:
How the Lambda function is going to interact with AWS services (IAM role)
How our Lambda function is going to be triggered (EventBridge)
How we are going to deploy our Lambda code (Docker + ECR)
Now, having this, we are able to figure out the services that need to be created using Terraform.
The first one is an IAM role that we will use to interact with S3, EventBridge, and ECR.
The second is the EventBridge rule (trigger of our Lambda function).
The third one is the ECR repository we will use to push our Docker image.
** We also need the policy to save the logs to Cloudwatch event (AWSLambdaBasicExecutionRole)

Docker
The next step is to write our Lambda code and use a Dockerfile to build a custom Docker image that we will use to deploy our Lambda function.
(Check the repo to see the code)

CI/CD
For the CI/CD part, we will use GitHub Actions. So what we need now is to be able to continuously integrate our code.

What does this job do?
This job simply runs all the CI tests I defined (just simple formatting with Black, isort,…) in a Docker container.
The next step now is to deploy our stack we wrote using Terraform.

The third step is to log in to ECR and push our Docker image to the ECR repository we created with Terraform.

AWS Lambda
Now, when we push our code to our GitHub repo, it will automatically (if all the jobs work well) push our Docker image to ECR.
REMINDER: This step needs to be done ONCE!!!
Okay!! We can create our Lambda function now and test if everything works well.


Next step
Now that we have the Lambda function created, we need to modify the GitHub Actions code so that each time we integrate new changes to the repo, it automatically updates our current Lambda function.
For this, we need to remove the Terraform job and also change the push job on GitHub Actions.

Noticed that in the run part, I added three lines to update our Lambda function each time a new push is made in our branch.
THAT’S IT FOR THIS SIMPLE PROJECT!
GitHub repo: https://github.com/Dorianteffo/ci_cd_lambda
——————————————
We are a data analytics agency focused on helping marketing, advertising, sales, and communication teams and agencies automate their reporting processes and build custom dashboards with KPIs for their business or their clients’ businesses.Contact us directly through our website: dublo.tech.