◄︎ Gregor's Portfolio Site
11 May 2019

Intro to aws lambda

What is AWS Lambda? It is compute service which allows you to execute your code without needing to manage any servers or worry about availability. Each self-contained Lambda file is referred to as a function. These functions can be written in C#, Go, Java, Node.js, Python or Ruby.

All Lambda functions are stateless, which means it does not save client data generated in each session. The advantage in this is it allows AWS to rapidly scale as many copies of the functions as are needed to process the incoming events and scale back down when not needed. It’s all automatically handled by Amazon. And what’s more, functions can still connect to other AWS services like S3 or DynamoDB to store stateful information. Best of all, Lambda is included in the AWS free tier . 1 million requests and 3 million seconds of compute time free.

Lambda Use Cases

Lambda and Functions as a Service are the highest level of abstraction in cloud computing. Don’t worry about servers, availability or infrastructure. Just write your code, set triggers, set outputs. Lambda functions can be triggered when files are added, when a request is sent to an API, when someone invokes an Alexa command, or when something changes somewhere else in your AWS infrastructure. So what is Lambda good for? Here are a few basic categories:

Triggering Lambda

Lambda can be triggered by events coming from other AWS services including S3, DynamoDB and API Gateway. An easy implementation would be building a function to execute when a new file is uploaded to an S3 bucket. Another example would be triggering the function through HTTP call. That’s what we’ll explore in the steps below.

This will be an introductory tutorial for those with little to no experience using Lambda. All you’ll need to get going is an AWS account. We’re going to use Lambda to calculate a random number and we’ll use API Gateway to cerate an HTTP endpoint that can trigger our function.

Part 1: Setting up the Lambda Function

Start by accessing the AWS Lambda console . You’ll be greeted by a list of any existing Lambda resources in your current region. Even if you have a function already deployed, go ahead and choose Create function to launch a new one. For our purposes we’re going to Author from scratch  Choose a function name, runtime and Execution role. Make the function name as descriptive as you’d like. For the Runtime, set it to Node 8.10. And for execution role, choose the permission setup you’re most comfortable with.  Press Create function when you’re all set. With your function created, you’ll be greeted with a wide array of configurable options. For now scroll down to the Function code where you’ll find a text editor containing index.js.

We are going to build a random number generator. Whenever our API is accessed, it will return a random number in the body of the response. For simplicity there’ll only be one endpoint and it won’t take any parameters. We can accomplish this in just a few lines of code. Delete the content of index.js and paste in the following and Save:

exports.handler = async (event) => {
    let min = 0;
    let max = 10;       
    let randomNumber = Math.floor(Math.random() * max) + min;
    return randomNumber;

Feel free to use the Test button to simulate a hit to our function. The Execution Results and summary will be visible in a tab below index.js.

Part 2: Creating the API Gateway

Now, with our Lambda function configured we will set up an API endpoint we can access through any web browser. Leave Lambda and go to API Gateway console. Press+ Create API to start the process.

We’ll stick with the basic configuration. Keep it set to REST protocol, mark it as a New API, and provide it a straight forward name. When ready, press Create API. Our Gateway will be blank until we go to Actions menu and Create Method. Create a GET request.  Keep the Integration Type as Lambda Function. Make sure your current region is set in Lambda Region. If you need to verify what your region is, look in the URL. For Lambda Function, begin typing your function name and click it in the menu and press Save. If prompted, confirm the the request to grant permissions to your Lambda function.

Access the Actions menu once more and choose Deploy API. Create a Deployment stage and prove it a name. For Security make sure to choose Open so it can be accessed anywhere. Press Deploy when ready.

In the Production Stage Editor you’ll be greeted with your Invoke URL. Clicking your URL will trigger your lambda function and display the results. You’ve made your first API!