|
3 | 3 | aws_ec2 as ec2, |
4 | 4 | aws_elasticloadbalancingv2 as elbv2, |
5 | 5 | aws_ecs as ecs, |
| 6 | + aws_iam as iam, |
6 | 7 | App, CfnOutput, Duration, Stack |
7 | 8 | ) |
8 | 9 |
|
| 10 | +# Initialize the CDK app and stack |
9 | 11 | app = App() |
10 | 12 | stack = Stack(app, "sample-aws-ec2-integ-ecs") |
11 | 13 |
|
12 | | -# Create a cluster |
| 14 | +# Create VPC with 2 Availability Zones |
13 | 15 | vpc = ec2.Vpc( |
14 | 16 | stack, "MyVpc", |
15 | 17 | max_azs=2 |
16 | 18 | ) |
17 | 19 |
|
| 20 | +# Create ECS cluster in the VPC |
18 | 21 | cluster = ecs.Cluster( |
19 | 22 | stack, 'EcsCluster', |
20 | 23 | vpc=vpc |
21 | 24 | ) |
22 | 25 |
|
| 26 | +# Create Auto Scaling Group for ECS cluster using launchtemplates |
| 27 | +# Uses t3.micro instances with Amazon Linux 2 ECS-optimized AMI |
23 | 28 | asg = autoscaling.AutoScalingGroup( |
24 | 29 | stack, "DefaultAutoScalingGroup", |
25 | | - instance_type=ec2.InstanceType.of( |
| 30 | + vpc=vpc, |
| 31 | + launch_template=ec2.LaunchTemplate(stack, "LaunchTemplate", |
| 32 | + instance_type=ec2.InstanceType.of( |
26 | 33 | ec2.InstanceClass.BURSTABLE3, |
27 | 34 | 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 | + ) |
30 | 41 | ) |
31 | 42 |
|
| 43 | +# Create and add capacity provider to the cluster |
32 | 44 | capacity_provider = ecs.AsgCapacityProvider(stack, "AsgCapacityProvider", |
33 | 45 | auto_scaling_group=asg |
34 | 46 | ) |
35 | | - |
36 | 47 | cluster.add_asg_capacity_provider(capacity_provider) |
37 | 48 |
|
38 | | -# Create Task Definition |
| 49 | +# Define ECS Task Definition |
39 | 50 | task_definition = ecs.Ec2TaskDefinition( |
40 | 51 | stack, "TaskDef") |
41 | 52 |
|
| 53 | +# Add container to task definition |
| 54 | +# Uses sample container image with 256MB memory limit |
42 | 55 | container = task_definition.add_container( |
43 | 56 | "web", |
44 | 57 | image=ecs.ContainerImage.from_registry("amazon/amazon-ecs-sample"), |
45 | 58 | memory_limit_mib=256 |
46 | 59 | ) |
47 | 60 |
|
| 61 | + |
| 62 | +# Expose port 80 |
48 | 63 | port_mapping = ecs.PortMapping( |
49 | 64 | container_port=80, |
50 | 65 | host_port=0, |
51 | 66 | protocol=ecs.Protocol.TCP |
52 | 67 | ) |
53 | | - |
54 | 68 | container.add_port_mappings(port_mapping) |
55 | 69 |
|
56 | | -# Create Service |
| 70 | +# Create ECS Service using the task definition |
57 | 71 | service = ecs.Ec2Service( |
58 | 72 | stack, "Service", |
59 | 73 | cluster=cluster, |
60 | 74 | task_definition=task_definition |
61 | 75 | ) |
62 | 76 |
|
63 | | - |
64 | | -# Create ALB |
| 77 | +# Create Application Load Balancer |
| 78 | +# Internet-facing ALB in the VPC |
65 | 79 | lb = elbv2.ApplicationLoadBalancer( |
66 | 80 | stack, "LB", |
67 | 81 | vpc=vpc, |
68 | 82 | internet_facing=True |
69 | 83 | ) |
70 | 84 |
|
| 85 | +# Add ALB listener on port 80 |
71 | 86 | listener = lb.add_listener( |
72 | 87 | "PublicListener", |
73 | 88 | port=80, |
74 | 89 | open=True |
75 | 90 | ) |
76 | 91 |
|
77 | | -asg.connections.allow_from(lb, port_range=ec2.Port.tcp_range(32768, 65535), description="allow incoming traffic from ALB") |
78 | 92 |
|
| 93 | +# Configure health check for target group |
79 | 94 | health_check = elbv2.HealthCheck( |
80 | 95 | interval=Duration.seconds(60), |
81 | 96 | path="/health", |
82 | 97 | timeout=Duration.seconds(5) |
83 | 98 | ) |
84 | 99 |
|
85 | | -# Attach ALB to ECS Service |
| 100 | +# Attach ALB to ECS Service with health check configuration |
86 | 101 | listener.add_targets( |
87 | 102 | "ECS", |
88 | 103 | port=80, |
89 | 104 | targets=[service], |
90 | 105 | health_check=health_check, |
91 | 106 | ) |
92 | 107 |
|
| 108 | +# Output the ALB DNS name |
93 | 109 | CfnOutput( |
94 | 110 | stack, "LoadBalancerDNS", |
95 | 111 | value="http://"+lb.load_balancer_dns_name |
96 | 112 | ) |
97 | 113 |
|
| 114 | +# Synthesize the stack |
98 | 115 | app.synth() |
0 commit comments