Learn Serverless

Serverless architecture is the industry’s latest buzzword and many of the largest tech companies have begun to embrace it. In this article, we’ll learn what it is and why you should use it. We’ll also set up AWS and create our serverless app. In the next article, we’ll create the slack app itself!


What is Serverless?

Serverless is a cloud computing paradigm in which the developer no longer has to worry about maintaining a server – they just focus on the code. Cloud providers, such as AWS or Azure, are now responsible for executing code and maintaining servers by dynamically allocating their resources. A variety of events can trigger code execution, including cron jobs, http requests, or database events. The code that developers send to the cloud is usually just a function so, many times, serverless architecture is implemented using Functions-as-a-Service, or FaaS. The major cloud providers provide frameworks for FaaS, such as AWS Lambda and Azure Functions.

Why Serverless?

Not only does serverless allow developers to just focus on code, but it has many other benefits as well. Since cloud providers are now responsible for executing code and dynamically allocate resources based on event triggers, you typically only pay per request, or when your code is being executed. Additionally, since cloud providers are handling your servers, you don’t have to worry about scaling up – the cloud provider will handle it. This makes serverless apps lower cost, easier to maintain, and easier to scale.


Setting up AWS Lambda

For this tutorial, I will be using AWS Lambda, so first, we’ll create an AWS account. I find AWS’s UI hard to understand and difficult to navigate, so I will be adding screenshots for each step.

Once you log in, you should see this:

Main screen

Next, we’ll set up an IAM user. An IAM (Identity and Access Management) user interacts with AWS and its resources on your behalf. This allows you to create different IAM users with different permissions and purposes, without compromising the security of your root user account.

Click on the “services” tab at the top of the page, and type “IAM” into the bar:

Click on the first result, and you’ll see, on the left-hand sidebar, that you’re at the dashboard. Click on the “Users” option to get to create our new IAM user.

Click on the “Add user” button to create a new user. Fill in the details as follows:

You can name your user anything you’d like, but I went with serverless-admin. Be sure that your user has “Programmatic access” to AWS, not “AWS Management Console Access”. You’d use the latter for teammates, or other humans who need access to AWS. We just need this user to interact with AWS Lambda, so we can just give them programmatic access.

For permissions, I’ve chosen to attach existing policies since I don’t have any groups, and I don’t have any existing users that I want to copy permissions for. In this example, I will create the user with Administrator access since it’s just for a personal project; however, if you were to use a serverless app in an actual production environment, your IAM user should be scoped to only access Lambda-necessary parts of AWS. (Instructions can be found here).

I didn’t add any tags and created the user. It’s vital to save the information given to you on the next screen – the Access ID and Secret Access Key.

Don’t leave this screen without copying down both! You won’t be able to see the Secret access key again after this screen.

Finally, we’ll add these credentials to command line AWS. Use this guide to get aws cli setup.

Make sure you have it installed by running aws --version. You should see something like this:

Then run aws configure and fill in the prompts:

I have the default region as us-east-2 already set up, but you can use this to determine what your region is.

To make sure that you have your credentials set up correctly, you can run cat ~/.aws/credentials in your terminal.

If you want to configure a profile other than your default, you can run the command as follows: aws configure --profile [profile name].

If you had trouble following the steps, you can also check out AWS’s documentation.


Set up serverless

Go to your terminal and install the serverless package globally using npm: npm i -g serverless. (More info on serverless here)
and your terminal should look something like this:

Next, navigate to the directory where you want to create the app, then run serverless and follow the prompts:

For this application, we’ll be using Node.js. You can name your app anything you want, but I’ve called mine exampleSlackApp.

Open your favorite code editor to the contents in exampleSlackApp (or whatever you’ve called your application).

First, we’ll take a look at serverless.yml. You’ll see there’s a lot of commented code here describing the different options you can use in the file. Definitely give it a read, but I’ve deleted it down to just:

service: exampleslackapp

provider:
  name: aws
  runtime: nodejs10.x
  region: us-east-2

functions:
  hello:
    handler: handler.hello
serverless.yml

I’ve included region since the default is us-east-1 but my aws profile is configured for us-east-2.

Let’s deploy what we already have by running serverless deploy in the directory of the app that serverless just created for us. The output should look something like this:

And if you run serverless invoke -f hello in your terminal, it’ll run the app, and you should see:

{
    "statusCode": 200,
    "body": "{n  "message": "Go Serverless v1.0! Your function executed successfully!",n  "input": {}n}"
}

For further proof that our slack app is live, you can head back to AWS console. Go to the services dropdown, search for “Lambda”, and click on the first option (“Run code without thinking about servers”).

And here’s your app!


We’re all set up now! – lookout for the next article (I’ll link here as well when it’s ready) to finishing making our serverless slack app! 🙂