javav2/usecases/resilient_service/README.md
This example shows how to use the AWS SDK for Java (v2) to create a load-balanced web service that returns book, movie, and song recommendations. It shows how the service responds to failures, and how to restructure the service for more resilience when failures occur.
Several components are used to demonstrate the resilience of the example web service:
Each of these components is created and managed with the SDK for Java as part of an interactive demo that runs at a command prompt.
An Auto Scaling group starts EC2 instances in a specified set of Availability Zones. This example uses an Auto Scaling group to keep the number of running instances within a specified range and to make them available across multiple Availability Zones. The Auto Scaling group is set as a load balancer target so that HTTP requests are handled by a single endpoint and dispatched equally to the instances in the group.
An AWS Identity and Access Management (IAM) instance profile specifies permissions that are granted to EC2 instances created during the demo. When you associate an instance profile with an instance, AWS SDK code that runs on the instance can assume the profile's role to get the permissions that are specified by the role's attached policies.
An Amazon EC2 launch template specifies how instances are created. This example creates a launch template that specifies the instance type, Amazon Machine Image (AMI), instance profile, and a Bash script that runs when the instance is started. The Bash script installs required Python packages and starts a demo Python web server that listens for HTTP requests on port 80. The Python web server uses the SDK for Python to get recommendation data from a DynamoDB table and to get parameter values from Systems Manager to control the flow of the demonstration.
Elastic Load Balancing is used to distribute incoming HTTP traffic across multiple instances. This example creates an Application Load Balancer. It also adds a listener that forwards requests from the load balancer endpoint to the EC2 instances that are managed by the Auto Scaling group. The target group performs health checks on the instances and pulls unhealthy instances out of the rotation.
All the components used in this example need to access one another. Therefore, you must create all components in the same virtual private cloud (VPC). A VPC is a logically isolated network provided by Amazon Virtual Private Cloud (Amazon VPC). You can use the default VPC that's included with your account, or create a new VPC.
To access the load balancer endpoint, you must allow inbound traffic on port 80 from your computer's IP address to your VPC. If this rule doesn't exist, the example tries to add it. Alternately, you can add a rule to the default security group for your VPC and specify your computer's IP address as a source.
To complete the tutorial, you need the following:
This starts an interactive scenario that walks you through several aspects of creating a resilient web service and lets you send requests to the load balancer endpoint and verify instance health along the way. You can run this example in the Java IDE.
Use the SDK for Java to create the following AWS resources:
This part of the example demonstrates resiliency by simulating several kinds of failures. It uses Systems Manager parameters to update how the web server responds to requests. It also uses health checks to show how to make your web server more resilient to failure.
Along with recommendations returned by the DynamoDB table, the web service includes the instance ID and Availability Zone so you can see how the load balancer distributes requests among the instances in the Auto Scaling group.
The scenario takes the following steps:
Use the SDK for Java to clean up all resources created for this example.
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0