Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update python application-load-balancer example #97

Merged
merged 2 commits into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Expand All @@ -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(
Expand All @@ -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()
Expand Down
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