Skip to content
Merged
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
38 changes: 34 additions & 4 deletions python/application-load-balancer/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
aws_autoscaling as autoscaling,
aws_ec2 as ec2,
aws_elasticloadbalancingv2 as elbv2,
aws_certificatemanager as acm,
App, CfnOutput, Stack
)

Expand All @@ -12,13 +13,15 @@ class LoadBalancerStack(Stack):
def __init__(self, app: App, id: str) -> None:
super().__init__(app, id)

# Create a VPC for our infrastructure
vpc = ec2.Vpc(self, "VPC")

# Read and prepare user data script for EC2 instances
data = open("./httpd.sh", "rb").read()
httpd=ec2.UserData.for_linux()
httpd.add_commands(str(data,'utf-8'))


# Create an Auto Scaling Group with EC2 instances
asg = autoscaling.AutoScalingGroup(
self,
"ASG",
Expand All @@ -30,19 +33,46 @@ def __init__(self, app: App, id: str) -> None:
user_data=httpd,
)

# Create an Application Load Balancer
lb = elbv2.ApplicationLoadBalancer(
self, "LB",
vpc=vpc,
internet_facing=True)

listener = lb.add_listener("Listener", port=80)
listener.add_targets("Target", port=80, targets=[asg])
listener.connections.allow_default_port_from_any_ipv4("Open to the world")
# Create HTTP listener with redirect
http_listener = lb.add_listener(
"HttpListener",
port=80,
default_action=elbv2.ListenerAction.redirect(
port="443",
protocol="HTTPS",
permanent=True,
host="#{host}",
path="/#{path}",
query="#{query}"
)
)

# Create HTTPS listener
https_listener = lb.add_listener(
"HttpsListener",
port=443,
certificates=[elbv2.ListenerCertificate.from_arn("certificate_arn")],
ssl_policy=elbv2.SslPolicy.RECOMMENDED
)

# Add target group to HTTPS listener
https_listener.add_targets("Target", port=80, targets=[asg])
https_listener.connections.allow_default_port_from_any_ipv4("Open to the world")

# Configure Auto Scaling based on request count
asg.scale_on_request_count("AModestLoad", target_requests_per_minute=60)

# Output the Load Balancer DNS name
CfnOutput(self,"LoadBalancer",export_name="LoadBalancer",value=lb.load_balancer_dns_name)


# Initialize the CDK app and create the stack
app = App()
LoadBalancerStack(app, "LoadBalancerStack")
app.synth()
Loading