Book Image

Serverless Architectures with AWS

By : Mohit Gupta
Book Image

Serverless Architectures with AWS

By: Mohit Gupta

Overview of this book

Serverless Architecture with AWS begins with an introduction to the serverless model and helps you get started with AWS and Lambda. You'll also get to grips with other capabilities of the AWS Serverless Platform and see how AWS supports enterprise-grade serverless applications with and without Lambda. This book will guide you in deploying your first serverless project and exploring the capabilities of serverless Amazon Athena, an interactive query service that makes it easy to analyze data in Amazon Simple Storage Service (S3 Amazon) using standard SQL. You’ll also learn about AWS Glue, a fully managed ETL service that makes categorizing data easy and cost-effective. You'll study how Amazon Kinesis makes it possible to unleash the potential of real-time data insights and analytics with capabilities such as video streams, data streams, data firehose, and data analytics. Last but not least, you’ll be equipped to combine Amazon Kinesis capabilities with AWS Lambda to create lightweight serverless architectures. By the end of the book, you will be ready to create and run your first serverless application that takes advantage of the high availability, security, performance, and scalability of AWS.
Table of Contents (8 chapters)

Chapter 1: AWS, Lambda, and Serverless Applications


Solution for Activity 1: Creating a New Lambda Function That Finds the Square Root of the Average of Two Input Numbers

  1. Click on Create a function to create your first Lambda function on the AWS Lambda page.

  2. On the Create function page, select Author from scratch.

  3. In the Author from scratch window, fill in the following details:

    Name: Enter myFirstLambdaFunction.

    Runtime: Choose Node.js 6.10. The Runtime window dropdown shows the list of languages that are supported by AWS Lambda and you can author your Lambda function code in any of the listed options. For this activity, we will author our code in Node.js.

    Role: Choose Create new role from template(s). In this section, you specify an IAM role.

    Role name: Enter lambda_basic_execution.

    Policy templates: Select Simple Microservice permissions.

  4. Now click on Create function.

  5. Go to the Function code section.

  6. Use the Edit code inline option, and enter this code:

    exports.handler = (event, context, callback) => {
        // TODO
        let first_num = 10;
        let second_num = 40;
    
        let avgNumber = (first_num+second_num)/2
        let sqrtNum = Math.sqrt(avgNumber)
        callback(null, sqrtNum);
    };
  7. Click on the dropdown next to Select a test event in the top-right corner of the screen and select Configure test event.

  8. When the popup appears, click on Create new test event and give it a name. Click on Create and the test event gets created.

  9. Click on the Test button next to test events and you should see the following window upon successful execution of the event:

    Figure 1.18: Test successful window

Solution for Activity 2: Calculating the Total Lambda Cost

  1. Note the monthly compute price and compute time provided by the Free Tier.

    The monthly compute price is $0.00001667 per GB-s and the Free Tier provides 400,000 GB-s.

  2. Calculate the total compute time in seconds.

    Total compute (seconds) = 20M * (1s) = 20,000,000 seconds

  3. Calculate the total compute time in GB-s.

    Total compute (GB-s) = 20,000,000 * 512MB/1024 = 10,000,000 GB-s

  4. Calculate the monthly billable compute in GB- s. Here's the formula:

    Monthly billable compute (GB- s) = Total compute – Free tier compute

                                                               = 10,00,000 GB-s – 400,000 Free Tier GB-s

                                                               = 9,600,000 GB-s

  5. Calculate the monthly compute charges in dollars. Here's the formula:

    Monthly compute charges = Monthly billable compute (GB-s) * Monthly compute price

                                                   = 9,600,000 * $0.00001667

                                                   = $160.02

  6. Calculate the monthly billable requests. Here's the formula:

    Monthly billable requests = Total requests – Free tier requests

                                                  = 20M requests – 1M Free Tier requests

                                                  = 19M Monthly billable requests

  7. Calculate the monthly request charges. Here's the formula:

    Monthly request charges = Monthly billable requests * Monthly request price

                                                 = 19M * $0.2/M

                                                 = $3.8

  8. Calculate the total cost. Here's the formula:

    Total cost = Monthly compute charge + Monthly request charges

                      = $160.02 + $3.8

                      = $163.82