• Tidak ada hasil yang ditemukan

55Figure 3-15. Here the function uses the event coming in to output

Dalam dokumen Beginning Serverless Computing (Halaman 66-72)

ChApter 3 ■ AmAzoN WeB ServiCeS

55

ChApter 3 ■ AmAzoN WeB ServiCeS

These logs are helpful for analyzing and monitoring your function’s executions. In addition to

CloudWatch, there is a Monitoring tab in the Lambda function’s console that gives you a high-level overview of your function’s executions and outputs. To look deeper into a log, click on its stream. This will give you a full detailed log for that execution. Figure 3-17 shows the executions on my Hello World function.

Figure 3-16. The CloudWatch Log Group for your Lambda keeps your streams for your executions. You can also click on specific streams and investigate logs within that time frame.

ChApter 3 ■ AmAzoN WeB ServiCeS

57

Figure 3-17. The opened stream gives you detailed UTC time and messages. You can view logs in a particular stream as far back as a week.

Now that we have become acclimated to the Lambda console, test events, and CloudWatch, we will build upon our Hello World function with environment variables.

The code for the Hello World function can be found at https://github.com/mgstigler/hello-world.git.

Environment Variables

To add onto our Hello World function, we are going to cover environment variables: what they are, how they’re used, and how they’re configured in AWS.

What Are Environment Variables

Environment variables are those set globally across your serverless application. They are given a key and a value. The value is that of the actual variable, while the key is the name used for that variable throughout the application. The benefit of environment variables is both security and ease of use.

Rather than leaving API keys and various access information scattered throughout your code, you can assign the actual secure variable to an environment variable to be used anonymously. In addition, if you know you are going to be using a variable repeatedly, setting it as an environment variable allows you to access it across your project without re-declaring it. It also allows you to make quick changes in one spot.

To see how environment variables are used, we are going to implement them in our Hello World application.

ChApter 3 ■ AmAzoN WeB ServiCeS

Using Environment Variables in Hello World

We are going to create an environment variable with the key provider and value AWS. This also demonstrates a best practice of separating provider logic from your code to prevent vendor lock-in. While for this example we are just using the value AWS, later it could be used to represent different services. For instance, if we knew we wanted to access a database, we could use the key DB_Host and set the value specific to the AWS database hostname. This makes it easily configurable if we choose to move to a different cloud provider. Figure 3-18 shows where and how we have configured our environment variables.

Now we can access this environment variable within our code. Figure 3-19 demonstrates how we reference environment variables and the log output for the execution of the Lambda function.

var AWS = require(‘aws-sdk’);

exports.handler = (event, context, callback) => { var myProvider = process.env.provider;

callback(null, “The cloud provider for this demo is: “ + myProvider + “. The event received is: “ + JSON.stringify(event));

};

Figure 3-18. You can configure your environment variables within the AWS Lambda console

ChApter 3 ■ AmAzoN WeB ServiCeS

59

Figure 3-19. The environment variables are accessed through process.env.variable-key

This shows how easy it is to create and access variables in your code. Now that we have completed a Hello World demonstration of Lambda, we will look at creating a new application that uses an HTTP event and responds to it by returning data from DynamoDB.

HTTP Event

For our first fleshed-out serverless application with Lambda, we are going to use AWS API Gateway to trigger a Lambda function that returns data from a DynamoDB NoSQL Database. API Gateway allows you to create HTTP resources and methods and set them to specific endpoints.

This application will mimic a virtual recipe book. We will create an API Gateway with one resource, Recipes, and one method for that resource, GET. For the endpoint to this GET request, we will set a Lambda function we create, called GetRecipes. This function will access a DynamoDB table that we will have prepopulated with recipes and return a JSON value of these recipes as the response. Before setting up our API Gateway, we will go ahead and create our Lambda function, leaving the trigger and code blank for now.

Once you have done this, you can move on to exploring API Gateway.

ChApter 3 ■ AmAzoN WeB ServiCeS

Exploring API Gateway

API Gateway is an AWS service that lets you easily create and access an API all through the API Gateway console. It gives you a public RESTful API interface to a wide host of AWS services. This allows you to interact easily with your databases, messaging services, and Lambda functions through a secure gateway.

In addition, it is incredibly easy to set up and create endpoints for. This allows the continuation of rapid development. To begin using API Gateway, navigate to the API Gateway service under Application

Services. The API Gateway console shows your APIs, usage plans, API Keys, Custom Domain Names, Client Certificates, and Settings. Figure 3-20 shows an example of the API Gateway Console.

To create an API, simply click Create API. This will take you through the process of setting up your own API. We will create a New API and name it Recipe API. After creating your API, you should be directed to a console that allows you to configure your API (Figure 3-21). We want to add a resource to our API called

“recipes.”

Figure 3-20. With the API Gateway you can easily access and create APIs and set keys and usage plans for them

ChApter 3 ■ AmAzoN WeB ServiCeS

61

Dalam dokumen Beginning Serverless Computing (Halaman 66-72)