How To Build a Serverless Web Application - Part B
Tags: lambda, aws, dynamodb, serverless
Published: 2024-02-26 12:08:38

<!-- wp:heading -->
<h2 class="wp-block-heading">Module 3 – Build a Serverless Backend</h2>
<!-- /wp:heading -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Overview</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Now comes the building of a backend process that will be used for handling requests of your web application. For this we will use AWS Lambda and Amazon DynamoDB. Previously, we mentioned that users will be allowed to make requests about the delivery of unicorn to a location of their choice. To deal with such requests, a service running in the cloud will be invoked by JavaScript running in the browser.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Architecture Overview</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>You will need to implement a Lambda function that will be handling user requests regarding unicorn. The flow will be like this: Lambda function will select a unicorn from the fleet, it will use DynamoDB table to record the request and then it will send the details regarding the unicorn being dispatched to the frontend.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Amazon API Gateway is responsible to invoke the function from the browser. However, this connection will be implemented in the upcoming module. This module is meant for testing your function in isolation only.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Implementation</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Follow the implementation steps carefully to avoid any inconvenience.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4 class="wp-block-heading"><strong>Create an Amazon DynamoDB Table</strong></h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Make use of DynamoDB console to create a new table.</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><!-- wp:list-item -->
<li>Visit Amazon DynamoDB console and select “Create Table” option.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Enter “Rides” as the table name (this field is case-sensitive).</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>For the partition key field, enter “RideId” and select the key type to be String (this field is case-sensitive).</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Let the default settings be selected in the “Table Settings” section and select “Create Table” option.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Wait for the table creation process to complete. When the status says “Active”, select your table name.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Visit Overview Tab to navigate to the “General Information” section of your newly created table. Choose “Additional Info” and copy the ARN.</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4 class="wp-block-heading"><strong>Create an IAM Role for your Lambda Function</strong></h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>An IAM role is associated with every Lambda function. This role is responsible for defining the AWS services the function is allowed to communicate with. For this tutorial, you will have to create an IAM role that gives permission to your Lambda function for writing logs to Amazon CloudWatch Logs and an access for writing items in the DynamoDB table.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Visit IAM console and select “Roles” option. Then choose “Create Role”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Select “AWS Service” in the Trusted Entity Type section. Then choose “Lambda” and select “Next”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Go to the filter box and enter “AWSLambdaBasicExecutionRole” and select “Enter”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Select the checkbox present next to “AWSLambdaBasicExecutionRole” and select “Next.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Enter the role name to be “WildRydesLambda” while leaving other settings to default.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Select “Create Role” option.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>On the roles page, type “WildRydesLambda” and select the newly created name of the role.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Under “Add Permissions” section in the Permissions tab, choose “Create Inline Policy” option.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>To select a service, type “DynamoDB” in the search bar and select it.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Click on “Select Actions”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Look for “Actions Allowed” section and type “PutItem” in the search box. Select its checkbox after it has appeared.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Now in the “Resources Section”, choose “Add ARN link”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Select “Text” tab and paste the ARN of your table. Choose “Add ARNs” option.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Select “Next”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>For the policy name enter “DynamoDBWriteAccess” and choose “Create Policy” option.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4 class="wp-block-heading"><strong>Create a Lambda Function for Handling Requests</strong></h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In this particular step, you will be allowed to create a function that will process API requests regarding unicorn dispatch from the web application. For creating a Lambda function (RequestUnicorn) use AWS Lambda console. This function will process the API requests. Use the below implementation for your function code. Also configure the function in such a way that it uses IAM role you created previously.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Visit AWS Lambda console and select “Create a Function” option.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Keep the default author to be selected.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>For function name enter “RequestUnicorn”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Select Node.js 16.x version as any newer versions will not work in this tutorial.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>From the “Change default execution role dropdown” select “Use an existing role” option.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>From the dropdown menu of existing roles select “WildRydesLambda”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Select “Create Function” option.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Now scroll down to the code source section. All the contents present in index.js file should be replaced with the contents written in requestUnicorn.js.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>The following code is present in requestUnicorn.js file. Copy and paste this code in the index.js file.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>const randomBytes = require('crypto').randomBytes;

const AWS = require('aws-sdk');

const ddb = new AWS.DynamoDB.DocumentClient();

const fleet = &#91;

    {

        Name: 'Angel',

        Color: 'White',

        Gender: 'Female',

    },

    {

        Name: 'Gil',

        Color: 'White',

        Gender: 'Male',

    },

    {

        Name: 'Rocinante',

        Color: 'Yellow',

        Gender: 'Female',

    },

];

exports.handler = (event, context, callback) => {

    if (!event.requestContext.authorizer) {

      errorResponse('Authorization not configured', context.awsRequestId, callback);

      return;

    }

    const rideId = toUrlString(randomBytes(16));

    console.log('Received event (', rideId, '): ', event);

    // Because we're using a Cognito User Pools authorizer, all of the claims

    // included in the authentication token are provided in the request context.

    // This includes the username as well as other attributes.

    const username = event.requestContext.authorizer.claims&#91;'cognito:username'];

    // The body field of the event in a proxy integration is a raw string.

    // In order to extract meaningful values, we need to first parse this string

    // into an object. A more robust implementation might inspect the Content-Type

    // header first and use a different parsing strategy based on that value.

    const requestBody = JSON.parse(event.body);

    const pickupLocation = requestBody.PickupLocation;

    const unicorn = findUnicorn(pickupLocation);

    recordRide(rideId, username, unicorn).then(() => {

        // You can use the callback function to provide a return value from your Node.js

        // Lambda functions. The first parameter is used for failed invocations. The

        // second parameter specifies the result data of the invocation.

        // Because this Lambda function is called by an API Gateway proxy integration

        // the result object must use the following structure.

        callback(null, {

            statusCode: 201,

            body: JSON.stringify({

                RideId: rideId,

                Unicorn: unicorn,

                Eta: '30 seconds',

                Rider: username,

            }),

            headers: {

                'Access-Control-Allow-Origin': '*',

            },

        });

    }).catch((err) => {

        console.error(err);

        // If there is an error during processing, catch it and return

        // from the Lambda function successfully. Specify a 500 HTTP status

        // code and provide an error message in the body. This will provide a

        // more meaningful error response to the end client.

        errorResponse(err.message, context.awsRequestId, callback)

    });

};

// This is where you would implement logic to find the optimal unicorn for

// this ride (possibly invoking another Lambda function as a microservice.)

// For simplicity, we'll just pick a unicorn at random.

function findUnicorn(pickupLocation) {

    console.log('Finding unicorn for ', pickupLocation.Latitude, ', ', pickupLocation.Longitude);

    return fleet&#91;Math.floor(Math.random() * fleet.length)];

}

function recordRide(rideId, username, unicorn) {

    return ddb.put({

        TableName: 'Rides',

        Item: {

            RideId: rideId,

            User: username,

            Unicorn: unicorn,

            RequestTime: new Date().toISOString(),

        },

    }).promise();

}

function toUrlString(buffer) {

    return buffer.toString('base64')

        .replace(/\+/g, '-')

        .replace(/\//g, '_')

        .replace(/=/g, '');

}

function errorResponse(errorMessage, awsRequestId, callback) {

  callback(null, {

    statusCode: 500,

    body: JSON.stringify({

      Error: errorMessage,

      Reference: awsRequestId,

    }),

    headers: {

      'Access-Control-Allow-Origin': '*',

    },

  });

}</code></pre>
<!-- /wp:code -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>At last, choose “Deploy”.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4 class="wp-block-heading"><strong>Validate your Implementation</strong></h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In this step, you will test the Lambda function that you just created. Later we will add REST API with API Gateway to invoke this function from the browser application.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Go to “RequestUnicorn” function and choose “Test” present in the Code Source section. From the dropdown, select “Configure Test Event”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Leave the selections of “Create New Event” to default.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>For event name, enter “TestRequestEvent”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Now copy the following test event and paste it into “Event JSON” section.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:code -->
<pre class="wp-block-code"><code>{

    "path": "/ride",

    "httpMethod": "POST",

    "headers": {

        "Accept": "*/*",

        "Authorization": "eyJraWQiOiJLTzRVMWZs",

        "content-type": "application/json; charset=UTF-8"

    },

    "queryStringParameters": null,

    "pathParameters": null,

    "requestContext": {

        "authorizer": {

            "claims": {

                "cognito:username": "the_username"

            }

        }

    },

    "body": "{\"PickupLocation\":{\"Latitude\":47.6174755835663,\"Longitude\":-122.28837066650185}}"

}</code></pre>
<!-- /wp:code -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Choose “Save” option.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Now again visit “Code Source Section” and choose “Test”. From the dropdown select “TestRequestEvent”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Choose “Test” present on the test tab.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Expand the “Details” of the succeeded message.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Your function result should look something like this.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:code -->
<pre class="wp-block-code"><code>{

    "statusCode": 201,

    "body": "{\"RideId\":\"SvLnijIAtg6inAFUBRT+Fg==\",\"Unicorn\":{\"Name\":\"Rocinante\",\"Color\":\"Yellow\",\"Gender\":\"Female\"},\"Eta\":\"30 seconds\"}",

    "headers": {

        "Access-Control-Allow-Origin": "*"

    }

}</code></pre>
<!-- /wp:code -->

<!-- wp:heading -->
<h2 class="wp-block-heading">Module 4 – Deploy a REST FUL API</h2>
<!-- /wp:heading -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Overview</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This module will deal with exposing the Lambda function as a REST FUL API. This API will be publicly accessed on the internet and will be secured using Amazon Cognito user pool. By making these required configurations, you will transform a statically hosted website into a dynamic one. When client-side JS is added, it will make AJAX calls to all the exposed APIs.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Architecture Overview</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The static website can already interact with the API due to the page configuration made. The “/ride.html” page shows a simple map-based interface that is used for requesting unicorn ride. After performing proper authentication using “/signin.html” page, the users will be able to choose pickup location. After this, they can request a ride by clicking on “Request Unicorn” button.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In this module, you will learn to build cloud components of the API. For this case, the application will use ajax() method to make remote requests.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Implementation</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Follow the implementation steps carefully to avoid any inconvenience.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4 class="wp-block-heading"><strong>Create a New REST API</strong></h4>
<!-- /wp:heading -->

<!-- wp:list {"ordered":true} -->
<ol><!-- wp:list-item -->
<li>Go to Amazon API Gateway console and select APIs.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Under REST API choose “Build”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Select “REST” in the “Choose the Protocol Section”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Select “New API” in the “Create new API” section.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Enter “WildRydes” as the API name and from the Endpoint Type dropdown choose “Edge Optimized”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Select “Create API”.</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4 class="wp-block-heading"><strong>Create Authorizer</strong></h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Now it is time to create Amazon Cognito User Pools Authorizer. Amazon API Gateway makes use of JWT. These tokens are returned by the user pool for authenticating the API calls. In this particular section, we will be creating an authorizer for API. Follow the below steps to configure the authorizer.</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><!-- wp:list-item -->
<li>Select “Authorizers” option present in the left navigation pane of the newly created API.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Select “Create New Authorizer” option.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>For Authorizer Name enter “WildRydes”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Select type as “Cognito”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>In the region dropdown menu of the Cognito user pool, select the region you have been following throughout this tutorial. For Cognito User Pool name enter “WildRydes”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>For the token source enter “Authorization”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Choose “Create” option.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Select “Test” for verifying authorizer configuration.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Copy the authorization token from ride.html page and paste into the Authorization (header) field. Verify that the HTTP status response code is 200.</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4 class="wp-block-heading"><strong>Create a New Source and Method</strong></h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>You will validate Lambda proxy integration backed by RequestUnicorn function by creating a new resource in your API and then creating a POST method for it.</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><!-- wp:list-item -->
<li>Select “Resources” present in the left navigation pane of your API.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Click on “Create Resource” from the Actions dropdown menu.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>For Resource Name enter “ride” which will create a resource path “/ride”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Enable API Gateway CORS by selecting the checkbox.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Again choose “Create Resource” option.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>From the Actions dropdown menu, select “Create Method”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>From the new dropdown menu that appears under OPTIONS, select “POST”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>For the integration type select “Lambda Function”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Allow Lambda Proxy Integration by selecting its checkbox.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Select the same region for Lambda Region as well.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>For Lambda Function enter “RequestUnicorn”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Select “Save”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Choose “Ok” to give Amazon API Gateway the permission to invoke Lambda function.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Choose “Method Request” card.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>You will see a pencil icon present right next to “Authorization”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>From the dropdown list, select WildRydes Cognito user pool authorizer. Check the checkmark icon as well.</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4 class="wp-block-heading"><strong>Deploy your API</strong></h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In this step, you will deploy your API.</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><!-- wp:list-item -->
<li>Select the option of “Deploy API” from the Actions dropdown menu.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Select [New Stage] in the “Deployment Stage” list.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>For stage name enter “Prod”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Select “Deploy”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Make sure to copy the “Invoke URL”.</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4 class="wp-block-heading"><strong>Update the Website Config</strong></h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Now you will add the invoke URL in the /js/config.js file. The config file will still have all the information regarding the updates you made in the previous module for userPoolID, Amazon Cognito, userPoolClientID and region.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Open the js folder on your machine and then open config.js file in any text editor.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Paste the copied invoke URL in the “invokeUrl” value of the config.js file.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Save the modified file.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Below is the complete view of config.js file. Your file will have different values from these.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>window._config = {

    cognito: {

        userPoolId: 'us-west-2_uXboG5pAb', // e.g. us-east-2_uXboG5pAb        

        userPoolClientId: '25ddkmj4v6hfsfvruhpfi7n4hv', // e.g. 25ddkmj4v6hfsfvruhpfi7n4hv

        region: 'us-west-2' // e.g. us-east-2

    },

    api: {

        invokeUrl: 'https://rc7nyt4tql.execute-api.us-west-2.amazonaws.com/prod' // e.g. https://rc7nyt4tql.execute-api.us-west-2.amazonaws.com/prod,

    }

};</code></pre>
<!-- /wp:code -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Add, commit and push the updated file to the repository.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:code -->
<pre class="wp-block-code"><code>$ git add .

$ git commit -m "new_configuration"

$ git push</code></pre>
<!-- /wp:code -->

<!-- wp:heading {"level":4} -->
<h4 class="wp-block-heading"><strong>Validate your Implementation</strong></h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Note: Clear your browser’s cache before executing the following steps.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Update ArcGIS JS version to 4.6 in the ride.html file.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:code -->
<pre class="wp-block-code"><code>&lt;script src="https://js.arcgis.com/4.6/">&lt;/script>

 &lt;link rel="stylesheet" href="https://js.arcgis.com/4.6/esri/css/main.css">

Below is the complete example of ride.html file.

&lt;div id="noApiMessage" class="configMessage" style="display: none;">

        &lt;div class="backdrop">&lt;/div>

        &lt;div class="panel panel-default">

            &lt;div class="panel-heading">

                &lt;h3 class="panel-title">Successfully Authenticated!&lt;/h3>

            &lt;/div>

            &lt;div class="panel-body">

                &lt;p>This page is not functional yet because there is no API invoke URL configured in &lt;a href="/js/config.js">/js/config.js&lt;/a>. You'll configure this in Module 3.&lt;/p>

                &lt;p>In the meantime, if you'd like to test the Amazon Cognito user pool authorizer for your API, use the auth token below:&lt;/p>

                &lt;textarea class="authToken">&lt;/textarea>

            &lt;/div>

        &lt;/div>

    &lt;/div>

    &lt;div id="noCognitoMessage" class="configMessage" style="display: none;">

        &lt;div class="backdrop">&lt;/div>

        &lt;div class="panel panel-default">

            &lt;div class="panel-heading">

                &lt;h3 class="panel-title">No Cognito User Pool Configured&lt;/h3>

            &lt;/div>

            &lt;div class="panel-body">

                &lt;p>There is no user pool configured in &lt;a href="/js/config.js">/js/config.js&lt;/a>. You'll configure this in Module 2 of the workshop.&lt;/p>

            &lt;/div>

        &lt;/div>

    &lt;/div>

    &lt;div id="main">

        &lt;div id="map">

        &lt;/div>

    &lt;/div>

    &lt;div id="authTokenModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="authToken">

        &lt;div class="modal-dialog" role="document">

            &lt;div class="modal-content">

                &lt;div class="modal-header">

                    &lt;button type="button" class="close" data-dismiss="modal" aria-label="Close">&lt;span aria-hidden="true">&amp;times;&lt;/span>&lt;/button>

                    &lt;h4 class="modal-title" id="myModalLabel">Your Auth Token&lt;/h4>

                &lt;/div>

                &lt;div class="modal-body">

                    &lt;textarea class="authToken">&lt;/textarea>

                &lt;/div>

                &lt;div class="modal-footer">

                    &lt;button type="button" class="btn btn-default" data-dismiss="modal">Close&lt;/button>

                &lt;/div>

            &lt;/div>

        &lt;/div>

    &lt;/div>

    &lt;script src="js/vendor/jquery-3.1.0.js">&lt;/script>

    &lt;script src="js/vendor/bootstrap.min.js">&lt;/script>

    &lt;script src="js/vendor/aws-cognito-sdk.min.js">&lt;/script>

    &lt;script src="js/vendor/amazon-cognito-identity.min.js">&lt;/script>

    &lt;script src="https://js.arcgis.com/4.6/">&lt;/script>

    &lt;script src="js/config.js">&lt;/script>

    &lt;script src="js/cognito-auth.js">&lt;/script>

    &lt;script src="js/esri-map.js">&lt;/script>

    &lt;script src="js/ride.js">&lt;/script>

&lt;/body>

&lt;/html></code></pre>
<!-- /wp:code -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Save the modified file and add, commit and push it to the Git repository.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Visit /ride.html.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>If you are taken to the ArcGIS sign in page then enter the credentials to proceed further.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>You will see a map. Click anywhere on the map so you can set a pickup location.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Select “Request Unicorn”. You will be notified with a notification that the unicorn is on its way.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2 class="wp-block-heading">Module 5 – Resource Cleanup</h2>
<!-- /wp:heading -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Overview</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In this module, you will delete all the previously created resources like AWS Amplify App, AWS Lambda function, Amazon Cognito User Pool, DynamoDB table, IAM role, REST API and CloudWatch logs. Doing this will save you from paying extra charges.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Implementation</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Follow the implementation steps carefully to avoid any inconvenience.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4 class="wp-block-heading"><strong>Delete your App</strong></h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>First of all, delete your Amplify app.</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><!-- wp:list-item -->
<li>Go to AWS Amplify console and select “wildrydes-site” app.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>From the homepage choose “Actions” and then select “Delete App” option. When prompted to confirm, enter delete and also choose delete.</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4 class="wp-block-heading"><strong>Delete your Amazon Cognito User Pool</strong></h4>
<!-- /wp:heading -->

<!-- wp:list {"ordered":true} -->
<ol><!-- wp:list-item -->
<li>Go to Amazon Cognito console and select “WildRydes” user pool name.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Select “Delete User Pool” option.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Right next to Deactivate deletion protection there will be a checkbox that needs to be selected.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>To confirm deletion enter “WildRydes” and choose “Delete”.</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4 class="wp-block-heading"><strong>Delete your Serverless Backend</strong></h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><strong>AWS Lambda Function</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><!-- wp:list-item -->
<li>Go to AWS Lambda console and select “RequestUnicorn” function.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>From the Actions dropdown menu select “Delete” option.</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>IAM Role</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><!-- wp:list-item -->
<li>Navigate to IAM console and select “Roles”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Type “WildRydesLambda” in the filter box.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Select the checkbox and choose “Delete”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Enter the name of role i.e. “WildRydesLambda” to confirm deletion. Choose “Delete”.</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>Amazon DynamoDB Table</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><!-- wp:list-item -->
<li>Go to Amazon DynamoDB console and select “Tables”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>There will be a checkbox next to “Rides” table, select it.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Choose “Delete” option.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Select the checkbox for “Delete all CloudWatch alarms for Rides”, enter confirm in the input box and select “Delete”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>The table will disappear after a few seconds.</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4 class="wp-block-heading"><strong>Delete your REST API</strong></h4>
<!-- /wp:heading -->

<!-- wp:list {"ordered":true} -->
<ol><!-- wp:list-item -->
<li>Go to Amazon API Gateway console and select WildRydes API.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>From the Actions dropdown menu, select “Delete”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Choose Delete to make a confirmation.</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4 class="wp-block-heading"><strong>Delete your CloudWatch Log</strong></h4>
<!-- /wp:heading -->

<!-- wp:list {"ordered":true} -->
<ol><!-- wp:list-item -->
<li>Visit Amazon CloudWatch console, expand logs and select “Log Groups”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Select the checkbox present next to /aws/lambda/RequestUnicorn log group.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>From the Actions dropdown, select “Delete log groups”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Choose Delete to confirm.</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:heading -->
<h2 class="wp-block-heading">Conclusion</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This is the end of the tutorial in which we learned<a href="https://aws.amazon.com/getting-started/hands-on/build-serverless-web-app-lambda-apigateway-s3-dynamodb-cognito/"> how to build a serverless web application using AWS services</a>. You need to adopt these practices now because then you will have tons of opportunities to come across. Go ahead and practice this tutorial on your own, debug the errors and become an expert.</p>
<!-- /wp:paragraph -->