Guide: First Serverless Project
Let’s learn about the Serverless Framework!
What is the purpose of this article?
To quickly get up and running with the Serverless Framework. During this article, you will learn how to deploy code and resources to AWS using the Serverless Framework. These resources could be any AWS service which your application needs to run (e.g. databases, cloud functions, REST API’s).
What is Serverless Framework?
The Serverless Framework is a way to quickly deploy resources to AWS and grow your applications as they evolve while being able to easily tear down the entire application and redeploy. The normal pattern would be to deploy your application to test, dev, and prod stages. Thereby allowing your development team to be able to have a consistently working version while they make changes and break things in the dev environment. Leaving your test environment for CICD pipelines for doing things like canary deploys.
Why should I care?
Because the Serverless Framework is by definition infrastructure as code. Meaning by running a simple command in the terminal you can spin up everything required to make your application run. No more manual human intervention. Once it’s put together and connected you never need to worry about a lever not being pulled when you undoubtedly have to tear down your application infrastructure. Everything from your ReactJS website to your REST or GraphQL APIs backed by DynamoDB can be worked into a single file so all parts of your application are included and no single piece is left out or forgotten.
Where do I start?
I’m glad you asked :)
Now that we have a baseline for why learning the Serverless Framework is important to your automation desires. Let’s jump into the quickest way to get a Serverless project running and deployed to AWS.
Install Node and Npm by running brew install node and check if the installation was successful.
brew install node
Install the Serverless Framework CLI
npm install -g serverless
Setup your AWS Account
Create an account, https://portal.aws.amazon.com/billing/signup.
Login to the AWS console, https://signin.aws.amazon.com/signin.
Click Services at the top left and search for IAM.
On the IAM console, we need to create a user for the Serverless Framework. Giving the Serverless Framework permission to create, update, and delete resources on AWS. Click Add user.
Give the user the name serverless-admin with Programmatic access selected. Click Next: Permissions.
Switch to the Attach existing policies directly tab and select AdministratorAccess. Then click Next: Review.
Now hit Create user.
We need to copy the Access Key ID and the Secret Access Key for later use. So click Download .csv. Click Close.
Now we’re ready to set up our local computer for deployments. To do that, open up Spotlight (Command–Space bar) then search for terminal.
With the terminal open, copy the following command passing in your Access Key ID and your Secret Access Key from the file you downloaded called, credentials.csv.
serverless config credentials --provider aws --key <> --secret <>
Hit Enter and the Serverless Framework should now have everything it needs to make deployments to AWS on your behalf. Sweet!
Our first Serverless Project
Now it’s time to create our first Serverless Project. Open the terminal again and type the following commands.
serverless create --template aws-nodejs --path my-first-serverless-project
This will create a new directory called, my-first-serverless-project. It will also create some boilerplate for jumpstarting an aws-nodejs project by using the template flag. You should see the following output if everything was successful.
Now we can open up our project in our favorite text editor. I’m currently using atom. I find atom lightweight and easy to just start writing code immediately. However, feel free to use whatever text editor you want.
Let’s open our new project and take a look at the files inside.
In the handler.js file, we can see some boilerplate code for our AWS Lambda function. The function will run and respond with “Go Serverless v1.0! Your function executed successfully!”.
In the serverless.yml file, you will see our service defined as my-first-serverless-project and some configuration for what provider and runtime to use. The main section to pay attention too is called functions.
The functions section defines our AWS Lambda function which we are about to deploy. In plain English it says, go to the handler.js file and grab the hello function (handler.hello).
With a bit of background of the boilerplate, that was auto-generated. Let’s do our first deployment. Inside of your project directory, run the following command.
serverless deploy -v
This will begin the deployment of our my-first-serverless-project stack which contains our AWS Lambda hello function. You should see the following console output if everything went okay.
We can check if our deployment was successful by watching the output in the terminal. Although, we can also confirm the status of our stack by checking the AWS Cloudformation console.
This shows us that our stack was deployed successfully to AWS. If we open the AWS Lambda console we should see our lambda function as well.
Finally, if we open our function by clicking on the name. We can create a simple test to get an idea for what the code in the handler.js file was actually doing. Click the Select a test event drop down and hit Configure test event.
We can give the test event a name of the test and click Create. Now we have a test event ready so let’s see what happens when we hit the button called Test.
Awesome! Everything is working as it should. If you want to explore further try updating the message field in the response body in your handler.js file. Then redeploy.
serverless deploy -v
Go back to the AWS Lambda console and you should see the message change when you hit test again.
To tear down the stack we can run a very simple command in the terminal.
serverless remove -v
Congratulations! You created your first Serverless project! In this short article, we actually accomplished a lot.
- Created an AWS account
- Configured our local machine to make AWS deployments using the Serverless Framework
- Explored the AWS console
- Deployed our first Serverless stack containing an AWS Lambda function
- Tested our deployed AWS Lambda function
- Removed our Serverless stack
Now that you have a basic idea for how to use the Serverless Framework to make AWS deployments. Try taking it a step further and try deploying other AWS resources.
- Industry Predictions for 2019
- Best Practices for Serverless Development
- Serverless CI/CD
- Serverless Web Applications — AWS v GCP
- Serverless Impact, Developer Velocity
- Guide, First Serverless Project
What does Serverless Guru do?
At Serverless Guru, we work with companies who want to accelerate their move to Serverless/Cloud Native event-driven development rapidly. We help clients with cloud development, backend development, frontend development, automation, best practices, and training to elevate entire teams. We are engineers first.
What did we miss?
When you leave your answer make sure to either comment below or tweet your answer to @serverlessgurux on Twitter.
Founder — Serverless Guru
LinkedIn — @ryanjonesirl
Twitter — @ryanjonesirl
Thanks for reading 😃