• Classes
  • 07 - Lambda Functions
  • Part 2

APS 03

In this assignment, we are going to create a new version of the work from the API class.

We will create a Lambda function that will make predictions using a pre-trained model. The Lambda function will be exposed through an API Gateway.

Accept assignment

All assignments delivery will be made using Git repositories. Access the link below to accept the invitation and start working on the third assignment.

Invitation link

Clone repository

Clone your private repository:

Question 1

Create a .gitignore and make sure the .env is in it!

Now, it's time to start working!

Our goal is to transform the predict route from class 02 into a lambda function. In other words, assume the model is already trained and that the model pickle can be embedded in the Docker image.

Important!

Notice that we will no longer be using FastAPI.

We will create a lambda function that has a handler for predict, then we will create an API Gateway that exposes the lambda function.

Part I

This part is about creating a Lambda function for the model prediction manually. It's a more hands-on approach compared to using the Terraform setup.

Info!

Use the work from class 02 as a reference.

You can copy the model pickles from class 02 (Ignore the training part).

Question 2

Create a proper project structure (folders and files).

Question 3

Create the .py file with the function handler.

Question 4

Create the requirements.txt file with the dependencies.

Question 5

Create the Dockerfile

Tip! 1

In order to install lightgbm, you will need to install some dependencies on the system. So, before RUN pip install -r requirements.txt you can add:

# Install system dependencies
RUN yum install -y libstdc++ cmake gcc-c++ && \
    yum clean all && \
    rm -rf /var/cache/yum

# Install the specified packages
RUN pip install -r requirements.txt

Question 6

Create the Docker image

Question 7

Test the Docker image locally

Warning

In the upcoming exercises, you will write scripts to provision resources (repository creation, functions, API, etc.).

If You prefer, you may use the Terraform setup right away (as will be required in Part 2).

Question 8

Create a new repository aps03_<INSPER_USERNAME> in ECR

Question 9

Tag and push your image to the ECR repository

Question 10

Create a lambda function associated with your image

Question 11

Test the lambda function

Question 12

Create an API Gateway and test it.

Leave in the README an example of how to test your API Gateway (curl command or Python code).

Part 2

This APS requires that you use the Terraform setup to create the Lambda function and API Gateway.

Use the past pages as a reference.

It's advisable to study about and implement:

  • How to use Terraform Modular Configuration (project structure)?
  • How to perform Variable Validation (e.g Lambda memory size)?
  • How to use Remote State (S3) to store Terraform state remotely for team collaboration?

Question 13

Redo all the steps from Part I using Terraform to provision infrastructure (where applicable).

Question 14

Create a good README.md file.

  • Explain how to deploy your project.

  • Leave in the README.md an example of how to test your API Gateway (curl command, bash script or Python code).

Delivering the assignment

Question 15

Commit and push before the deadline: mission accomplished!

After pushing the code on GitHub, to achieve the highest grade, the student must inform the professor (in class or during office hours) that they wish to present the project. The presentation will involve showing that the project works and discussing its components, possibly demonstrating that they studied or learned beyond the project requirements (if that was the case).

Before starting this stage, it is advisable to have the repository open (both on GitHub’s website and in VSCode), have the deployment completed and functional on AWS, and have code or calls ready to demonstrate its operation.

Students who do not present their project to the professor will have their maximum grade restricted at a B.

Deadlines

  • Github submission Deadline: available here
  • Presentation deadline (only for maximum concept): october 15th.

Rubrics

Concept Criteria
I (Insufficient) Did not complete the required steps, major components missing, or solution does not work.
D (Developing) Attempted some steps, but with significant errors or missing parts; solution is incomplete or unreliable.
C (Essential) Completed all required steps from Part 1; solution works but lacks polish or best practices, improvements possible.
B (Above Average) Solution is robust (both Part 1 and 2), well-documented (wrote a good README), follows best practices.
A (Excellent) Presented the project to the professor during class or office hours. Demonstrates deep understanding and extra effort. The Terraform infrastructure state is maintained centrally in S3.