How to Start Using the Serverless Framework with AWS
Getting started using the Serverless Framework with AWS
What is the Serverless Framework
There is a distinction to be made here between Serverless and the Serverless Framework.
Serverless is the new buzzword in technology but what exactly is it?
The basic premise is allowing the developer to focus on the application and not the infrastructure.
It’s a cloud computing model where your provider e.g. Amazon (AWS), Google or others manage the provision of servers. The provider spins up the server when needed so you only pay for what you use. You just focus on the code and everything else is taken care of.
For a more detailed introduction to Serverless and it’s pros and cons take a look at my ‘The Complete Beginners Guide to Serverless’ article
This article is about the Serverless Framework, an open-source framework for building and operating serverless applications including;
- Developing
- Deploying
- Monitoring
Why use the Serverless Framework
There are many ways to set up, develop, deploy and monitor serverless applications and I encourage you to research them.
I find the Serverless framework to be the most efficient. It’s a complete open source solution for developing serverless applications. If you want to delve deeper feel free to take a look at my article on the pros and cons of the serverless framework
Creating an account
First things first we need to create a serverless account, which can be done from https://dashboard.serverless.com/
Installing Serverless
Once you have created an account it’s time to set up the Serverless CLI (command line interface) on your system. This will allow you to run commands from your terminal.
Make sure you have npm installed by following this guide. Now it’s time to install the serverless framework.
To do this run
npm install -g serverless
By adding the -g this will install serverless globally so you can access the command anywhere not just in a certain directory. Now you need to login, this will redirect you to the Serverless login page. From your terminal type
serverless login
Now make sure you have an AWS account if you have you can skip this section and go straight to the ‘First Project’ section. If not then sign up for an AWS account. It’s free and comes with 12 months of generous usage limits, then
Create AWS Access Keys
- Login to your AWS account and go to the Identity and Access Management (IAM) page
- Click on Users and then Add user. Fill in the details and be sure to select the Programmatic access checkbox. Click Next to go through to the permissions page, click on Attach existing policies directly. Search for and select AdministratorAccess then click Next: Review and Create user.
- View and copy the API Key and Secret as we will need this in the next and final step
The final step is to run and enter your API Key, Secret and region as eu-west-2 by entering into your terminal
aws configure
First Project
Now that you have installed and logged in to the Serverless framework we can now create our first project.
I have set up a template that we can use to get us familiar with using the serverless commands.
You can take a look through the repository here feel free to fork/star the repo as we will be building on top of it in the upcoming articles.
To clone the repo to your machine run this command in your terminal
git clone git@github.com:GoldenRatio3/Serverless-101.git
Next, change into the directory so we can link to your AWS account
cd Serverless-101
Run the serverless command to create an application and connect to AWS. Looks like the below image, you can name your application serverless-101.
serverless
Now open in your favourite IDE (I would recommend visual studio code)
You should see something like this
We have 5 files
- .gitignore file which allows us to ignore committing auto-generated files to git
- handler.js file this is the entry point to our application and where the logic lives
- LICENSE file stating the uses for this code
- README.md file that explains how to install, run etc.
- serverless.yml file which is the main config file for your serverless function
Let’s Deploy
We have set up the Serverless framework and cloned the repo to our machine.
The next step is to deploy to your cloud provider (we will be using AWS for this article)
To deploy we just run
serverless deploy
This will build our packages and deploy to our cloud provider
Once the command completes, fire up postman and hit the endpoint that’s showing in the terminal. If all has been done correctly you should receive a response like the one below
Update
Now we have deployed our first serverless application, let’s update the function’s logic and see how easy it is to update and then redeploy.
First, we will update the message being returned to the client by opening up handler.js and updating the string on line 8 from:
message: "Great work! You have now deployed your first lambda"
to
message: "Great work! You have now updated and redeployed your lambda"
now we can save and run
serverless deploy
this will repackage our new application and deploy it.
To test the update has been successful, fire up postman as before and hit the endpoint. You should now see a response similar to below
Nice work!
Remove
We can also remove and clean up the application we have deployed by running
serverless remove
this will remove all objects from AWS including the S3 objects and the cloudformation stack, then at any time we can redeploy by running
serverless deploy
That’s a wrap
We have now created, setup and deployed our very first serverless application to AWS and confirmed the endpoint is up and available using postman.
If you want to be notified when the next articles are released for adding POST and DELETE endpoints, monitoring, running locally and debugging tips then you can subscribe to my mailing list over at my blog.
Thanks for reading this article, if you have any questions, feel free to reach out in the comments section below.