|
| 1 | +import aws_cdk as cdk |
| 2 | +import constructs |
| 3 | +from aws_cdk import aws_ec2 as ec2 |
| 4 | +from aws_cdk import aws_ecs as ecs |
| 5 | +from aws_cdk import aws_ecs_patterns as ecs_patterns |
| 6 | +from aws_cdk import aws_elasticloadbalancingv2 as elbv2 |
| 7 | +from aws_cdk import aws_rds as rds |
| 8 | + |
| 9 | +# FIXME |
| 10 | +db_user = "wordpress" |
| 11 | +db_password = "wordpress-password" |
| 12 | +db_name = "wordpress" |
| 13 | + |
| 14 | + |
| 15 | +class WordpressStack(cdk.Stack): |
| 16 | + def __init__(self, scope: constructs.Construct, construct_id: str, **kwargs) -> None: |
| 17 | + super().__init__(scope, construct_id, **kwargs) |
| 18 | + |
| 19 | + # VPC |
| 20 | + self.vpc = ec2.Vpc( |
| 21 | + self, |
| 22 | + "VPC", |
| 23 | + nat_gateways=1, |
| 24 | + cidr="10.0.0.0/16", |
| 25 | + subnet_configuration=[ |
| 26 | + ec2.SubnetConfiguration( |
| 27 | + name="public", subnet_type=ec2.SubnetType.PUBLIC, cidr_mask=24 |
| 28 | + ), |
| 29 | + ec2.SubnetConfiguration( |
| 30 | + name="private", subnet_type=ec2.SubnetType.PRIVATE_WITH_NAT, cidr_mask=24 |
| 31 | + ), |
| 32 | + ], |
| 33 | + ) |
| 34 | + self.cluster_sec_group = ec2.SecurityGroup( |
| 35 | + self, |
| 36 | + "cluster-sec-group", |
| 37 | + security_group_name="cluster-sec-group", |
| 38 | + vpc=self.vpc, |
| 39 | + allow_all_outbound=True, |
| 40 | + ) |
| 41 | + |
| 42 | + database = rds.DatabaseInstance( |
| 43 | + self, |
| 44 | + "WordpressDatabase", |
| 45 | + credentials=rds.Credentials.from_password( |
| 46 | + username=db_user, password=cdk.SecretValue.plain_text(db_password) |
| 47 | + ), |
| 48 | + database_name=db_name, |
| 49 | + engine=rds.DatabaseInstanceEngine.MARIADB, |
| 50 | + vpc=self.vpc, |
| 51 | + ) |
| 52 | + |
| 53 | + # ECS cluster |
| 54 | + cluster = ecs.Cluster(self, "ServiceCluster", vpc=self.vpc) |
| 55 | + |
| 56 | + docker_image = ecs.ContainerImage.from_registry("wordpress") |
| 57 | + web_service = ecs_patterns.ApplicationLoadBalancedFargateService( |
| 58 | + self, |
| 59 | + "Wordpress", |
| 60 | + cluster=cluster, |
| 61 | + target_protocol=elbv2.ApplicationProtocol.HTTP, |
| 62 | + protocol=elbv2.ApplicationProtocol.HTTP, |
| 63 | + desired_count=1, |
| 64 | + # container size |
| 65 | + cpu=512, |
| 66 | + memory_limit_mib=2048, |
| 67 | + task_image_options=ecs_patterns.ApplicationLoadBalancedTaskImageOptions( |
| 68 | + image=docker_image, |
| 69 | + container_port=80, |
| 70 | + container_name="webapp", |
| 71 | + enable_logging=True, |
| 72 | + environment={ |
| 73 | + "WORDPRESS_DB_HOST": f"{database.db_instance_endpoint_address}:{database.db_instance_endpoint_port}", |
| 74 | + "WORDPRESS_DB_USER": db_user, |
| 75 | + "WORDPRESS_DB_PASSWORD": db_password, |
| 76 | + "WORDPRESS_DB_NAME": db_name, |
| 77 | + }, |
| 78 | + ), |
| 79 | + ) |
| 80 | + |
| 81 | + # TODO: add APIGW and dns + cert |
0 commit comments