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
Check out the React.js App that this API was made for at: https://ydnamjs.github.io/d6-counter-frontend/
- Amazon Web Services (AWS)
- Python
- Docker
- FastAPI
- PyTorch
- Torchvision
- openCV
- Python Image Library (PIL)
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.
Navigate to the "Console Home" page by clicking the aws button which is always in the top left of the screen
Navigate to the All Services menu by selecting "View All Services"
In the all services menu, under "Compute" select "EC2"
In the EC2 dashboard select "Launch Instance"
Give your instance a name
In the "Application and OS Images (Amazon Machine Image)" section select Ubuntu and Ubuntu Server 24.04 LTS
In the "Configure Storage" section increase your root volume's size to 30 GiB
On the right, in the "Summary" section, select "Launch instance"
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"
Navigate to your instance's connect page
Follow the instructions listed on this page to connect to your instance
Your key is most likely in your Downloads folder
sudo apt install unzip
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
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
Navigate to the "Console Home" page by clicking the aws button which is always in the top left of the screen
Navigate to the All Services menu by selecting "View All Services"
In the all services menu, under "Security, Identity, & Compliance" select "IAM"
In the IAM Menu, navigate to the "Users" menu
In the Users Menu, select "Create user"
Give the user a name and select "next"
In the "Set permissions" menu, select "Attach policies directly" then select "Create policy"
In the "Create policy" menu's "Policy editor" section, select "JSON"
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
Give the policy a name and optionally a description
At the bottom of the "Review and Create" menu, select "Create policy"
Return to the "Create User" tab and refresh the policy list
Enable the policy just created in the "Create policy" tab by checking the box next to its name
At the bottom of the "Create User" menu, select "Next"
At the bottom of the "Review and create" menu, select "Create user"
Select the user in the "users" menu
In the "Summary" section, select "Create access key"
Select "Command Line Interface (CLI)" from the "Use Case" Section
Check the "Confirmation" box and select "Next"
Optionally, enter a description and then select "Create access key"
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
Copy the "Secret access key" and paste it into the "AWS Secret Access Key ID" field for the aws configure
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
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
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
Navigate to the "Console Home" page by clicking the aws button which is always in the top left of the screen
Navigate to the All Services menu by selecting "View All Services"
In the all services menu, under "Compute" select "Lambda"
In the "Lambda" page, select "Create a function"
In the "Create function" menu, select "Container image", then enter a name and select "Browse images"
Open the "Select repository" dropdown and select the repository that the image was pushed to
Select the image by clicking the circle next to it and then click the "Select image" button
At the buttom of the "Create function" menu, select "Create function"
In the function overview menu, select "Configuration"
In the "Configuration" menu, select "General Configuration" then "Edit"
In the "General Configuration" menu set the "Memory" to 2000 MB, "Ephemeral storage" to 1000 MB, and "Timeout" to 1 min 0 sec
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"











































