Skip to content

Commit 4770ee5

Browse files
kaiz-ioMichael Kaiser
andauthored
Fix(python/ecs/alb): Swap to LaunchTemplates (#1114)
* chore(cleanup):Clean out undeployable examples. Many updates needed * chore(cleanup):Clean out undeployable examples. Many updates needed * Fix(python/ecs/alb): Swap to launchtemplates Fixes #1086 --------- Co-authored-by: Michael Kaiser <[email protected]>
1 parent 3337710 commit 4770ee5

File tree

1 file changed

+29
-12
lines changed
  • python/ecs/ecs-service-with-advanced-alb-config

1 file changed

+29
-12
lines changed

python/ecs/ecs-service-with-advanced-alb-config/app.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,96 +3,113 @@
33
aws_ec2 as ec2,
44
aws_elasticloadbalancingv2 as elbv2,
55
aws_ecs as ecs,
6+
aws_iam as iam,
67
App, CfnOutput, Duration, Stack
78
)
89

10+
# Initialize the CDK app and stack
911
app = App()
1012
stack = Stack(app, "sample-aws-ec2-integ-ecs")
1113

12-
# Create a cluster
14+
# Create VPC with 2 Availability Zones
1315
vpc = ec2.Vpc(
1416
stack, "MyVpc",
1517
max_azs=2
1618
)
1719

20+
# Create ECS cluster in the VPC
1821
cluster = ecs.Cluster(
1922
stack, 'EcsCluster',
2023
vpc=vpc
2124
)
2225

26+
# Create Auto Scaling Group for ECS cluster using launchtemplates
27+
# Uses t3.micro instances with Amazon Linux 2 ECS-optimized AMI
2328
asg = autoscaling.AutoScalingGroup(
2429
stack, "DefaultAutoScalingGroup",
25-
instance_type=ec2.InstanceType.of(
30+
vpc=vpc,
31+
launch_template=ec2.LaunchTemplate(stack, "LaunchTemplate",
32+
instance_type=ec2.InstanceType.of(
2633
ec2.InstanceClass.BURSTABLE3,
2734
ec2.InstanceSize.MICRO),
28-
machine_image=ecs.EcsOptimizedImage.amazon_linux2(),
29-
vpc=vpc
35+
machine_image=ecs.EcsOptimizedImage.amazon_linux2023(),
36+
user_data=ec2.UserData.for_linux(),
37+
role=iam.Role(stack, "LaunchTemplateRole",
38+
assumed_by=iam.ServicePrincipal("ec2.amazonaws.com")
39+
)
40+
)
3041
)
3142

43+
# Create and add capacity provider to the cluster
3244
capacity_provider = ecs.AsgCapacityProvider(stack, "AsgCapacityProvider",
3345
auto_scaling_group=asg
3446
)
35-
3647
cluster.add_asg_capacity_provider(capacity_provider)
3748

38-
# Create Task Definition
49+
# Define ECS Task Definition
3950
task_definition = ecs.Ec2TaskDefinition(
4051
stack, "TaskDef")
4152

53+
# Add container to task definition
54+
# Uses sample container image with 256MB memory limit
4255
container = task_definition.add_container(
4356
"web",
4457
image=ecs.ContainerImage.from_registry("amazon/amazon-ecs-sample"),
4558
memory_limit_mib=256
4659
)
4760

61+
62+
# Expose port 80
4863
port_mapping = ecs.PortMapping(
4964
container_port=80,
5065
host_port=0,
5166
protocol=ecs.Protocol.TCP
5267
)
53-
5468
container.add_port_mappings(port_mapping)
5569

56-
# Create Service
70+
# Create ECS Service using the task definition
5771
service = ecs.Ec2Service(
5872
stack, "Service",
5973
cluster=cluster,
6074
task_definition=task_definition
6175
)
6276

63-
64-
# Create ALB
77+
# Create Application Load Balancer
78+
# Internet-facing ALB in the VPC
6579
lb = elbv2.ApplicationLoadBalancer(
6680
stack, "LB",
6781
vpc=vpc,
6882
internet_facing=True
6983
)
7084

85+
# Add ALB listener on port 80
7186
listener = lb.add_listener(
7287
"PublicListener",
7388
port=80,
7489
open=True
7590
)
7691

77-
asg.connections.allow_from(lb, port_range=ec2.Port.tcp_range(32768, 65535), description="allow incoming traffic from ALB")
7892

93+
# Configure health check for target group
7994
health_check = elbv2.HealthCheck(
8095
interval=Duration.seconds(60),
8196
path="/health",
8297
timeout=Duration.seconds(5)
8398
)
8499

85-
# Attach ALB to ECS Service
100+
# Attach ALB to ECS Service with health check configuration
86101
listener.add_targets(
87102
"ECS",
88103
port=80,
89104
targets=[service],
90105
health_check=health_check,
91106
)
92107

108+
# Output the ALB DNS name
93109
CfnOutput(
94110
stack, "LoadBalancerDNS",
95111
value="http://"+lb.load_balancer_dns_name
96112
)
97113

114+
# Synthesize the stack
98115
app.synth()

0 commit comments

Comments
 (0)