Table of contents
This is an article guide on creating a serverless movie API using Boto3 which is an AWS SDK for Python. It is a great start for anyone willing to practice using different AWS services like Lambda functions, API gateway, S3 bucket, etc.
Tools Used
Lambda Function
Lambda is an AWS serverless compute service(Function as a Service) that allows you to run backend code without provisioning or managing servers. You simply upload your code and Lambda takes care of scaling and resource management.API Gateway
API Gateway is a service that enables you to create, publish, maintain, monitor, and secure APIs at any scale. It acts as a front door for applications to access data, business logic, or functionality hosted on Lambda or other backend services.Boto3
Boto3 is the AWS SDK for Python, which allows developers to programmatically interact with AWS services. It provides an easy-to-use Pythonic interface to AWS services, enabling automation of resource provisioning and management tasks.Since we will be creating these AWS resources programmatically and not using the console or any infrastructure as Code, we will need an SDK.
S3 Bucket
S3 (Simple Storage Service) is an object storage service that stores and retrieves data from anywhere on the web. While typically used for storing static assets like images and files
The S3 bucket is used to store our movie data cover image and it comes in handy if we want to consume the created APIs and build out a user interface for it. but it can be skipped since we will not be building out any UIs.DynamoDB
DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. It's ideal for handling large amounts of data, which will be used to store and retrieve our movie data.
Project Structure
You can find the code here: https://github.com/Audrey-me/MovieAPI
Project Setup
Clone the repository and cd into the project directory
Read the "readme.md" file to understand the project structure
Configure your AWS CLI and put in your AWS credentials
aws configure
run this command to install the AWS SDK Boto3
pip install boto3
On the "services.py" file, comment out the other functions within the "main" function to allow the S3 bucket "my-movies-api-bucket" to be created first, then head over to the S3 console and upload your cover images there. then generate a public URL for those images.
def main(): create_s3() # create_dynamodb() # input("DynamoDB table created. Press Enter to continue...") # upload_data_to_dynamodb('movies.json', 'Movies-API') # input("Data uploaded to DynamoDB. Press Enter to continue...") # role_arn = input("Please enter the ARN of the IAM role created in the AWS console: ") # lambda_arn = create_lambda(role_arn) # input("Lambda function created. Press Enter to continue...") # create_api_gateway(lambda_arn) # input("API Gateway created and deployed. Press Enter to finish...")
Within the "movies.json" file, edit the "imageCoverUrl" value with the link to your image public URL.
{ "title": "Shawshank Redemption", "genre": "Drama", "year": 1994, "imageCoverUrl": "https://my-movies-api-bucket.s3.us-west-2.amazonaws.com/shawshankposter.jpeg" },
Go to the IAM Console to create an IAM role. This role will have the necessary permissions to read from DynamoDB, write logs to CloudWatch, and allow API Gateway to invoke it. take note of the ARN generated.
Then Run the "services.py" file again to continue creating the different AWS resources. It creates the DynamoDB table, the Lambda function, the API Gateway, and a zip file that contains our lambda function code that will be uploaded to lambda for further execution.
while creating the dynamodb you will be requested to provide the path for your "movies.json" file, this will enable it to read the contents of the file as well as upload it to your dynamodb table.python3 services.py
You will need to provide the ARN of the created IAM role during the creation of the Lambda function.
For simplicity and direct pass-through to Lambda functions we used the "AWS PROXY" integration making it easier to manage the integration without needing detailed request/response mapping in API Gateway.
We can head over to the AWS Console to check if the Lambda function and API Gateway has been successfully created.
Go to your browser and enter the "invoke URL" with the resource to view our different APIs
"/getmovies" resource retrieves all the movie data in our databasewhile "/getmoviesbyyear" retrieves a particular movie data based on the query parameter passed.
In conclusion, we have been able to create an API and deploy it using the different AWS serverless services.
Congratulations on coming this far in the article! I hope you will try out what you have learned. For further questions and clarification, please send a comment.