diff --git a/python/application-load-balancer/app.py b/python/application-load-balancer/app.py index 40e0b866d..18b9cca02 100644 --- a/python/application-load-balancer/app.py +++ b/python/application-load-balancer/app.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +import base64 from aws_cdk import ( aws_autoscaling as autoscaling, aws_ec2 as ec2, @@ -13,6 +14,11 @@ def __init__(self, app: core.App, id: str) -> None: vpc = ec2.Vpc(self, "VPC") + data = open("./httpd.sh", "rb").read() + httpd=ec2.UserData.for_linux() + httpd.add_commands(str(data,'utf-8')) + + asg = autoscaling.AutoScalingGroup( self, "ASG", @@ -20,7 +26,8 @@ def __init__(self, app: core.App, id: str) -> None: instance_type=ec2.InstanceType.of( ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO ), - machine_image=ec2.AmazonLinuxImage(), + machine_image=ec2.AmazonLinuxImage(generation=ec2.AmazonLinuxGeneration.AMAZON_LINUX_2), + user_data=httpd, ) lb = elbv2.ApplicationLoadBalancer( @@ -33,6 +40,7 @@ def __init__(self, app: core.App, id: str) -> None: listener.connections.allow_default_port_from_any_ipv4("Open to the world") asg.scale_on_request_count("AModestLoad", target_requests_per_second=1) + core.CfnOutput(self,"LoadBalancer",export_name="LoadBalancer",value=lb.load_balancer_dns_name) app = core.App() diff --git a/python/application-load-balancer/httpd.sh b/python/application-load-balancer/httpd.sh new file mode 100644 index 000000000..15d86b332 --- /dev/null +++ b/python/application-load-balancer/httpd.sh @@ -0,0 +1,14 @@ +#!/bin/sh + +#install httpd +yum install httpd -y + +#enable and start httpd +systemctl enable httpd +systemctl start httpd +echo " Example Web Server" > /var/www/html/index.html +echo "" >> /var/www/html/index.html +echo "

Welcome AWS $(hostname -f)

" >> /var/www/html/index.html +echo "
" >> /var/www/html/index.html +curl http://169.254.169.254/latest/meta-data/instance-id >> /var/www/html/index.html +echo "
" >> /var/www/html/index.html