AWS Lambda made easy!

Tim Haag
6 min readJan 6, 2021

My introduction into lambdas for clients, non-technical people or just devs about to get into it is that simple:

Think about it like a small computer used for one specific use case such as image compression or sending emails. This small computer is only running if you need it, this means if you don’t need it you don’t have anything to pay but if you need it a lot AWS takes care of the availability and scalability and you can be sure it will work even when you have traffic peaks. Take a look at the pricing it’s ridiculously cheep: https://aws.amazon.com/lambda/pricing/

Of course this is super simplified but you get the point.

In this article I’ll show you how to:

  • create a simple lambda
  • deploy it to AWS
  • make it public using AWS API Gateway
  • use the lambda

LET’S GET INTO THE EXCITING STUFF!

CREATE

Create a file called index.js either with your preferred IDE or with a text editor it doesn’t mater. To get started it is required that you name it index.js!

The code for an easy lambda could look something like this:

The file has to be named index.js because on the AWS console you’ve to define an entry point for the lambda to run. The default is index.handler which is translated into a file called „index.js“ and a function exported from this file called „handler“.

The return value of the „handler“ function will always be a promise because of the „async“ keyword and AWS knows what to do with this promise. There’s also another way to write lambdas using a callback but because callbacks are pretty obsolete nowadays we’ll not get into that today.

In the case everything within that function worked the promise would „resolve“ with an object containing the properties statusCode and body. On the other hand if something would throw an error within this function the returned promise would „reject“ with the thrown error message/data. We don’t have anything to do with this retuned promise because AWS is taking care of it but it’s good to understand how it works and why it has to took like this.

The other interesting part of this function is the „event“ parameter. Depending on how you’ve configured your lambda within the AWS console you’ll get information about the request including the request body which is important if you want to do something based on the data sent to the lambda.

The last thing you’ve to know is the return object itself. „statusCode“ is the http status code (list of all http codes: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes) and you’ll find this one in the response header if you inspect it within the browser. The „body“ property is the data (important it has to be stringified => JSON.stringified) you want to send back to the client.

DEPLOY

We’ll not go into automated deployment with any type of pipelines or cloud transformation because it’s also important to know how this works on the most basic level. This will be super easy.

Navigate to the folder on your computer where your index.js is. You need to create a zip-file including the index.js.

Go into your AWS console and search for lambda. The top part should look something like this:

Click on „Create function“

Select „Author from scratch“ and put in whatever name you want and hit „Create function“ in the bottom right. This could take a couple of seconds.

On the page you’ve been redirected to you should see something like:

AWS will automatically put in some sample lambda code but we want to use our own. Click on „Actions“ then „upload a .zip file“ and select the zip file you’ve created. AWS will automatically unzips the file and replaces the sample lambda code with yours.

You’ve just deployed you first lambda!

BUT we also want to make it globally available. Just to let you know it’s not always recommended or useful to make a lambda globally available especially if it’s handling sensitive data or just something which is not made for the public. Lambdas can also be called within you’re own AWS account/VPC (virtual private cloud) for example from EC2 instances with the specific IAM (identity and access management and we don’t need to go into detail about this here) role or when you upload something to your S3 bucket and the lambda should process it (for example you want to compress the uploaded image automatically via a lambda).

MAKE IT PUBLIC

To make your lambda public we’ve to use API Gateway which is the routing service of AWS where you can define API structures, the available methods for endpoints and what an endpoint is doing exactly e. g. calling our lambda.

Search for „API gateway“ within your AWS console.

Click on „Create API“ then on „build“ within the „REST API“ section.

Select „New API“ and put in a name for your API.

If you want a specific endpoint for your lambda click on „Actions“ then „Create Resource“. If not you’ve to click on “Actions” and “Enable CORS”.

Put in the name of the endpoint and check „Enable API Gateway CORS“ to make it publicly accessible.

Click „Create Resource“.

Through API gateway you can define the http method this endpoint should be accessible on. In our case we can simple activate the GET-method by clicking on the endpoint then „Actions“, „Create method“ and select “GET” then click the small checkmark besides the selected method.

Choose „Lambda function“ and check „Use Lambda Proxy integration“. The „Use Lambda Proxy integration“ is not really required in our case but it’s good to know because this will tell API gateway to proxy the given data for example from a post body to the lambda function. If you don’t use it you simply don’t have access to the data sent from the client. Select the lambda you’ve created and hit “Save”. If you now click on the GET-method below your resource it should look something like this:

Now your API gateway is set up. The last step is to deploy your API. Click again on „Actions“ then „Deploy API“ and enter a stage name. In a production environment you would have multiple stages. Probably multiple test stages and one production stage. We can just enter a new stage name and name it for example „prod“ for production. Hit „deploy“ and you should see something like:

This is the public url of your API gateway. You can just click on it to open it up in another tab. Because I have defined a resource within my API called „easy-lambda“ you’ve to append this to the url like „/easy-lambda“. You can review your defined resource by clicking on the stage „prod“. To change something on your api you’ve to click on “Resource” on the left side and after your done don’t forget to deploy your API.

If everything was done correctly we should see our message from the “body” property.

Alright, that’s it for deploying a lambda and making it public via API gateway. If you think that article helped you in some way consider destroying that clap button below and if you’ve any comments or questions use the comment section and I’ll get back to you.

--

--