Outreachy internship with Mozilla: AWS Lambda with Serverless

Hi there!

In this blog post, we will see how AWS lambda works with Serverless framework.

In my previous blog posts, I’ve talked about what is AWS, how to create your first Lambda function, what is AWS API gateway etc. So, you have already seen that there are several ways to set up an API gateway and Lambda functions. One can do it all with the AWS web interface or with command line commands. But, the Serverless framework makes it a way more easy. Now, let’s see what is Serverless framework and how it makes our task easy. :)

Serverless Framework is a kind of toolkit that is used to deploy serverless architectures to any provider. My mentor ( Les Orchard) gave an interesting definition while explaining this to me-

“Serverless framework sets up an API gateway and Lambda functions automatically from a recipe.” :D

Isn’t is cool? Hehe.

Getting Started with Serverless

  1. First of all, you need to install serverless framework in your machine.

npm install -g serverless

2. Next, you need to set up your AWS provider credentials. For this, you should know your API Key & Secret key. If you don’t have any, you should search for Identity & Access Management (IAM) after signing in to your AWS account and should create an IAM user with admin access. This enables you to see your API and Secret key. Next, you can export your API and Secret key as environment variables so they would be accessible to Serverless and the AWS SDK in your shell as shown below:

export AWS_ACCESS_KEY_ID=<your-key-here>

export AWS_SECRET_ACCESS_KEY=<your-secret-key-here>

3. Now, you can create a new service using any language (Node.js, python, java etc) template by specifying a unique name and an optional path for your service. You can also go to that particular directory first and then use serverless create command without mentioning the path.

serverless create — template aws-nodejs--path my-service

Now, we created a handler.js and serverless.yml file as templates. You can modify those files according to your application’s requirements.

4. Finally, you can deploy your service using-

serverless deploy

Tada. Your application starts running. :)

This thing is difficult to understand. I’ve asked so many questions to my mentor and he answered all of them with atmost patience. :)

So, the idea is to use this information and implement it for one of the preset theme of Firefox color with POST request. I’ve already written a script that takes all the preset themes and produce a directory that contains add-on files for each of the themes. But now, AWS lambda came into picture and the major idea is to write lambda function with API gateway that accepts theme JSON via POST and send the formatted data in a response (in zipped folder). I tried that with serverless framework. My mentor also taught me an awesome thing called Cloudwatch. :)

Cloudwatch is useful to check each and every step of your requests (GET/POST) and you can see how data is being passed. So, I checked my code on Cloudwatch (with lot of console statements :P ). I checked for each preset theme using curl. As of now, data is passed correctly and add-on is created perfectly. Only there’s a little difficulty in passing data as zipped folder in the response. I know this could be solved as we take deeper dive in the project. :)

At the end, I would like to thank my mentor (Les Orchard). I can’t believe that Outreachy is ending. :( In my next blog post, I will write about my whole journey. I am so grateful to my mentor and Outreachy organizers for giving me this opportunity.

Les, I know I can’t ping you over mozilla slack channel and mozilla email from tomorrow onwards :( , but I will keep asking my stupid questions over emails or IRC. Hehe. :P

All my dear readers, I will keep updating and writing my blogs ( not as an Outreachy intern anymore but as a happy mozilla contributor). :D

Stay tuned! :)

Thanks for reading.