Serverless Architectures with Kubernetes
上QQ阅读APP看书,第一时间看更新

The Serverless Framework

The Serverless Framework was announced in 2015 with the name JavaScript Amazon Web Services (JAWS). It was initially developed in Node.js to make it easier for people to develop AWS Lambda functions. In the same year, it changed the name to Serverless Framework and expanded its scope to other cloud providers and serverless frameworks, including Google Cloud Functions, Azure Functions, Apache OpenWhisk, Fn, and many more.

Serverless Framework is open source, and its source code is available at GitHub: https://github.com/serverless/serverless. It is a very popular repository with more than 31,000 stars, as shown in the following screenshot:

Figure 3.20: Serverless Framework GitHub repository

The official website of the framework is available at https://serverless.com and provides extensive documentation, use cases, and examples. The main features of the Serverless Framework can be grouped into four main topics:

  • Cloud-agnostic: The Serverless Framework aims to create a cloud-agnostic serverless application development environment so that vendor lock-in is not a concern.
  • Reusable Components: Serverless functions that are developed in the Serverless Framework are open source and available. These components help us to create complex applications quickly.
  • Infrastructure-as-code: All the configuration and source code that's developed in the Serverless Framework is explicitly defined and can be deployed with a single command.
  • Developer Experience: The Serverless Framework aims to enhance developer experience via its CLI, configuration parameters, and active community.

These four characteristics of the Serverless Framework make it the most well-known framework for creating serverless applications in the cloud. In addition, the framework focuses on the management of the complete life cycle of serverless applications:

  • Develop: It is possible to develop apps locally and reuse open source plugins via the framework CLI.
  • Deploy: The Serverless Framework can deploy to multiple cloud platforms and roll out and roll back versions from development to production.
  • Test: The framework supports testing the functions out of the box by using the command-line client functions.
  • Secure: The framework handles secrets for running the functions and cloud-specific authentication keys for deployments.
  • Monitor: The metrics and logs of the serverless applications are available with the serverless runtime and client tools.

In the following exercise, a serverless application will be created, configured, and deployed to AWS using the Serverless Framework. The framework will be used inside a Docker container to show how easy it is to get started with serverless applications.

Note

The Serverless Framework can be downloaded and installed to a local computer with npm. A Docker container, including the Serverless Framework installation, will be used in the following exercise so that we have a fast and reproducible setup.

In the following exercise, the hello-world function will be deployed to AWS Lambda using the Serverless Framework. In order to complete this exercise, you need to have an active Amazon Web Services account. You can create an account at https://aws.amazon.com/.

Exercise 9: Running Functions with the Serverless Framework

In this exercise, we aim to configure the Serverless Framework and deploy our very first function using it. With the Serverless Framework, it is possible to create cloud-agnostic serverless applications. In this exercise, we will deploy the functions to AWS Lambda. However, it is possible to deploy the same functions to different cloud providers.

To successfully complete this exercise, we need to ensure that the following steps are executed:

  1. In your Terminal, run the following command to start the Serverless Framework development environment:

    docker run -it --entrypoint=bash onuryilmaz/serverless

    This command will start a Docker container in interactive mode. In the following steps, actions will be taken inside this Docker container, as shown in the following screenshot:

    Figure 3.21: Starting a Docker container for serverless
  2. Run the following command to check the framework version:

    serverless version

    This command lists the Framework, Plugin, and SDK versions, and getting a complete output indicates that everything is set up correctly, as shown in the following screenshot:

    Figure 3.22: Framework version
  3. Run the following command to use the framework interactively:

    serverless

    Press Y to create a new project and choose AWS Node.js from the dropdown, as shown in the following screenshot:

    Figure 3.23: Creating a new project in the framework
  4. Set the name of the project to hello-world and press Enter. The output is as follows:
    Figure 3.24: Successful creation of the project
  5. Press Y for the AWS credential setup question, and then press Y again for the Do you have an AWS account? question. The output will be as follows:
    Figure 3.25: AWS account setup

    You now have a URL for creating a serverless user. Copy and save the URL; we'll need it later.

  6. Open the URL from Step 4 in your browser and start adding users to the AWS console. The URL will open the Add user screen with predefined selections. Click Next: Permissions at the end of the screen, as shown in the following screenshot:
    Figure 3.26: AWS Add user
  7. The AdministratorAccess policy should be selected automatically. Click Next: Tags at the bottom of the screen, as shown in the following screenshot:
    Figure 3.27: AWS Add user – Permissions
  8. If you want to tag your users, you can add optional tags in this view. Click Next: Review, as shown in the following screenshot:
    Figure 3.28: AWS Add user – Tags
  9. This view shows the summary of the new user. Click Create User, as shown in the following screenshot:
    Figure 3.29: AWS Add user – Review

    You will be redirected to a success page with an Access Key ID and secret, as shown in the following screenshot:

    Figure 3.30: AWS Add user – Success
  10. Copy the key ID and secret access key so that you can use it in the following steps of this exercise and the activity for this chapter. You need to click Show to reveal the secret access key.
  11. Return to your Terminal and press Enter to enter the key ID and secret information, as shown in the following screenshot:
    Figure 3.31: AWS Credentials in the framework
  12. Press Y for the Serverless account enable question and select register from the dropdown, as shown in the following screenshot:
    Figure 3.32: Serverless account enabled
  13. Write your email and a password to create a Serverless Framework account, as shown in the following screenshot:
    Figure 3.33: Serverless account register
  14. Run the following commands to change the directory and deploy the function:

    cd hello-world

    serverless deploy -v

    These commands will make the Serverless Framework deploy the function into AWS, as shown in the following screenshot:

    Figure 3.34: Serverless Framework deployment output

    Note

    The output logs start by packaging the service and creating AWS resources for the source code, artifacts, and functions. After all the resources have been created, the Service Information section will provide a summary of the functions and URLs.

    At the end of the screen, you will find the Serverless Dashboard URL for the deployed function, as shown in the following screenshot:

    Figure 3.35: Stack Outputs

    Copy the dashboard URL so that you can check the function metrics in the upcoming steps.

  15. Invoke the function by using the following command in your Terminal:

    serverless invoke --function hello

    This command invokes the deployed function and prints out the response, as shown in the following screenshot:

    Figure 3.36: Function output

    As the output shows, statusCode is 200, and the body of the response indicates that the function has responded successfully.

  16. Open the Serverless Dashboard URL you copied at the end of Step 8 into your browser, as shown in the following screenshot:
    Figure 3.37: Serverless Dashboard login
  17. Log in with the email and password you created in Step 5.

    You will be redirected to the application list. Expand hello-world-app and click on the successful deployment line, as shown in the following screenshot:

    Figure 3.38: Serverless Dashboard application list

    In the function view, all the runtime information, including API endpoints, variables, alerts, and metrics, are available. Scroll down to see the number of invocations. The output should be as follows:

    Figure 3.39: Serverless Dashboard function view

    Since we have only invoked the function once, you will only see 1 in the charts.

  18. Return to your Terminal and delete the function with the following command:

    serverless remove

    This command will remove the deployed function and all its dependencies, as shown in the following screenshot:

Figure 3.40: Removing the function

Exit the Serverless Framework development environment container by writing exit in the Terminal, as shown in the following screenshot:

Figure 3.41: Exiting the container

In this exercise, we have created, configured, and deployed a serverless function using the Serverless Framework. Furthermore, the function is invoked via a CLI, and its metrics are checked from the Serverless Dashboard. The Serverless Framework creates a comprehensive abstraction for cloud providers so that it is only passed as credentials to the platform. In other words, where to deploy is just a matter of configuration with the help of serverless frameworks.

In the following activity, a real-life serverless daily weather application will be developed. You will create a serverless framework application with an invocation schedule and deploy it to a cloud provider. In addition, the weather status messages will be sent to a cloud-based collaboration tool known as Slack.

Note

In order to complete the following activity, you need to be able to access a Slack workplace. You can use your existing Slack workspace or create a new one for free at https://slack.com/create.

Activity 3: Daily Weather Status Function for Slack

The aim of this activity is to create a real-life serverless application that sends weather status messages in specific Slack channels. The function will be developed with the Serverless Framework so that it can run on multiple cloud platforms in the future. The function will be designed to run at particular times for your team so that they're informed about the weather status, such as early in the morning before their morning commute. These messages will be published on Slack channels, which is the main communication tool within the team.

In order to get the weather status to share within the team, you can use wttr.in (https://github.com/chubin/wttr.in), which is a free-to-use weather data provider. Once completed, you will have deployed a function to a cloud provider, namely, AWS Lambda:

Figure 3.42: Daily weather function

Finally, when the scheduler invokes the function, or when you invoke it manually, you will get messages regarding the current weather status in your Slack channel:

Figure 3.43: Slack message with the current weather status

Note

In order to complete this activity, you should configure Slack by following the Slack setup steps.

Slack Setup

Execute the following steps to configure Slack:

  1. In your Slack workspace, click your username and select Customize Slack
  2. Click Configure apps in the opened window. 
  3. Click on Browse the App Directory to add a new application from the directory.
  4. Find Incoming WebHooks from the search box in App Directory. 
  5. Click on Set Up for the Incoming WebHooks application.
  6. Fill in the configuration for incoming webhooks with your specific channel name and icon. 
  7. Open your Slack workspace and the channel you configured in Step 6 to be able to check the integration message.

    Note

    Detailed screenshots of the Slack setup steps can be found on page 387.

Execute the following steps to complete this activity.

  1. In your Terminal, create a Serverless Framework application structure in a folder called daily-weather.
  2. Create a package.json file to define the Node.js environment in the daily-weather folder.
  3. Create a handler.js file to implement the actual functionality in the daily-weather folder.
  4. Install the Node.js dependencies for the serverless application.
  5. Export the AWS credentials as environment variables.
  6. Deploy the serverless application to AWS using the Serverless Framework.
  7. Check AWS Lambda for the deployed functions in the AWS Console.
  8. Invoke the function with the Serverless Framework client tools.
  9. Check the Slack channel for the posted weather status.
  10. Return to your Terminal and delete the function with the Serverless Framework.
  11. Exit the Serverless Framework development environment container.

    Note

    The solution to this activity can be found on page 387.