[version_1.0]

Note

The exercises in this course will have an associated charge in your AWS account. In this exercise, you will create the following resources:

This exercise includes instructions to delete all the resources that you create in the exercises.

Familiarize yourself with AWS X-Ray pricing, Amazon API Gateway pricing, and the AWS Free Tier.

Exercise 6: Optimizing the Application

In this exercise, you enable AWS X-Ray on the ListDragons AWS Lambda function. You view those traces, and then enable X-Ray on API Gateway.

Next, you can optionally enable API caching in API Gateway. With API Gateway caching, you will make fewer requests to your backend AWS Lambda functions. Using this feature optimizes for latency, and potentially for cost (when you factor in the costs of your backend infrastructure).

Task 1: Updating Lambda roles

In this task, you add additional permissions to your Lambda roles. The ListDragons function will send traces to the X-Ray service, so the AWSXrayWriteOnlyAccess policy must allow these requests.

  1. In the console, open the AWS Identity and Access Management (IAM) dashboard.

  2. In the navigation pane, choose Roles.

  3. In the Search box, enter dragons.

  4. In the search results, choose the dragons-read-lambda-role link.

  5. Select Add permissions > Attach policies.

  6. In the Search box, enter AWSXrayWriteOnlyAccess and press Enter.

  7. In the search results, select AWSXrayWriteOnlyAccess.

  8. Choose Attach policies.

Task 2: Adding X-Ray to the Lambda function

In this task, you will update your ListDragons Lambda function to integrate the tracing features of X-Ray.

  1. In the AWS Cloud9 or local IDE terminal, list the contents of the ListDragons source directory.

  2. In the AWS Cloud9 or local IDE, open the listDragons.py source file. If you are using AWS Cloud9, you can open the file from the terminal. In the terminal, choose listDragons.py and select Open.

    Open option in the menu for listDragons.py
    Open option in the menu for listDragons.py
  3. Locate the line of code that imports the JSON library: import json. Immediately after this line, add the following code and save the file.

    This code imports the X-Ray SDK for Python, and makes a call to patch_all() to patch all supported libraries. You can learn more about the patching process in Patching libraries to instrument downstream calls in the AWS X-Ray Developer Guide.

  4. In the terminal, change to the ListDragons directory, and install the X-Ray SDK.

  5. Create a new .zip package for the updated ListDragons function, and update the function in Lambda by using the following AWS CLI command: aws lambda update-function-code.

Now, you configure the Lambda function.

  1. In the console, open the Lambda dashboard and make sure that you are on the Functions page.

  2. Choose the ListDragons Lambda function, and then choose the Configuration tab.

  3. On the left menu, choose Monitoring and operations tools, and then choose Edit.

  4. Under AWS X-Ray, enable Active tracing.

  5. Choose Save.

  6. Choose the Test tab.

  7. Generate some trace data by choosing Test a few times.

Task 3: Viewing the X-Ray traces

In this task, you will use the X-Ray console to view the traces from your ListDragons function.

  1. In the console, open the X-Ray dashboard.

  2. In the navigation pane, choose Traces. By default, it will display traces that were captured in the past 5 minutes.

  3. From the Trace list, choose a trace.

    You should see a trace map that X-Ray built from the requests in your application.

    Request trace map for ListDragons
    Request trace map for ListDragons
    Trace map request information
    Trace map request information

Task 4: Enabling X-Ray tracing in API Gateway

In this task, you will enable X-Ray tracing in your Amazon API Gateway REST API. You should be able to see a full trace of the application, originating from a browser request.

  1. In the console, open the API Gateway dashboard.

  2. Choose your DragonsApp API.

  3. In the API navigation pane, under API: DragonsApp, choose Stages.

  4. In the Stages navigation pane, choose the prod stage.

  5. On the right, in the prod Stage Editor, choose the Logs/Tracing tab.

  6. Select Enable X-Ray Tracing and choose Save Changes.

  7. Visit the URL for your Dragons application. If you don’t have it bookmarked, the URL will be https://<MY-BUCKET>.s3.amazonaws.com/dragonsapp/index.html. Replace <MY-BUCKET> with the unique bucket that you created previously.

    The application remembers your Dragons endpoint, Amazon Cognito domain, and Amazon Cognito client ID. If they aren’t available, you can retrieve them from previous exercises.

  8. In the web application, choose Login. The login flow will update the Amazon Cognito ID token in your application.

  9. After you complete the login, choose List and then choose GET /dragons.

  10. In the console, return to the X-Ray dashboard.

  11. In the navigation pane, choose Traces.

    You should see the most recent traces. Note that the traces now contain the details that were added by X-Ray, such as HTTP method, URL, and client IP address.

    Trace list
    Trace list
Trace example
Trace example

Task 5: (Optional) Enabling REST API caching in API Gateway

This final exercise includes instructions to delete all the resources created in the exercises.

Note

API Gateway caching is no longer eligible for the AWS Free Tier. This task is optional.

In this task, you will enable caching in your API Gateway stage. You can observe the benefits of caching by inspecting X-Ray traces after you enable the cache.

In this example, you will cache the results of the ListDragons function for 5 minutes. Thus, your clients will potentially see data from up to 5 minutes ago.

For the Dragons application, the sightings are infrequent. However, the List Dragons screen shows significant activity, so you have decided to experiment with caching.

  1. In the console, open the API Gateway dashboard.

  2. Choose your DragonsApp API.

  3. In the API Gateway navigation pane, under API: DragonsApp, choose Stages.

  4. In the Stages navigation pane, choose the prod stage.

  5. On the Settings tab, under Cache Settings, select Enable API cache.

  6. For Cache capacity, select 0.5GB.

  7. Choose Save Changes.

    It takes a few minutes to create the cache instance for API Gateway. Refresh the page every few minutes until you see the Cache status on the Settings tab change to AVAILABLE.

  8. Return to your Dragons application, and choose the GET /dragons button several again.

  9. In the console, return to the X-Ray dashboard.

  10. Observe the most recent traces that you initiated from the Dragons application. Sort the traces by age so that you can compare them.

    Trace list after caching
    Trace list after caching
  11. Compare the earliest trace and the most recent trace.

    The earliest trace includes the calls to your backend Lambda functions, which were used to populate the cache.

    Earliest trace
    Earliest trace

    The most recent trace shows you a request that was served from your API Gateway cache.

    Most recent trace
    Most recent trace

Task 6: Deleting all lab resources

Congratulations! You have successfully completed the course project.

In this task, you will log in to your AWS account as an administrator and delete the AWS resources that you created for this project.

  1. If needed, log in to the console as an administrator.

  2. Delete the API Gateway DragonsApp API.
  3. Delete the Amazon Cognito domain and user pool that you created.
  4. Delete the S3 bucket for the Dragons application.
  5. Delete the IAM policies that you created.
  6. Delete the IAM roles that you created.
  7. Delete the Lambda functions for the Dragons application.
  8. Delete the AWS Cloud9 development environment for this project.
  9. Delete the parameters that you stored in the AWS Systems Manager Parameter Store.
  10. Delete the AWS Step Functions state machine that you created.