Session 4 | Lambda

Turing logo

This Session's Setup

Open up the AWS Mangement Console and login to your AWS account using the admin user you created in the setup. Do not log into your root account.

At the top of your Management Console next to your username, you'll see "Ohio" listed as your region. Go ahead and change that to US East (N. Virginia).


Checklist

Logistics and Expectations

  1. Lambda is an extremely powerful service that can be used many ways with a variety of internal AWS tools and external libraries. In this session, we will only cover the basic use of Lambda. We've included links to these internal and external tools and encourage you to explore them on your own.
  2. Use the chat box to jot down questions you have during class.
  3. Play your part in creating a safe and welcoming learning environment for your classmates.

Target

Agenda and Goals

  • Understand what serverless means
  • Understand the purpose of Lambda
  • Create and test a simple Lambda function using the browser console
  • Use the CLI to deploy a function with dependencies
  • Invoke a Lambda function with an S3 trigger

Brainstorm

Try It: Brainstorming

In sessions 1, 2, and 3, we worked with AWS's "serverfull" options. What might serverless be referring to? Take a minute to brainstorm, then share your thoughts in the chat box.


Cloud icon

What is "Serverless"?

From AWS:

Serverless is the native architecture of the cloud that enables you to shift more of your operational responsibilities to AWS, increasing your agility and innovation. Serverless allows you to build and run applications and services without thinking about servers. It eliminates infrastructure management tasks such as server or cluster provisioning, patching, operating system maintenance, and capacity provisioning.

The name "serverless" is misleading. Indeed, serverless computing does use servers. It's just that you don't need to acquire, provision, or maintain those servers.

Initially, the word "serverless" referred to AWS Lambda. In the past few years, "serverless" has come to encompass multiple AWS services, including S3, DynamoDB, API Gateway, Elastic File System, and more. These services allow the user to focus on building with the tools instead of managing and operating servers.

The diagram below from AWS shows a basic flow for a serverless web application:

AWS serverless diagram

Today, we won't be building out a full web application. Instead, we will be setting up an S3 trigger which invoke our Lambda function. It will look more like this diagram from AWS:

Flow from S3 upload to Lambda function trigger

Additional Resources


AWS Lambda icon

Lambda Overview

Lambda is a service that allows you to write functions that are hosted by AWS. There are no servers involved from the developer's perspective. A Lambda function only runs when it is invoked, and your account is only charged for the time that the Lambda function is executing.

Compare this to a web application hosted on an EC2 instance. When we provision that instance, we pay for every hour that it runs, even if nobody uses it. In addition, if we are suddenly inundated traffic, one of two things will happen: either our EC2 instance will not be able to handle the traffic (since your hardware is limited), or an Auto Scaling Group will add EC2 instances which will end up costing more money.

With Lambda, you pay only for the amount of time your function executes. Additionally, Lambda can scale infinitely. Each trigger will invoke another separate call of the Lambda function. Under free tier usage, you get 1 million Lambda requests and 400,000GB of compute time, free.

Languages

Lambda supports several runtimes, including:

When you're building out your Lambda function, it's important to use the same version of the language that Lambda supports. Otherwise, your function will not run on Lambda.

IAM Roles

If your Lambda function is integrated with other AWS services, such as S3, DynamoDB, CloudWatch, etc., you will need to grant Lambda an IAM role with policies that grant permission to use those resources. You can think of an IAM role in a similar way to an IAM user, except that a role is generally used to grant one AWS service permission to access another service, rather than granting a human or an app permission.

Every Lambda function will have a role, with the most basic version being the ability to post logs to CloudWatch. It is best practice to give every Lambda function its own role. In production code, don't share the same role between functions. Let's look at an AWS diagram showing how Lambda could a role to access other AWS services:

Lambda role diagram

Lambda can be used through the AWS console, the AWS CLI, SDKs, the Serverless framework, or AWS SAM CLI. In this course, we'll be using both the AWS console and the AWS CLI.

Additional Resources


Lady programming

Lambda Demo

We'll look at the Lambda console and and watch a function be created. Although you're welcome to follow along, it might actually be more useful just to watch the demo and take notes. You'll get to try creating your own lambda function next.

Together: Creating and Testing a Lambda Function

This section will take place in the AWS Management Console, but here's an overview of what we will cover:

Up and Running

  1. Setting up a function
  2. Creating an execution role
  3. Visual designer
  4. Lambda triggers
  5. Layers
  6. CloudWatch and other resources
  7. Cloud9 Editor
  8. Handler naming convention
  9. Environment variables
  10. Memory and timeout

Writing Code and Testing

  1. Event and context parameters
  2. Setting up test events
  3. Executing test events
  4. Looking at CloudWatch Logs

Try It: Calculator

Create a new function that takes in three numbers and returns the result of adding those numbers together. Modify the basic "Hello World" test event to pass in the correct data.

Additional Resources


Convert Image

Workshop: Lambda Function with S3 Triggers

In this section, you'll build out a function that is triggered when you upload an object to an S3 bucket.


Mop and bucket

Cleanup

See the cleanup instructions in the workshop portion.