Your first Serverless application
So you’re excited about the Serverless Framework but don’t know how to start?
This step-by-step blog post will show you how you can create, deploy and run your first Serverless application.
Let’s get started!
This chapter is a free sample taken from our “Learn Serverless” book.
This chapter will focus on the creation of our first real Serverless application. We’ll create a simple “Hello world” like application and look at the stuff the Serverless framework does for us behind the scenes.
Don’t be afraid if you don’t understand everything from scratch. We’ll take a deeper dive in the upcoming chapters.
Creating a Serverless project
Open up a terminal and cd into the directory where you want your project to be created.
Next up type
serverless project create
to start the wizard which will guide you through the setup process:
The first thing we need to enter is a name for our project. We’ll name it “helloworld”:
Enter a name for this project: helloworld
After that we need to enter the stage name for the project. Each stage is a separate, encapsulated Serverless application. Common stages are development, testing and production. Using stages is great because you can e.g. deploy your current code (which is under heavy development) to the development stage and tinker around while the production stage runs the latest stable version in production.
We create a stage we call development.
Enter a new stage name for this project: development
Next up enter the ACCESS KEY ID and SECRET ACCESS KEY of our previously created serverless-admin user. We’ve downloaded a .csv file with those credentials. Open up the file and enter the information accordingly:
Enter the ACCESS KEY ID for your Admin AWS IAM User: ENTERACCESSKEYIDHEREEnter the SECRET ACCESS KEY for your Admin AWS IAM User: ENTERSECRETACCESSKEYHERE
Now we need to specify in which AWS region our code should be deployed. We’ll pick eu-central-1. You should pick a region which is near to you:
Select a region for your project: eu-central-1
Now Serverless does it’s thing and generates everything project related for us. Next up it sets up the used AWS resources according to the auto generated CloudFormation template.
Creating our first function
Let’s create our first function we can invoke! We call it greeter.
Type in
cd helloworld
to move into the created Serverless project if (you haven’t already).
Type
serverless function create
Serverless asks you some questions about your function you want to create. The first thing is the name. We call it greeter
Enter a new function name: greeter
After that we need to specify which runtime we want to use. We’ll pick nodejs.
The last thing Serverless asks us if we want the function, an event or an endpoint. We’ll
choose the creation of an endpoint.
Great. Serverless has created a new function and endpoint for us!
Testing it locally
Let’s test our newly created function locally before we deploy it to AWS.
Run
serverless function run greeter
in the terminal (note that you don’t have a trailing / a the end of the function). You should get an output that the function was successfully executed.
Great let’s deploy it to AWS!
Deployment
Let’s deploy our API Gateway and Lambda function. Open the deployment dashboard with the
serverless dash deploy
command and select the functions gateway and the function from the menu.
Then move the cursor to Deploy and press enter to deploy the gateway and function to AWS.
Serverless will now minify, optimize and zip your Lambda function. Then they will be uploaded to the previously created S3 bucket. An API Gateway endpoint will be created and connected to the Lambda function.
Wait until Serverless has finished and copy the generated GET URL from the terminal output. Open up a browser with this link and see how your first Lambda function is executed in the AWS cloud.
Aside: Stages
We’ve already talked about it briefly but let’s take another look at it:
Serverless uses stages so that you can e.g. deploy your yet to be tested code into the testing stage. This separations is great because you can create different stages for different purposes (e.g. a production, testing and development stage). You can then deploy your production ready code to production while testing code in the testing stage.
Finished
Great. You’ve created, deployed and run your first Lambda function without every thinking about servers.
Our book “Learn Serverless” shows you how you can build a note taking application from scratch and run it in the cloud . Completely serverless.