Beispiel #1
0
    def __init__(
        self,
        scope: Construct,
        id: str,
        cluster: _Cluster,
        shared_airflow_env: dict,
        **kwargs,
    ) -> None:
        super().__init__(scope, id, **kwargs)
        task_definition = FargateTaskDefinition(
            self,
            "task-def",
            cpu=512,
            memory_limit_mib=1024,
        )

        task_definition.add_container(
            "container",
            image=ContainerImage.from_registry("apache/airflow:1.10.12-python3.8"),
            command=["scheduler"],
            logging=LogDriver.aws_logs(stream_prefix="scheduler"),
            environment=shared_airflow_env,
        )

        FargateService(
            self,
            "service",
            cluster=cluster,
            task_definition=task_definition,
        )
Beispiel #2
0
    def __init__(
        self,
        scope: Construct,
        id: str,
        cluster: _Cluster,
        shared_airflow_env: dict,
        vpc: _Vpc,
        **kwargs,
    ) -> None:
        super().__init__(scope, id, **kwargs)
        task_definition = FargateTaskDefinition(
            self, "task-def", cpu=512, memory_limit_mib=1024
        )
        container = task_definition.add_container(
            "container",
            image=ContainerImage.from_registry("apache/airflow:1.10.12-python3.8"),
            command=["webserver"],
            logging=LogDriver.aws_logs(stream_prefix="webserver"),
            environment=shared_airflow_env,
        )

        port_mapping = PortMapping(container_port=8080, host_port=8080)
        container.add_port_mappings(port_mapping)

        service = FargateService(
            self,
            "service",
            cluster=cluster,
            task_definition=task_definition,
        )

        lb = ApplicationLoadBalancer(self, "lb", vpc=vpc, internet_facing=True)
        listener = lb.add_listener("public_listener", port=80, open=True)

        health_check = HealthCheck(
            interval=Duration.seconds(60),
            path="/health",
            timeout=Duration.seconds(5),
        )

        listener.add_targets(
            "webserver",
            port=8080,
            targets=[service],
            health_check=health_check,
        )

        CfnOutput(self, "LoadBalancerDNS", value=lb.load_balancer_dns_name)
Beispiel #3
0
    def __init__(
        self, scope: Construct, id: str, shared_airflow_env: dict, **kwargs
    ) -> None:
        super().__init__(scope, id, **kwargs)
        task_definition = FargateTaskDefinition(
            self,
            "initdb",
            cpu=512,
            memory_limit_mib=1024,
        )
        task_definition.add_container(
            "initdb",
            image=ContainerImage.from_registry("apache/airflow:1.10.12-python3.8"),
            command=["initdb"],
            logging=LogDriver.aws_logs(stream_prefix="initdb"),
            environment=shared_airflow_env,
        )

        CfnOutput(self, "init-task-def", value=task_definition.task_definition_arn)
    def __init__(
        self,
        scope: Construct,
        id: str,
        *,
        subdomain_name: str,
        deployment: Deployment,
        policy: Policy,
        application_name: str,
        image_name: str,
        port: int,
        memory_limit_mib: int,
        desired_count: int,
        cluster: ICluster,
        priority: int,
        path_pattern: Optional[str] = None,
        health_check_grace_period: Optional[Duration] = None,
        allow_via_http: Optional[bool] = False,
        command: Optional[List[str]] = None,
        environment: Mapping[str, str] = {},
        secrets: Mapping[str, Secret] = {},
        volumes: Mapping[str, Volume] = {},
    ) -> None:
        super().__init__(scope, id)

        full_application_name = f"{deployment.value}-{application_name}"

        log_group = tasks.add_logging(full_application_name)
        self.task_role = tasks.add_role(full_application_name)
        policy.add_role(self.task_role)

        logging = LogDrivers.aws_logs(
            stream_prefix=full_application_name,
            log_group=log_group,
        )

        image = ImageFromParameterStore(
            self,
            "ImageName",
            parameter_name=f"/Version/{deployment.value}/{application_name}",
            image_name=image_name,
            policy=policy,
        )

        task_definition = Ec2TaskDefinition(
            self,
            "TaskDef",
            network_mode=NetworkMode.BRIDGE,
            execution_role=self.task_role,
            task_role=self.task_role,
            volumes=list(volumes.values()),
        )

        self.container = task_definition.add_container(
            "Container",
            image=ContainerImage.from_registry(image.image_ref),
            memory_limit_mib=memory_limit_mib,
            logging=logging,
            environment=environment,
            secrets=secrets,
            command=command,
        )
        self.container.add_mount_points(*[
            MountPoint(
                container_path=path,
                read_only=False,
                source_volume=volume.name,
            ) for path, volume in volumes.items()
        ])

        self.add_port(port=port, )

        self.service = Ec2Service(
            self,
            "Service",
            cluster=cluster,
            task_definition=task_definition,
            desired_count=desired_count,
            health_check_grace_period=health_check_grace_period,
        )
        policy.add_service(self.service)
        policy.add_cluster(cluster)

        self.service.add_placement_strategies(
            PlacementStrategy.spread_across(
                BuiltInAttributes.AVAILABILITY_ZONE), )

        self.add_target(
            subdomain_name=subdomain_name,
            port=port,
            priority=priority,
            path_pattern=path_pattern,
            allow_via_http=allow_via_http,
        )

        # Remove the security group from this stack, and add it to the ALB stack
        self.service.node.try_remove_child("SecurityGroup1")
        for security_group in cluster.connections.security_groups:
            self.service.connections.add_security_group(security_group)
    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        # The code that defines your stack goes here

        vpc = Vpc(self, "MyVpc", max_azs=2)

        ecs_cluster = Cluster(self, 'FagateCluster', vpc=vpc)

        alb = ApplicationLoadBalancer(self,
                                      'EcsLb',
                                      vpc=vpc,
                                      internet_facing=True)

        listener = alb.add_listener('EcsListener', port=80)

        listener.add_fixed_response('Default-Fix', status_code='404')
        listener.node.default_child.default_action = [{
            "type": "fixed-response",
            "fixedResponseConfig": {
                "statusCode": "404"
            }
        }]

        website_bucket = Bucket(self,
                                'PetclinicWebsite',
                                website_index_document='index.html',
                                public_read_access=True,
                                removal_policy=core.RemovalPolicy.DESTROY)

        deployment = BucketDeployment(
            self,
            'PetclinicDeployWebsite',
            sources=[Source.asset('./spring-petclinic-static')],
            destination_bucket=website_bucket,
            retain_on_delete=False
            #destination_key_prefix='web/static'
        )

        # Modify the config.js with CF custome resource
        modify_policy = [
            PolicyStatement(actions=[
                "s3:PutObject", "s3:PutObjectAcl", "s3:PutObjectVersionAcl",
                "s3:GetObject"
            ],
                            effect=Effect.ALLOW,
                            resources=[website_bucket.bucket_arn + "/*"]),
            PolicyStatement(actions=["s3:ListBucket"],
                            effect=Effect.ALLOW,
                            resources=[website_bucket.bucket_arn]),
            PolicyStatement(actions=["dynamodb:*"],
                            effect=Effect.ALLOW,
                            resources=[
                                "arn:aws:dynamodb:" + self.region + ":" +
                                self.account + ":*"
                            ])
        ]

        with open("custom-resource-code/init.py", encoding="utf-8") as fp:
            code_body = fp.read()

        dynamodb_tables = []

        for s in ['customers', 'vets', 'visits']:
            table = Table(
                self,
                s.capitalize() + 'Table',
                partition_key={
                    'name': 'id',
                    'type': AttributeType.STRING
                },
                removal_policy=core.RemovalPolicy.DESTROY,
                read_capacity=5,
                write_capacity=5,
            )

            dynamodb_tables.append(table.table_name)

            asset = DockerImageAsset(
                self,
                'spring-petclinic-' + s,
                repository_name=self.stack_name + '-' + s,
                directory='./spring-petclinic-serverless/spring-petclinic-' +
                s + '-serverless',
                build_args={
                    'JAR_FILE':
                    'spring-petclinic-' + s + '-serverless-2.0.7.jar'
                })

            ecs_task = FargateTaskDefinition(self,
                                             'TaskDef-Fargate-' + s,
                                             memory_limit_mib=512,
                                             cpu=256)

            ecs_task.add_to_task_role_policy(
                PolicyStatement(actions=["dynamodb:*"],
                                effect=Effect.ALLOW,
                                resources=[table.table_arn]))

            ecs_task.add_to_task_role_policy(
                PolicyStatement(actions=['xray:*'],
                                effect=Effect.ALLOW,
                                resources=['*']))

            env = {
                'DYNAMODB_TABLE_NAME': table.table_name,
                'SERVER_SERVLET_CONTEXT_PATH': '/api/' + s.rstrip('s')
            }

            ecs_container = ecs_task.add_container(
                'Container-' + s,
                image=ContainerImage.from_docker_image_asset(asset),
                logging=LogDriver.aws_logs(stream_prefix=s),
                environment=env)

            ecs_container.add_port_mappings(PortMapping(container_port=8080))

            # Sidecare Container for X-Ray
            ecs_sidecar_container = ecs_task.add_container(
                'Sidecar-Xray-' + s,
                image=ContainerImage.from_registry('amazon/aws-xray-daemon'))

            ecs_sidecar_container.add_port_mappings(
                PortMapping(container_port=2000, protocol=Protocol.UDP))

            ecs_service = FargateService(self,
                                         'FargateService-' + s,
                                         cluster=ecs_cluster,
                                         service_name='spring-petclinic-' + s,
                                         desired_count=2,
                                         task_definition=ecs_task)

            parttern = '/api/' + s.rstrip('s') + '/*'
            priority = randint(1, 10) * len(s)
            check = HealthCheck(
                path='/api/' + s.rstrip('s') + '/manage',
                healthy_threshold_count=2,
                unhealthy_threshold_count=3,
            )

            target = listener.add_targets('ECS-' + s,
                                          path_pattern=parttern,
                                          priority=priority,
                                          port=80,
                                          targets=[ecs_service],
                                          health_check=check)

        resource = CustomResource(
            self,
            "S3ModifyCustomResource",
            provider=CustomResourceProvider.lambda_(
                SingletonFunction(self,
                                  "CustomResourceSingleton",
                                  uuid="f7d4f730-4ee1-11e8-9c2d-fa7ae01bbebc",
                                  code=InlineCode(code_body),
                                  handler="index.handler",
                                  timeout=core.Duration.seconds(300),
                                  runtime=Runtime.PYTHON_3_7,
                                  initial_policy=modify_policy)),
            properties={
                "Bucket": website_bucket.bucket_name,
                "InvokeUrl": 'http://' + alb.load_balancer_dns_name + '/',
                "DynamoDBTables": dynamodb_tables
            })

        core.CfnOutput(self,
                       "FagateALBUrl",
                       export_name="FagateALBUrl",
                       value=alb.load_balancer_dns_name)
        core.CfnOutput(self,
                       "FagatePetclinicWebsiteUrl",
                       export_name="FagatePetclinicWebsiteUrl",
                       value=website_bucket.bucket_website_url)