From a60259007dc4bbe9d7b26b3d64a28fcf0ecaae76 Mon Sep 17 00:00:00 2001 From: Laren-AWS Date: Wed, 11 Oct 2023 15:35:41 -0700 Subject: [PATCH 1/4] Use EC2 instance metadata to get the region instead of passing in a hardcoded value. --- python/cross_service/resilient_service/auto_scaler.py | 2 +- python/cross_service/resilient_service/test/resources/server.py | 2 +- workflows/resilient_service/resources/server.py | 2 +- workflows/resilient_service/resources/server_startup_script.sh | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/python/cross_service/resilient_service/auto_scaler.py b/python/cross_service/resilient_service/auto_scaler.py index 03aa3637ed8..25147298c5d 100644 --- a/python/cross_service/resilient_service/auto_scaler.py +++ b/python/cross_service/resilient_service/auto_scaler.py @@ -200,7 +200,7 @@ def replace_instance_profile( inst_ready = True self.ssm_client.send_command( InstanceIds=[instance_id], DocumentName='AWS-RunShellScript', - Parameters={'commands': ['cd / && sudo python3 server.py 80 us-west-2']}) + Parameters={'commands': ['cd / && sudo python3 server.py 80']}) log.info("Restarted the Python web server on instance %s.", instance_id) except ClientError as err: raise AutoScalerError( diff --git a/python/cross_service/resilient_service/test/resources/server.py b/python/cross_service/resilient_service/test/resources/server.py index 8c404005888..00d2f719c3a 100644 --- a/python/cross_service/resilient_service/test/resources/server.py +++ b/python/cross_service/resilient_service/test/resources/server.py @@ -115,7 +115,7 @@ def run(): """ parser = argparse.ArgumentParser() parser.add_argument('port', default=80, type=int, help="The port where the HTTP server listens.") - parser.add_argument('region', default='us-west-2', help="The AWS Region of AWS resources used by this example.") + parser.add_argument('--region', default=ec2_metadata.region, help="The AWS Region of AWS resources used by this example.") args = parser.parse_args() server_port = args.port diff --git a/workflows/resilient_service/resources/server.py b/workflows/resilient_service/resources/server.py index 8c404005888..00d2f719c3a 100644 --- a/workflows/resilient_service/resources/server.py +++ b/workflows/resilient_service/resources/server.py @@ -115,7 +115,7 @@ def run(): """ parser = argparse.ArgumentParser() parser.add_argument('port', default=80, type=int, help="The port where the HTTP server listens.") - parser.add_argument('region', default='us-west-2', help="The AWS Region of AWS resources used by this example.") + parser.add_argument('--region', default=ec2_metadata.region, help="The AWS Region of AWS resources used by this example.") args = parser.parse_args() server_port = args.port diff --git a/workflows/resilient_service/resources/server_startup_script.sh b/workflows/resilient_service/resources/server_startup_script.sh index 68db90cedb9..17d5472b5b1 100644 --- a/workflows/resilient_service/resources/server_startup_script.sh +++ b/workflows/resilient_service/resources/server_startup_script.sh @@ -4,4 +4,4 @@ sleep 30 # prevent "Error: Rpmdb changed underneath us" yum install python-pip -y python3 -m pip install boto3 ec2-metadata wget -O server.py https://raw.githubusercontent.com/awsdocs/aws-doc-sdk-examples/main/workflows/resilient_service/resources/server.py -python3 server.py 80 us-west-2 +python3 server.py 80 From e2eb43205b31bdb2278ce7e01768563834cc8afa Mon Sep 17 00:00:00 2001 From: Laren-AWS Date: Wed, 11 Oct 2023 15:38:11 -0700 Subject: [PATCH 2/4] Use branch version of server.py for testing. --- workflows/resilient_service/resources/server_startup_script.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/workflows/resilient_service/resources/server_startup_script.sh b/workflows/resilient_service/resources/server_startup_script.sh index 17d5472b5b1..fef2813dc39 100644 --- a/workflows/resilient_service/resources/server_startup_script.sh +++ b/workflows/resilient_service/resources/server_startup_script.sh @@ -3,5 +3,6 @@ yum -y update sleep 30 # prevent "Error: Rpmdb changed underneath us" yum install python-pip -y python3 -m pip install boto3 ec2-metadata -wget -O server.py https://raw.githubusercontent.com/awsdocs/aws-doc-sdk-examples/main/workflows/resilient_service/resources/server.py +wget -O server.py https://raw.githubusercontent.com/Laren-AWS/aws-doc-sdk-examples/fix-resilient-service-region/workflows/resilient_service/resources/server.py +# wget -O server.py https://raw.githubusercontent.com/awsdocs/aws-doc-sdk-examples/main/workflows/resilient_service/resources/server.py python3 server.py 80 From 763b98d227c09b9ad4b9524d286dc0ea2f0b2237 Mon Sep 17 00:00:00 2001 From: Laren-AWS Date: Wed, 11 Oct 2023 16:25:06 -0700 Subject: [PATCH 3/4] Get server script from main. --- workflows/resilient_service/resources/server_startup_script.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/workflows/resilient_service/resources/server_startup_script.sh b/workflows/resilient_service/resources/server_startup_script.sh index fef2813dc39..17d5472b5b1 100644 --- a/workflows/resilient_service/resources/server_startup_script.sh +++ b/workflows/resilient_service/resources/server_startup_script.sh @@ -3,6 +3,5 @@ yum -y update sleep 30 # prevent "Error: Rpmdb changed underneath us" yum install python-pip -y python3 -m pip install boto3 ec2-metadata -wget -O server.py https://raw.githubusercontent.com/Laren-AWS/aws-doc-sdk-examples/fix-resilient-service-region/workflows/resilient_service/resources/server.py -# wget -O server.py https://raw.githubusercontent.com/awsdocs/aws-doc-sdk-examples/main/workflows/resilient_service/resources/server.py +wget -O server.py https://raw.githubusercontent.com/awsdocs/aws-doc-sdk-examples/main/workflows/resilient_service/resources/server.py python3 server.py 80 From eb8c6e81f5b3d5738286dcf6f66be4d831a3742d Mon Sep 17 00:00:00 2001 From: Laren-AWS Date: Thu, 12 Oct 2023 08:53:34 -0700 Subject: [PATCH 4/4] Remove region from spec. --- workflows/resilient_service/SPECIFICATION.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/workflows/resilient_service/SPECIFICATION.md b/workflows/resilient_service/SPECIFICATION.md index eb1e87e97ac..80463c091e6 100644 --- a/workflows/resilient_service/SPECIFICATION.md +++ b/workflows/resilient_service/SPECIFICATION.md @@ -675,7 +675,7 @@ Select an instance, replace its instance profile, and reboot the instance. self.ssm_client.send_command( InstanceIds=[instance_id], DocumentName='AWS-RunShellScript', - Parameters={'commands': ['cd / && sudo python3 server.py 80 us-west-2']}) + Parameters={'commands': ['cd / && sudo python3 server.py 80']}) ``` Tell the user all about it: