Exemple #1
0
    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, *kwargs)

        # Create a cluster
        vpc = ec2.Vpc(self, "Vpc", max_a_zs=2)

        cluster = ecs.Cluster(self, 'fargate-service-autoscaling', vpc=vpc)

        # Create Fargate Service
        fargate_service = ecs_patterns.LoadBalancedFargateService(
            self,
            "sample-app",
            cluster=cluster,
            image=ecs.ContainerImage.from_registry("amazon/amazon-ecs-sample"))

        # Setup AutoScaling policy
        scaling = fargate_service.service.auto_scale_task_count(max_capacity=2)
        scaling.scale_on_cpu_utilization(
            "CpuScaling",
            target_utilization_percent=50,
            scale_in_cooldown=core.Duration.seconds(60),
            scale_out_cooldown=core.Duration.seconds(60),
        )

        core.CfnOutput(
            self,
            "LoadBalancerDNS",
            value=fargate_service.load_balancer.load_balancer_dns_name)
Exemple #2
0
    def __init__(self, scope: Construct, id: str):
        super().__init__(scope, id)

        # The code that defines your stack goes here
        vpc = aws_ec2.Vpc(self, 'vpc')
        cluster = aws_ecs.Cluster(self, 'cluster', vpc=vpc)
        # task_definition = aws_ecs.FargateTaskDefinition(self, 'NodeTask')
        # log_driver = aws_ecs.LogDriver.aws_logs(stream_prefix="NodeAppContainerLog")
        # container = task_definition.add_container('NodeApp',
        #                               image=aws_ecs.ContainerImage.from_asset("nodejsapp"), logging=log_driver)
        # port_mapping = aws_ecs.PortMapping(container_port=8080)
        # container.add_port_mappings(port_mapping)
        #
        # aws_ecs.FargateService(self, 'service',
        #                        cluster=cluster,
        #                        task_definition=task_definition,
        #                        desired_count=5)
        aws_ecs_patterns.LoadBalancedFargateService(
            self,
            'NodeApp',
            cluster=cluster,
            desired_count=5,
            container_name="NodeApp",
            container_port=8080,
            image=aws_ecs.ContainerImage.from_asset("nodejsapp"))
Exemple #3
0
    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, *kwargs)

        # Create VPC and Fargate Cluster
        # NOTE: Limit AZs to avoid reaching resource quotas
        vpc = ec2.Vpc(
            self, "MyVpc",
            max_a_zs=2
        )

        cluster = ecs.Cluster(
            self, 'Ec2Cluster',
            vpc=vpc
        )

        fargate_service = ecs_patterns.LoadBalancedFargateService(
            self, "FargateService",
            cluster=cluster,
            image=ecs.ContainerImage.from_registry("amazon/amazon-ecs-sample")
        )

        core.CfnOutput(
            self, "LoadBalancerDNS",
            value=fargate_service.load_balancer.load_balancer_dns_name
        )
Exemple #4
0
    def __init__(self, scope: core.Stack, id: str, ecs_cluster, vpc,
                 services_3000_sec_group, desired_service_count, **kwargs):
        super().__init__(scope, id, **kwargs)
        self.ecs_cluster = ecs_cluster
        self.vpc = vpc
        self.services_3000_sec_group = services_3000_sec_group
        self.desired_service_count = desired_service_count

        # This will create an ALB with listener/target group, ecs task def, ecs fargate service, logging in cloudwatch
        # and security group from ALB to containers. This essentially condenses 95 lines of code into 15.
        self.fargate_load_balanced_service = aws_ecs_patterns.LoadBalancedFargateService(
            self,
            "FrontendFargateLBService",
            cluster=self.ecs_cluster,
            image=aws_ecs.ContainerImage.from_registry(
                "brentley/ecsdemo-frontend"),
            container_port=3000,
            cpu=256,
            memory_limit_mi_b=512,
            enable_logging=True,
            desired_count=self.desired_service_count,
            load_balancer_type=aws_ecs_patterns.LoadBalancerType(
                'APPLICATION'),
            public_load_balancer=True,
            environment={
                "CRYSTAL_URL": "http://ecsdemo-crystal.service:3000/crystal",
                "NODEJS_URL": "http://ecsdemo-nodejs.service:3000"
            },
        )

        # There has to be a better way, but for now this is what we got.
        # Allow inbound 3000 from Frontend Service to Backend
        self.sec_grp_ingress_backend_to_frontend_3000 = aws_ec2.CfnSecurityGroupIngress(
            self,
            "InboundBackendSecGrp3000",
            ip_protocol='TCP',
            source_security_group_id=self.fargate_load_balanced_service.
            service.connections.security_groups[0].security_group_id,
            from_port=3000,
            to_port=3000,
            group_id=self.services_3000_sec_group.security_group_id)

        # There has to be a better way, but for now this is what we got.
        # Allow inbound 3000 Backend to Frontend Service
        self.sec_grp_ingress_frontend_to_backend_3000 = aws_ec2.CfnSecurityGroupIngress(
            self,
            "InboundFrontendtoBackendSecGrp3000",
            ip_protocol='TCP',
            source_security_group_id=self.services_3000_sec_group.
            security_group_id,
            from_port=3000,
            to_port=3000,
            group_id=self.fargate_load_balanced_service.service.connections.
            security_groups[0].security_group_id,
        )
Exemple #5
0
    def __init__(self, scope: core.Construct, id: str, config: dict, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        certificate = certificatemanager.Certificate.from_certificate_arn(
            self, 'certificate', config['certificate_arn'])
        hosted_zone = route53.HostedZone.from_lookup(
            self, 'hosted_zone', domain_name=config['domain_name'], 
            private_zone=True, vpc_id=config['vpc_id'])
        vpc = ec2.Vpc.from_lookup(self, 'vpc', vpc_id=config['vpc_id'])

        cluster = ecs.Cluster(self, 'cluster', vpc=vpc)
        load_balanced_fargate_service = ecs_patterns.LoadBalancedFargateService(
            self, 'fargate_service', cluster=cluster,
            certificate=certificate,
            domain_name=config['nginx_domain_name'],
            domain_zone=hosted_zone,
            image=ecs.ContainerImage.from_registry(config['container_image']),
            public_load_balancer=False
        )
        self._set_subnets(load_balanced_fargate_service, config['subnet_ids'])
    def __init__(self, app: cdk.App, id: str, **kwargs) -> None:
        super().__init__(app, id)

        #        bucket = s3.Bucket(self,
        #        "MyFirstBucketwithCDK",
        #        versioned=True,
        #        encryption=s3.BucketEncryption.KmsManaged,)

        # Build the VPC
        vpc = ec2.Vpc(self, "MyVpc", max_a_zs=3)

        # Define ECS cluster constructr
        cluster = ecs.Cluster(self, 'FargateCluster', vpc=vpc)

        fargate_service = ecs_patterns.LoadBalancedFargateService(
            self,
            "FargateService",
            cluster=cluster,
            image=ecs.ContainerImage.from_registry("amazon/amazon-ecs-sample"))

        cdk.CfnOutput(
            self,
            "LoadBalancerDNS",
            value=fargate_service.load_balancer.load_balancer_dns_name)