This project involves building a serverless web application that allows users to request unicorn rides from the Wild Rydes fleet. The application will feature a user-friendly HTML interface for indicating pick-up locations and a backend RESTful web service that will dispatch unicorns. Users can register and log in before making requests. The architecture of the application uses a combination of AWS services, including Lambda, API Gateway, DynamoDB, Cognito, and Amplify Console. The Amplify Console provides hosting for static web resources, while the backend API is built using Lambda and API Gateway. User management and authentication functions are provided by Cognito, and DynamoDB is used as a persistence layer for data storage. The application leverages serverless technology to deliver a highly scalable and cost-effective solution.
Step 1: Host a Static Website
Configure AWS Amplify to host the static resources for the web application with continuous deployment built in. The benefits of continuous deployment include faster time-to-market, reduced risk of errors and downtime, and improved overall software quality.
First, create a Git repository.
I will then use the AWS CLI to run git clone (using Git credentials I created in the IAM console) to clone an empty repository.
Next, I will copy the website content from an existing publicly accessible S3 bucket and add the content to the repository.
I will finish by committing and pushing the files to my Git service.
Next I'll enable web hosting with AWS Amplify. Amplify will deploy the code I committed to git.
Yay! The site works. Next I will test the Amplify Console's ability to rebuild and redeploy the app when it detects changes to the connected repository. I will do this by making a change to the main page. I will change the tab title from "Wild Rydes" to "Wild Rydes - Rydes of the Future!"
I've just created a static website which will be the base for the Wild Rydes business. AWS Amplify Console makes it really easy to deploy static websites following a continuous integration and delivery model.
Step 2: Manage Users
Now I'll create an Amazon Cognito user pool to manage users' accounts. I'll deploy pages that enable customers to register as a new user, verify their email address, and sign into the site.
AWS Cognito offers two types of pools: User Pools and Identity Pools. User Pools are used for user authentication and management of user profiles, while Identity Pools provide temporary AWS credentials for users to securely access AWS resources without requiring AWS account credentials. These pools enable developers to create a secure and scalable authentication and authorization system for their applications.
I will be using a user pool as the backend for the provided registration and sign-in pages.
To ensure that the web application can correctly authenticate users with the Cognito User Pool and access other AWS services securely, I will update the "config.js" file that contains settings for the Cognito User Pool ID, App Client ID, and AWS Region I just created.
I am now able to sign up for the app.
Step 3: Build a Serverless Backend
I will now create a backend process using AWS Lambda and Amazon DynamoDB to handle requests from the web application. The app enables users to request a unicorn ride to a specific location, and a cloud-based service will need to fulfill those requests by processing the data sent from the JavaScript running in the user's browser.
A Lambda function will be invoked each time a user requests a unicorn. The function will select a unicorn from the fleet, record the request in a DynamoDB table, and then respond to the frontend application with details about the unicorn being dispatched.
First create a DynamoDB table.
Create a role and attach a policy called "AWSLambdaBasicExecutionRole". This will let a Lambda function write logs to Amazon CloudWatch Logs.
Next, I will create a custom inline policy for this role called "ddb:PutItem". With this policy, the IAM role associated with the Lambda function will have permission to put new items into the DynamoDB table.
To process API requests from the web application, I will build the core Lambda function. The Lambda function will process a request to book a unicorn ride and record the ride information in a DynamoDB table. It takes in an event object and a context object, extracts information from the event object, finds an available unicorn, records the ride information in a DynamoDB table, and returns a response object to the API Gateway. The response object includes the ride ID, unicorn details, estimated time of arrival, and the rider's username.
Lastly, I will test the function that I built using the AWS Lambda console.
The test was successful.
Step 4: Deploy a RESTful API
Now I will create a RESTful API using Amazon API Gateway and secure it with Amazon Cognito. The API will be publicly accessible on the internet. I will then use client-side JavaScript to make AJAX calls to the API from my static website, turning it into a dynamic web application.
First I will create a new REST API.
Then I'll create a new resource called "/ride" within my API, and a POST method.
Voila! The website is now fully operational.
Step 5: Terminate Resources
As a best practice, it is best to delete resources you're not using to avoid unwanted charges. I will now go through and terminate all the resources I created, including an AWS Amplify app, an Amazon Cognito User Pool, an AWS Lambda function, an IAM role, a DynamoDB table, a REST API, and a CloudWatch Log.