diff --git a/terraform/java/ec2/asg/main.tf b/terraform/java/ec2/asg/main.tf index 41849a03c..775fa263d 100644 --- a/terraform/java/ec2/asg/main.tf +++ b/terraform/java/ec2/asg/main.tf @@ -152,6 +152,7 @@ resource "aws_autoscaling_group" "asg" { max_size = 1 launch_configuration = aws_launch_configuration.launch_configuration.name vpc_zone_identifier = [data.aws_subnets.default_subnets.ids.0] + health_check_type = "EC2" } resource "aws_instance" "remote_service_instance" { @@ -215,6 +216,21 @@ resource "null_resource" "remote_service_setup" { # The application needs time to come up and reach a steady state, this should not take longer than 30 seconds sleep 30 + # Check if the application is up. If it is not up, then exit 1. + attempt_counter=0 + max_attempts=30 + until $(curl --output /dev/null --silent --head --fail --max-time 5 $(echo "http://localhost:8080/healthcheck" | tr -d '"')); do + if [ $attempt_counter -eq $max_attempts ];then + echo "Failed to connect to endpoint." + exit 1 + fi + echo "Attempting to connect to the remote endpoint. Tried $attempt_counter out of $max_attempts" + attempt_counter=$(($attempt_counter+1)) + sleep 10 + done + + echo "Successfully connected to remote endpoint" + EOF ] } diff --git a/terraform/java/ec2/default/main.tf b/terraform/java/ec2/default/main.tf index ffdc38b58..ba667e631 100644 --- a/terraform/java/ec2/default/main.tf +++ b/terraform/java/ec2/default/main.tf @@ -138,6 +138,21 @@ resource "null_resource" "main_service_setup" { # The application needs time to come up and reach a steady state, this should not take longer than 30 seconds sleep 30 + # Check if the application is up. If it is not up, then exit 1. + attempt_counter=0 + max_attempts=30 + until $(curl --output /dev/null --silent --head --fail --max-time 5 $(echo "http://localhost:8080" | tr -d '"')); do + if [ $attempt_counter -eq $max_attempts ];then + echo "Failed to connect to endpoint." + exit 1 + fi + echo "Attempting to connect to the main endpoint. Tried $attempt_counter out of $max_attempts" + attempt_counter=$(($attempt_counter+1)) + sleep 10 + done + + echo "Successfully connected to main endpoint" + EOF ] } @@ -206,6 +221,21 @@ resource "null_resource" "remote_service_setup" { # The application needs time to come up and reach a steady state, this should not take longer than 30 seconds sleep 30 + # Check if the application is up. If it is not up, then exit 1. + attempt_counter=0 + max_attempts=30 + until $(curl --output /dev/null --silent --head --fail --max-time 5 $(echo "http://localhost:8080/healthcheck" | tr -d '"')); do + if [ $attempt_counter -eq $max_attempts ];then + echo "Failed to connect to endpoint." + exit 1 + fi + echo "Attempting to connect to the remote endpoint. Tried $attempt_counter out of $max_attempts" + attempt_counter=$(($attempt_counter+1)) + sleep 10 + done + + echo "Successfully connected to remote endpoint" + EOF ] } diff --git a/terraform/python/ec2/asg/main.tf b/terraform/python/ec2/asg/main.tf index 96dc8dd0c..bc40a0802 100644 --- a/terraform/python/ec2/asg/main.tf +++ b/terraform/python/ec2/asg/main.tf @@ -166,6 +166,7 @@ resource "aws_autoscaling_group" "asg" { max_size = 1 launch_configuration = aws_launch_configuration.launch_configuration.name vpc_zone_identifier = [data.aws_subnets.default_subnets.ids.0] + health_check_type = "EC2" } resource "aws_instance" "remote_service_instance" { @@ -241,6 +242,21 @@ resource "null_resource" "remote_service_setup" { # The application needs time to come up and reach a steady state, this should not take longer than 30 seconds sleep 30 + # Check if the application is up. If it is not up, then exit 1. + attempt_counter=0 + max_attempts=30 + until $(curl --output /dev/null --silent --head --fail --max-time 5 $(echo "http://localhost:8001/healthcheck" | tr -d '"')); do + if [ $attempt_counter -eq $max_attempts ];then + echo "Failed to connect to endpoint." + exit 1 + fi + echo "Attempting to connect to the remote endpoint. Tried $attempt_counter out of $max_attempts" + attempt_counter=$(($attempt_counter+1)) + sleep 10 + done + + echo "Successfully connected to remote endpoint" + EOF ] } diff --git a/terraform/python/ec2/default/main.tf b/terraform/python/ec2/default/main.tf index 31446114d..8e2ddf821 100644 --- a/terraform/python/ec2/default/main.tf +++ b/terraform/python/ec2/default/main.tf @@ -104,48 +104,68 @@ resource "null_resource" "main_service_setup" { provisioner "remote-exec" { inline = [ + <<-EOF + #!/bin/bash + # Install Python and wget - "sudo yum install wget -y", - "sudo yum install unzip -y", - "sudo dnf install -y python3.9", - "sudo dnf install -y python3.9-pip", + sudo yum install wget -y + sudo yum install unzip -y + sudo dnf install -y python3.9 + sudo dnf install -y python3.9-pip # Copy in CW Agent configuration - "agent_config='${replace(replace(file("./amazon-cloudwatch-agent.json"), "/\\s+/", ""), "$REGION", var.aws_region)}'", - "echo $agent_config > amazon-cloudwatch-agent.json", + agent_config='${replace(replace(file("./amazon-cloudwatch-agent.json"), "/\\s+/", ""), "$REGION", var.aws_region)}' + echo $agent_config > amazon-cloudwatch-agent.json # Get and run CW agent rpm - "${var.get_cw_agent_rpm_command}", - "sudo rpm -U ./cw-agent.rpm", - "sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:./amazon-cloudwatch-agent.json", + ${var.get_cw_agent_rpm_command} + sudo rpm -U ./cw-agent.rpm + sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:./amazon-cloudwatch-agent.json # Get ADOT Wheel and install it - "${var.get_adot_wheel_command}", + ${var.get_adot_wheel_command} # Get and run the sample application with configuration - "aws s3 cp ${var.sample_app_zip} ./python-sample-app.zip", - "unzip -o python-sample-app.zip", + aws s3 cp ${var.sample_app_zip} ./python-sample-app.zip + unzip -o python-sample-app.zip # Export environment variables for instrumentation - "cd ./django_frontend_service", - "python3.9 -m pip install -r ec2-requirements.txt", - "export DJANGO_SETTINGS_MODULE=\"django_frontend_service.settings\"", - "export OTEL_PYTHON_DISTRO=\"aws_distro\"", - "export OTEL_PYTHON_CONFIGURATOR=\"aws_configurator\"", - "export OTEL_METRICS_EXPORTER=none", - "export OTEL_TRACES_EXPORTER=otlp", - "export OTEL_AWS_APPLICATION_SIGNALS_ENABLED=true", - "export OTEL_AWS_APPLICATION_SIGNALS_EXPORTER_ENDPOINT=http://localhost:4315", - "export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://localhost:4315", - "export OTEL_EXPORTER_OTLP_TRACES_PROTOCOL=grpc", - "export OTEL_EXPORTER_OTLP_METRICS_PROTOCOL=grpc", - "export OTEL_SERVICE_NAME=python-sample-application-${var.test_id}", - "export OTEL_TRACES_SAMPLER=always_on", - "python3.9 manage.py migrate", - "nohup opentelemetry-instrument python3.9 manage.py runserver 0.0.0.0:8000 --noreload &", + cd ./django_frontend_service + python3.9 -m pip install -r ec2-requirements.txt + export DJANGO_SETTINGS_MODULE="django_frontend_service.settings" + export OTEL_PYTHON_DISTRO="aws_distro" + export OTEL_PYTHON_CONFIGURATOR="aws_configurator" + export OTEL_METRICS_EXPORTER=none + export OTEL_TRACES_EXPORTER=otlp + export OTEL_AWS_APPLICATION_SIGNALS_ENABLED=true + export OTEL_AWS_APPLICATION_SIGNALS_EXPORTER_ENDPOINT=http://localhost:4315 + export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://localhost:4315 + export OTEL_EXPORTER_OTLP_TRACES_PROTOCOL=grpc + export OTEL_EXPORTER_OTLP_METRICS_PROTOCOL=grpc + export OTEL_SERVICE_NAME=python-sample-application-${var.test_id} + export OTEL_TRACES_SAMPLER=always_on + python3.9 manage.py migrate + nohup opentelemetry-instrument python3.9 manage.py runserver 0.0.0.0:8000 --noreload & # The application needs time to come up and reach a steady state, this should not take longer than 30 seconds - "sleep 30" + sleep 30 + + # Check if the application is up. If it is not up, then exit 1. + attempt_counter=0 + max_attempts=30 + until $(curl --output /dev/null --silent --head --fail --max-time 5 $(echo "http://localhost:8000" | tr -d '"')); do + if [ $attempt_counter -eq $max_attempts ];then + echo "Failed to connect to endpoint." + exit 1 + fi + echo "Attempting to connect to the main endpoint. Tried $attempt_counter out of $max_attempts" + attempt_counter=$(($attempt_counter+1)) + sleep 10 + done + + echo "Successfully connected to main endpoint" + + EOF ] } @@ -179,49 +199,68 @@ resource "null_resource" "remote_service_setup" { provisioner "remote-exec" { inline = [ + <<-EOF + #!/bin/bash + # Install Python and wget - "sudo yum install wget -y", - "sudo yum install unzip -y", - "sudo dnf install -y python3.9", - "sudo dnf install -y python3.9-pip", + sudo yum install wget -y + sudo yum install unzip -y + sudo dnf install -y python3.9 + sudo dnf install -y python3.9-pip # Copy in CW Agent configuration - "agent_config='${replace(replace(file("./amazon-cloudwatch-agent.json"), "/\\s+/", ""), "$REGION", var.aws_region)}'", - "echo $agent_config > amazon-cloudwatch-agent.json", + agent_config='${replace(replace(file("./amazon-cloudwatch-agent.json"), "/\\s+/", ""), "$REGION", var.aws_region)}' + echo $agent_config > amazon-cloudwatch-agent.json # Get and run CW agent rpm - "${var.get_cw_agent_rpm_command}", - "sudo rpm -U ./cw-agent.rpm", - "sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:./amazon-cloudwatch-agent.json", + ${var.get_cw_agent_rpm_command} + sudo rpm -U ./cw-agent.rpm + sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:./amazon-cloudwatch-agent.json # Get ADOT Wheel and install it - "${var.get_adot_wheel_command}", + ${var.get_adot_wheel_command} # Get and run the sample application with configuration - "aws s3 cp ${var.sample_app_zip} ./python-sample-app.zip", - "unzip -o python-sample-app.zip", + aws s3 cp ${var.sample_app_zip} ./python-sample-app.zip + unzip -o python-sample-app.zip # Export environment variables for instrumentation - "cd ./django_remote_service", - "export DJANGO_SETTINGS_MODULE=\"django_remote_service.settings\"", - "python3.9 -m pip install -r requirements.txt --force-reinstall", - "export OTEL_PYTHON_DISTRO=\"aws_distro\"", - "export OTEL_PYTHON_CONFIGURATOR=\"aws_configurator\"", - "export OTEL_METRICS_EXPORTER=none", - "export OTEL_TRACES_EXPORTER=otlp", - "export OTEL_AWS_APPLICATION_SIGNALS_ENABLED=true", - "export OTEL_AWS_APPLICATION_SIGNALS_EXPORTER_ENDPOINT=http://localhost:4315", - "export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://localhost:4315", - "export OTEL_EXPORTER_OTLP_TRACES_PROTOCOL=grpc", - "export OTEL_EXPORTER_OTLP_METRICS_PROTOCOL=grpc", - "export OTEL_SERVICE_NAME=python-sample-remote-application-${var.test_id}", - "export OTEL_TRACES_SAMPLER=always_on", - "python3.9 manage.py migrate", - "nohup opentelemetry-instrument python3.9 manage.py runserver 0.0.0.0:8001 --noreload &", - + cd ./django_remote_service + export DJANGO_SETTINGS_MODULE="django_remote_service.settings" + python3.9 -m pip install -r requirements.txt --force-reinstall + export OTEL_PYTHON_DISTRO="aws_distro" + export OTEL_PYTHON_CONFIGURATOR="aws_configurator" + export OTEL_METRICS_EXPORTER=none + export OTEL_TRACES_EXPORTER=otlp + export OTEL_AWS_APPLICATION_SIGNALS_ENABLED=true + export OTEL_AWS_APPLICATION_SIGNALS_EXPORTER_ENDPOINT=http://localhost:4315 + export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://localhost:4315 + export OTEL_EXPORTER_OTLP_TRACES_PROTOCOL=grpc + export OTEL_EXPORTER_OTLP_METRICS_PROTOCOL=grpc + export OTEL_SERVICE_NAME=python-sample-remote-application-${var.test_id} + export OTEL_TRACES_SAMPLER=always_on + python3.9 manage.py migrate + nohup opentelemetry-instrument python3.9 manage.py runserver 0.0.0.0:8001 --noreload & # The application needs time to come up and reach a steady state, this should not take longer than 30 seconds - "sleep 30" + sleep 30 + + # Check if the application is up. If it is not up, then exit 1. + attempt_counter=0 + max_attempts=30 + until $(curl --output /dev/null --silent --head --fail --max-time 5 $(echo "http://localhost:8001/healthcheck" | tr -d '"')); do + if [ $attempt_counter -eq $max_attempts ];then + echo "Failed to connect to endpoint." + exit 1 + fi + echo "Attempting to connect to the remote endpoint. Tried $attempt_counter out of $max_attempts" + attempt_counter=$(($attempt_counter+1)) + sleep 10 + done + + echo "Successfully connected to remote endpoint" + + EOF ] }