Example #1
0
    def __init__(self, scope: core.Construct, id: str, environment_name: str,
                 base_domain_name: str, full_domain_name: str,
                 base_app_name: str, full_app_name: str, **kwargs) -> None:

        super().__init__(scope, id, **kwargs)

        self.environment_name = environment_name
        self.base_domain_name = base_domain_name
        self.full_domain_name = full_domain_name
        self.base_app_name = base_app_name
        self.full_app_name = full_app_name

        self.hosted_zone = HostedZone(self, "HostedZone").hosted_zone

        self.certificate = SiteCertificate(self, "SiteCert")

        self.vpc_stack = VpcStack(self, "VpcStack")
        self.vpc = self.vpc_stack.vpc

        self.alb_stack = AlbStack(self, "AlbStack")
        self.alb = self.alb_stack.alb

        self.https_listener = self.alb_stack.https_listener

        self.static_site_stack = StaticSiteStack(self, "StaticSiteStack")
        self.static_site_bucket = self.static_site_stack.static_site_bucket

        self.backend_assets = BackendAssetsStack(self, "BackendAssetsStack")
        self.backend_assets_bucket = self.backend_assets.assets_bucket

        self.cloudfront = CloudFrontStack(self, "CloudFrontStack")

        if os.path.isdir("./quasar/dist/pwa"):
            s3_deployment.BucketDeployment(
                self,
                "BucketDeployment",
                destination_bucket=self.static_site_bucket,
                sources=[s3_deployment.Source.asset("./quasar/dist/pwa")],
                distribution=self.cloudfront.distribution,
            )

        self.ecs = EcsStack(self, "EcsStack")
        self.cluster = self.ecs.cluster

        self.rds = RdsStack(self, "RdsStack")

        self.elasticache = ElastiCacheStack(self, "ElastiCacheStack")

        # image used for all django containers: gunicorn, daphne, celery, beat
        self.image = ecs.AssetImage(
            "./backend",
            file="scripts/prod/Dockerfile",
            target="production",
        )

        self.variables = Variables(
            self,
            "Variables",
            bucket_name=self.backend_assets_bucket.bucket_name,
            db_secret=self.rds.db_secret,
            full_domain_name=self.full_domain_name,
            postgres_host=self.rds.rds_cluster.get_att(
                "Endpoint.Address").to_string(),
            redis_host=self.elasticache.elasticache.
            attr_redis_endpoint_address,  # noqa
        )

        self.backend_service = BackendServiceStack(self, "BackendServiceStack")
        self.flower_service = FlowerServiceStack(self, "FlowerServiceStack")

        self.celery_default_service = CeleryDefaultServiceStack(
            self, "CeleryDefaultServiceStack")

        # migrate, collectstatic, createsuperuser
        self.backend_tasks = BackendTasksStack(self, "BackendTasksStack")
Example #2
0
    def __init__(self, scope: core.Construct, id: str, **kwargs,) -> None:
        super().__init__(
            scope, id, **kwargs,
        )

        self.flower_task = ecs.FargateTaskDefinition(self, "FlowerTask")

        FLOWER_PASSWORD = os.environ.get("FLOWER_PASSWORD", "flowerpassword")
        REDIS_SERVICE_HOST = (
            scope.elasticache.elasticache.attr_redis_endpoint_address
        )
        CELERY_BROKER_URL = f"redis://{REDIS_SERVICE_HOST}:6379/1"

        self.flower_task.add_container(
            "FlowerContainer",
            image=ecs.ContainerImage.from_registry("mher/flower"),
            logging=ecs.LogDrivers.aws_logs(
                stream_prefix="FlowerContainer",
                log_retention=logs.RetentionDays.ONE_DAY,
            ),
            command=[
                "--url_prefix=flower",
                f"--broker={CELERY_BROKER_URL}",
                f"--basic_auth=flower:{FLOWER_PASSWORD}",
            ],
        )

        self.flower_task.add_container(
            "FlowerProxyContainer",
            image=ecs.AssetImage("./nginx/flowerproxy"),
            # image=ecs.ContainerImage.from_registry("nginx"),
        )

        port_mapping = ecs.PortMapping(
            container_port=80, protocol=ecs.Protocol.TCP
        )
        self.flower_task.default_container.add_port_mappings(port_mapping)

        self.flower_service = ecs.FargateService(
            self,
            "FlowerService",
            task_definition=self.flower_task,
            assign_public_ip=True,
            cluster=scope.ecs.cluster,
            security_group=ec2.SecurityGroup.from_security_group_id(
                self,
                "FlowerServiceSecurityGroup",
                security_group_id=scope.vpc.vpc_default_security_group,
            ),
        )

        scope.https_listener.add_targets(
            "FlowerTarget",
            port=80,
            targets=[self.flower_service],
            priority=1,
            path_patterns=["/flower/*", "/flower*"],
            health_check=elbv2.HealthCheck(
                healthy_http_codes="200-404", path="/flower"
            ),
        )