Skip to content

Commit

Permalink
feat: make Python load balancer example start an actual HTTP server (a…
Browse files Browse the repository at this point in the history
stevensu1977 authored and rix0rrr committed Sep 17, 2019
1 parent 62ab40c commit 1201124
Showing 2 changed files with 23 additions and 1 deletion.
10 changes: 9 additions & 1 deletion python/application-load-balancer/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python3
import base64
from aws_cdk import (
aws_autoscaling as autoscaling,
aws_ec2 as ec2,
@@ -13,14 +14,20 @@ 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",
vpc=vpc,
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()
14 changes: 14 additions & 0 deletions python/application-load-balancer/httpd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh

#install httpd
yum install httpd -y

#enable and start httpd
systemctl enable httpd
systemctl start httpd
echo "<html><head><title> Example Web Server</title></head>" > /var/www/html/index.html
echo "<body>" >> /var/www/html/index.html
echo "<div><center><h2>Welcome AWS $(hostname -f) </h2>" >> /var/www/html/index.html
echo "<hr/>" >> /var/www/html/index.html
curl http://169.254.169.254/latest/meta-data/instance-id >> /var/www/html/index.html
echo "</center></div></body></html>" >> /var/www/html/index.html

0 comments on commit 1201124

Please sign in to comment.