|
| 1 | +import aws_cdk.aws_ec2 as ec2 |
| 2 | +import aws_cdk.aws_ecs as ecs |
| 3 | +import aws_cdk.aws_ecs_patterns as ecs_patterns |
| 4 | +from aws_cdk import core |
| 5 | +import os |
| 6 | + |
| 7 | + |
| 8 | +class CdkEcsExampleStack(core.Stack): |
| 9 | + def __init__(self, scope: core.Construct, construct_id: str, **kwargs) -> None: |
| 10 | + super().__init__(scope, construct_id, **kwargs) |
| 11 | + vpc = ec2.Vpc(self, "EcsVpc", max_azs=2, nat_gateways=0) |
| 12 | + vpc.add_s3_endpoint("S3Endpoint") |
| 13 | + vpc.add_interface_endpoint( |
| 14 | + "EcrDockerEndpoint", service=ec2.InterfaceVpcEndpointAwsService.ECR_DOCKER |
| 15 | + ) |
| 16 | + vpc.add_interface_endpoint( |
| 17 | + "EcrEndpoint", service=ec2.InterfaceVpcEndpointAwsService.ECR |
| 18 | + ) |
| 19 | + vpc.add_interface_endpoint( |
| 20 | + "CloudWatchLogsEndpoint", |
| 21 | + service=ec2.InterfaceVpcEndpointAwsService.CLOUDWATCH_LOGS, |
| 22 | + ) |
| 23 | + cluster = ecs.Cluster(self, "EcsCluster", vpc=vpc) |
| 24 | + task_definition = ecs.FargateTaskDefinition( |
| 25 | + self, |
| 26 | + "DemoServiceTask", |
| 27 | + family="DemoServiceTask", |
| 28 | + volumes=[ |
| 29 | + ecs.Volume( |
| 30 | + name="test-volume", |
| 31 | + host=ecs.Host(source_path=os.path.join(os.getcwd(), "service")), |
| 32 | + ) |
| 33 | + ], |
| 34 | + ) |
| 35 | + |
| 36 | + image = ecs.ContainerImage.from_asset("service") |
| 37 | + |
| 38 | + container = task_definition.add_container("app", image=image) |
| 39 | + container.add_port_mappings(ecs.PortMapping(container_port=8080)) |
| 40 | + |
| 41 | + # Add mount point for the container |
| 42 | + container.add_mount_points( |
| 43 | + ecs.MountPoint( |
| 44 | + container_path="/app", source_volume="test-volume", read_only=True |
| 45 | + ), |
| 46 | + ) |
| 47 | + |
| 48 | + ecs_patterns.ApplicationLoadBalancedFargateService( |
| 49 | + self, |
| 50 | + "DemoService", |
| 51 | + cluster=cluster, |
| 52 | + desired_count=2, |
| 53 | + task_definition=task_definition, |
| 54 | + ) |
0 commit comments