|
| 1 | +import os |
| 2 | + |
| 3 | +from aws_cdk import ( |
| 4 | + core, |
| 5 | + aws_ec2 as ec2, |
| 6 | + aws_ecs as ecs, |
| 7 | + aws_logs as logs, |
| 8 | + aws_cloudformation as cloudformation, |
| 9 | + aws_elasticloadbalancingv2 as elbv2, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +class FlowerServiceStack(cloudformation.NestedStack): |
| 14 | + def __init__(self, scope: core.Construct, id: str, **kwargs,) -> None: |
| 15 | + super().__init__( |
| 16 | + scope, id, **kwargs, |
| 17 | + ) |
| 18 | + |
| 19 | + self.flower_task = ecs.FargateTaskDefinition(self, "FlowerTask") |
| 20 | + |
| 21 | + FLOWER_PASSWORD = os.environ.get("FLOWER_PASSWORD", "flowerpassword") |
| 22 | + REDIS_SERVICE_HOST = ( |
| 23 | + scope.elasticache.elasticache.attr_redis_endpoint_address |
| 24 | + ) |
| 25 | + CELERY_BROKER_URL = f"redis://{REDIS_SERVICE_HOST}:6379/0" |
| 26 | + self.flower_task.add_container( |
| 27 | + "FlowerContainer", |
| 28 | + image=ecs.ContainerImage.from_registry("mher/flower"), |
| 29 | + logging=ecs.LogDrivers.aws_logs( |
| 30 | + stream_prefix="FlowerContainer", |
| 31 | + log_retention=logs.RetentionDays.ONE_DAY, |
| 32 | + ), |
| 33 | + command=[ |
| 34 | + "--url_prefix=flower", |
| 35 | + f"--broker={CELERY_BROKER_URL}", |
| 36 | + f"--basic_auth=flower:{FLOWER_PASSWORD}", |
| 37 | + ], |
| 38 | + ) |
| 39 | + |
| 40 | + port_mapping = ecs.PortMapping( |
| 41 | + container_port=5555, protocol=ecs.Protocol.TCP |
| 42 | + ) |
| 43 | + self.flower_task.default_container.add_port_mappings(port_mapping) |
| 44 | + |
| 45 | + self.flower_service = ecs.FargateService( |
| 46 | + self, |
| 47 | + "FlowerService", |
| 48 | + task_definition=self.flower_task, |
| 49 | + assign_public_ip=True, |
| 50 | + cluster=scope.ecs.cluster, |
| 51 | + security_group=ec2.SecurityGroup.from_security_group_id( |
| 52 | + self, |
| 53 | + "FlowerServiceSecurityGroup", |
| 54 | + security_group_id=scope.vpc.vpc_default_security_group, |
| 55 | + ), |
| 56 | + ) |
| 57 | + |
| 58 | + scope.https_listener.add_targets( |
| 59 | + "FlowerTarget", |
| 60 | + port=80, |
| 61 | + targets=[self.flower_service], |
| 62 | + priority=1, |
| 63 | + path_patterns=["/flower/*", "flower/*"], |
| 64 | + health_check=elbv2.HealthCheck( |
| 65 | + healthy_http_codes="200-401", path="/flower/" |
| 66 | + ), |
| 67 | + ) |
0 commit comments