Skip to content

ydnamjs/d6-counter-backend-lambda

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

D6 Counter Backend Lambda

This project is an API made with FastAPI that receives images of six sided dice and runs them through an AI model for detection and classification

It is intended for use in AWS Lambda Functions

Live Front End Site

Check out the React.js App that this API was made for at: https://ydnamjs.github.io/d6-counter-frontend/

Technologies Used

  • Amazon Web Services (AWS)
  • Python
  • Docker
  • FastAPI
  • PyTorch
  • Torchvision
  • openCV
  • Python Image Library (PIL)

Building and Deploying for AWS Lambda (Ubuntu)

Due to the file size of the function, it is not possible to deploy using the zip upload or AWS S3 options so a docker image must be built and uploaded to AWS ECR (Elastic Container Registry).

For consistency and reproducibility, the build process will be done on an AWS EC2 instance.

Launching an AWS EC2 instance

Navigate to the "Console Home" page by clicking the aws button which is always in the top left of the screen

image

Navigate to the All Services menu by selecting "View All Services"

image

In the all services menu, under "Compute" select "EC2"

image

In the EC2 dashboard select "Launch Instance"

image

Give your instance a name

image

In the "Application and OS Images (Amazon Machine Image)" section select Ubuntu and Ubuntu Server 24.04 LTS

image

In the "Configure Storage" section increase your root volume's size to 30 GiB

image

On the right, in the "Summary" section, select "Launch instance"

image

In the menu that automatically opens after, select "Create new key pair", enter a name in the "Key pair name" field, and select "Launch Instance"

image

Navigate to your instance's connect page

image

image

image

Follow the instructions listed on this page to connect to your instance

Your key is most likely in your Downloads folder

image

Installing Required Programs

Install Unzip:

sudo apt install unzip

Install AWS CLI by running the following commands:

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
rm -r aws
rm awscliv2.zip

See https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html for official instructions

Install Docker by running the following commands:

sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

See https://docs.docker.com/engine/install/ubuntu/ for official instructions

Setting up AWS Credentials

Navigate to the "Console Home" page by clicking the aws button which is always in the top left of the screen

image

Navigate to the All Services menu by selecting "View All Services"

image

In the all services menu, under "Security, Identity, & Compliance" select "IAM"

image

In the IAM Menu, navigate to the "Users" menu

image

In the Users Menu, select "Create user"

image

Give the user a name and select "next"

image

In the "Set permissions" menu, select "Attach policies directly" then select "Create policy"

image

In the "Create policy" menu's "Policy editor" section, select "JSON"

image

Paste the below policy in the text editor

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "ecr:CreateRepository",
                "ecr:CompleteLayerUpload",
                "ecr:TagResource",
                "ecr:GetAuthorizationToken",
                "ecr:UploadLayerPart",
                "ecr:InitiateLayerUpload",
                "ecr:BatchCheckLayerAvailability",
                "ecr:PutImage"
            ],
            "Resource": "*"
        }
    ]
}

See https://docs.aws.amazon.com/AmazonECR/latest/userguide/image-push-iam.html for additional information

Select "Next" at the bottom of the "Policy editor" section

image

Give the policy a name and optionally a description

image

At the bottom of the "Review and Create" menu, select "Create policy"

image

Return to the "Create User" tab and refresh the policy list

image

Enable the policy just created in the "Create policy" tab by checking the box next to its name

image

At the bottom of the "Create User" menu, select "Next"

image

At the bottom of the "Review and create" menu, select "Create user"

image

Select the user in the "users" menu

image

In the "Summary" section, select "Create access key"

image

Select "Command Line Interface (CLI)" from the "Use Case" Section

image

Check the "Confirmation" box and select "Next"

image

Optionally, enter a description and then select "Create access key"

image

Inside the EC2 Instance run the following command:

sudo aws configure

Copy the "Access key" and paste it into the "AWS Access Key ID" field for the aws configure

image

Copy the "Secret access key" and paste it into the "AWS Secret Access Key ID" field for the aws configure

image

Optionally enter a region like "us-east-2" into the "Default region name" field for the aws configure

Enter "json" as the "Default output format" field for the aws configure

Building and Pushing the Docker Image

See: https://docs.aws.amazon.com/lambda/latest/dg/python-image.html#python-image-instructions for official instructions

Return to the AWS IAM Console and copy your "Account ID" from the AWS Account section

image

Return to your EC2 instance and run the following command replacing both "us-east-2"s with your region of choice and the "111111111111" with your "Account ID"

sudo aws ecr get-login-password --region us-east-2 | sudo docker login --username AWS --password-stdin 111111111111.dkr.ecr.us-east-2.amazonaws.com

Create an ECR Repository by running the following command replacing the "us-east-2" with your region of choice

sudo aws ecr create-repository --repository-name d6-counter-backend-lambda-demo --region us-east-2 --image-scanning-configuration scanOnPush=true --image-tag-mutability MUTABLE

Clone this repository

git clone https://github.com/ydnamjs/d6-counter-backend-aws-lambda.git

Navigate into the repository

cd d6-counter-backend-aws-lambda

Build the Docker image

sudo docker build --platform linux/amd64 -t d6-counter-backend-lambda-demo:latest .

Tag the docker image replacing "111111111111" with your "Account ID" and "us-east-2" with your region of choice

sudo docker tag d6-counter-backend-lambda-demo:latest 111111111111.dkr.ecr.us-east-2.amazonaws.com/d6-counter-backend-lambda-demo:latest

Push the image up to the ECR Repository replacing "111111111111" with your "Account ID" and "us-east-2" with your region of choice

sudo docker push 111111111111.dkr.ecr.us-east-2.amazonaws.com/d6-counter-backend-lambda-demo:latest

Deploying Image on AWS Lambda

Navigate to the "Console Home" page by clicking the aws button which is always in the top left of the screen

image

Navigate to the All Services menu by selecting "View All Services"

image

In the all services menu, under "Compute" select "Lambda"

image

In the "Lambda" page, select "Create a function"

image

In the "Create function" menu, select "Container image", then enter a name and select "Browse images"

image

Open the "Select repository" dropdown and select the repository that the image was pushed to

image

Select the image by clicking the circle next to it and then click the "Select image" button

image

At the buttom of the "Create function" menu, select "Create function"

image

In the function overview menu, select "Configuration"

image

In the "Configuration" menu, select "General Configuration" then "Edit"

image

In the "General Configuration" menu set the "Memory" to 2000 MB, "Ephemeral storage" to 1000 MB, and "Timeout" to 1 min 0 sec

image

In the "Execution role" section of the "General Configuration" menu select "Create a new role from AWS policy templates" then enter a role name and click "Save"

image

About

API made with FastAPI that receives images of six sided dice and runs them through an AI model for detection and classification

Resources

Stars

Watchers

Forks

Contributors