Before we finish this chapter, we are going to take a look at the serverless toolkit. This is an application that aims to provide a consistent experience when it comes to deploying your serverless functions across different cloud providers. You can find the service's homepage at https://serverless.com/.
As you can see from the home page, it supports both AWS and Microsoft Azure, as well as the Google Cloud Platforms and IBM OpenWhisk. You will also notice that there is a Sign Up button; click on this and follow the onscreen prompts to create your account.
Once signed up, you will receive some very simple instructions on how to install the tool and also deploy your first application; let's follow these now. First of all, we need to install the command-line tool by running:
$ npm install serverless -g
The installation will take a few minutes, and once it is installed you should be able to run:
$ serverless version
This will confirm the version that was installed by the previous command:
Now that the command-line tool is installed and we have confirmed that we can get the version number without any errors, we need to log in. To do this, run:
$ serverless login
This command will open a browser window and take you to a login page where you will need to select which account you wish to use:
As you can see in the preceding screenshot, it knows I last logged into serverless using my GitHub account, so clicking this will generate a Verification Code:
Pasting the code into your Terminal at the prompt and pressing Enter on your keyboard will then log you in:
Now that we are logged in, we can create our first project, which is going to be another hello-world application.
To launch our hello-world function in AWS, we must first create a folder to hold the artifacts created by the serverless toolkit and change to it; I created mine on my Desktop using:
$ mkdir ~/Desktop/Serverless
$ cd ~/Desktop/Serverless
To generate the files needed to launch our hello-world application, we need to run:
$ serverless create --template hello-world
This will return the following message:
Opening serverless.yml in my editor, I can see the following (I have removed the comments):
# The `events` block defines how to trigger the handler.helloWorld code
I updated the service to be russ-test-serverless-hello-world; you should choose something unique as well. Once I had saved my updated serverless.yml file, I ran:
$ serverless deploy
This, as you may have already guessed, deployed the hello-world application to AWS:
Access the endpoint URL using HTTPie:
$ http --body "https://5rwwylyo4k.execute-api.us-east-1.amazonaws.com/dev/hello-world"
This returns the following JSON:
"Accept-Encoding": "gzip, deflate",
"Via": "1.1 dd12e7e803f596deb3908675a4e017be.cloudfront.net
"X-Forwarded-For": "18.104.22.168, 22.214.171.124",
"message": "Go Serverless v1.0! Your function executed successfully!"
Entering the endpoint URL in your browser, (in my case as I am using Safari) shows you the RAW output:
Going to the URL mentioned at the very end of the serverless deploy command gives you an overview of the function you have deployed to Lambda using serverless:
Open the AWS Console by going to https://console.aws.amazon.com/, select Lambda from the Services menu, and then change to the region your function was launching in; this should show you your function:
At this point, you might scratch your head thinking, How was it launched in my account? I didn't provide any credentials! The serverless tool is designed to use the same credentials as the AWS CLI we installed before we launched our first Lambda function—these can be found at ~/.aws/credentials on your machine.
To remove the function, simply run:
$ serverless remove
And this will remove everything in your AWS account that the serverless toolkit has created.