def __init__(self, scope: cdk.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.VpcNetwork(
            self, "MyGhostOnEcsVpc",
            max_a_zs=2
        )

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

        fargate_service = ecs.LoadBalancedFargateService(
            self, "FargateGhostOnEcsService",
            cluster=cluster,
            image=ecs.ContainerImage.from_registry("ghost")
        )

        cdk.CfnOutput(
            self, "LoadBalancerDNS",
            value=fargate_service.load_balancer.dns_name
        )
Exemplo n.º 2
0
    def __init__(self, scope: cdk.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, *kwargs)

        vpc = ec2.VpcNetwork(
            self, "MyVpc",
            max_a_zs=2
        )

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

        cluster.add_capacity("DefaultAutoScalingGroup",
                             instance_type=ec2.InstanceType("t2.micro"))

        ecs_service = ecs.LoadBalancedEc2Service(
            self, "Ec2Service",
            cluster=cluster,
            memory_limit_mi_b=512,
            image=ecs.ContainerImage.from_registry("amazon/amazon-ecs-sample")
        )

        cdk.CfnOutput(
            self, "LoadBalancerDNS",
            value=ecs_service.load_balancer.dns_name
        )
Exemplo n.º 3
0
    def __init__(self, scope: cdk.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, *kwargs)

        vpc = ec2.VpcNetwork(self, "MyVpc", max_a_zs=2)

        asg = autoscaling.AutoScalingGroup(
            self,
            "MyFleet",
            instance_type=ec2.InstanceType("t2.xlarge"),
            machine_image=ecs.EcsOptimizedAmi(),
            associate_public_ip_address=True,
            update_type=autoscaling.UpdateType.ReplacingUpdate,
            desired_capacity=3,
            vpc=vpc,
            vpc_subnets={'subnetType': ec2.SubnetType.Public})

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

        cluster.add_auto_scaling_group(asg)
        cluster.add_capacity("DefaultAutoScalingGroup",
                             instance_type=ec2.InstanceType("t2.micro"))
Exemplo n.º 4
0
    def __init__(self, app: cdk.App, id: str) -> None:
        super().__init__(app, id)

        vpc = ec2.VpcNetwork(self, "VPC")

        asg = autoscaling.AutoScalingGroup(
            self,
            "ASG",
            vpc=vpc,
            instance_type=ec2.InstanceTypePair(
                ec2.InstanceClass.Burstable2, ec2.InstanceSize.Micro
            ),
            machine_image=ec2.AmazonLinuxImage(),
        )

        lb = elbv2.ApplicationLoadBalancer(self, "LB", vpc=vpc, internet_facing=True)

        listener = lb.add_listener("Listener", port=80)
        listener.add_targets("Target", port=80, targets=[asg])
        listener.connections.allow_default_port_from_any_ipv4("Open to the world")

        asg.scale_on_request_count("AModestLoad", target_requests_per_second=1)
Exemplo n.º 5
0
    def __init__(self, scope: cdk.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, *kwargs)

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

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

        # Create Fargate Service
        fargate_service = ecs.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_sec=60,
            scale_out_cooldown_sec=60,
        )

        cdk.CfnOutput(
            self, "LoadBalancerDNS",
            value=fargate_service.load_balancer.dns_name
        )
Exemplo n.º 6
0
    def __init__(self, app: cdk.App, id: str, **kwargs) -> None:
        super().__init__(app, id, **kwargs)

        vpc = ec2.VpcNetwork(self, "VPC")

        asg = autoscaling.AutoScalingGroup(
            self,
            "ASG",
            vpc=vpc,
            instance_type=ec2.InstanceTypePair(ec2.InstanceClass.Burstable2,
                                               ec2.InstanceSize.Micro),
            machine_image=ec2.AmazonLinuxImage(),
        )

        lb = elb.LoadBalancer(self,
                              "LB",
                              vpc=vpc,
                              internet_facing=True,
                              health_check={"port": 80})
        lb.add_target(asg)

        listener = lb.add_listener(external_port=80)
        listener.connections.allow_default_port_from_any_ipv4(
            "Open to the world")
Exemplo n.º 7
0
from aws_cdk import (
    aws_ec2 as ec2,
    aws_ecs as ecs,
    aws_elasticloadbalancingv2 as elbv2,
    cdk,
)

app = cdk.App()
stack = cdk.Stack(app, "aws-ec2-integ-ecs")

# Create a cluster
vpc = ec2.VpcNetwork(stack, "MyVpc", max_a_zs=2)

cluster = ecs.Cluster(stack, 'EcsCluster', vpc=vpc)
cluster.add_capacity("DefaultAutoScalingGroup",
                     instance_type=ec2.InstanceType("t2.micro"))

# Create Task Definition
task_definition = ecs.Ec2TaskDefinition(stack, "TaskDef")
container = task_definition.add_container(
    "web",
    image=ecs.ContainerImage.from_registry("amazon/amazon-ecs-sample"),
    memory_limit_mi_b=256)
container.add_port_mappings(container_port=80,
                            host_port=8080,
                            protocol=ecs.Protocol.Tcp)

# Create Service
service = ecs.Ec2Service(stack,
                         "Service",
                         cluster=cluster,