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 )
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.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") ) cdk.CfnOutput( self, "LoadBalancerDNS", value=fargate_service.load_balancer.load_balancer_dns_name )
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 )
def __init__(self, scope: cdk.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_sec=60, scale_out_cooldown_sec=60, ) cdk.CfnOutput( self, "LoadBalancerDNS", value=fargate_service.load_balancer.load_balancer_dns_name)
def __init__(self, scope: cdk.App, id: str, **kwargs) -> None: super().__init__(scope, id, **kwargs) resource = MyCustomResource( self, "DemoResource", message="CustomResource says hello" ) # Publish the custom resource output cdk.CfnOutput( self, "ResponseMessage", description="The message that came back from the Custom Resource", value=resource.response, )
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)
"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, task_definition=task_definition) # Create ALB lb = elbv2.ApplicationLoadBalancer(stack, "LB", vpc=vpc, internet_facing=True) listener = lb.add_listener("PublicListener", port=80, open=True) # Attach ALB to ECS Service listener.add_targets("ECS", port=80, targets=[service], health_check={ "interval_secs": 60, "path": "/health", "timeout_seconds": 5 }) cdk.CfnOutput(stack, "LoadBalancerDNS", value=lb.load_balancer_dns_name) app.run()
def __init__(self, app: cdk.App, id: str, vpc: ec2.Vpc, servicedomain: str, **kwargs) -> None: super().__init__(app, id) cluster = ecs.Cluster(self, id, vpc=vpc) cluster.add_default_cloud_map_namespace( name=servicedomain, type=ecs.NamespaceType.PrivateDns) self._cluster = cluster ecssg = ec2.SecurityGroup(self, 'ECSServiceSecurityGroup', vpc=vpc) ecssg.add_ingress_rule(peer=ec2.CidrIPv4(vpc.vpc_cidr_block), connection=ec2.TcpAllPorts()) self._clustersg = ecssg # Bastion host stuff ------------------------------------------------------------------------------------- # BastionInstanceRole pd = pu.PolicyUtils.createpolicyfromfile( './appmeshdemo/policydocs/appmesh.json') bir = iam.Role( self, 'BastionInstanceRole', assumed_by=iam.ServicePrincipal('ec2'), inline_policies={'appmesh': pd}, managed_policy_arns=[ 'arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforSSM' ]) bip = iam.CfnInstanceProfile(self, 'BastionInstanceProfile', roles=[bir.role_name]) # Bastion EC2 instance bsg = ec2.SecurityGroup(self, 'BastionSG', vpc=vpc) bsg.add_ingress_rule(peer=ec2.AnyIPv4(), connection=ec2.TcpAllPorts()) ni = ec2.CfnNetworkInterfaceProps() ni['associatePublicIpAddress'] = True ni['deviceIndex'] = '0' ni['groupSet'] = [bsg.security_group_name] ni['subnetId'] = vpc.public_subnets[0].subnet_id bhi = ec2.CfnInstance( self, 'BastionInstance', instance_type='t2.micro', iam_instance_profile=bip.instance_profile_name, image_id=ec2.AmazonLinuxImage().get_image(self).image_id, network_interfaces=[ni]) # Load-Balancer stuff ------------------------------------------------------------------------------------ plbsg = ec2.SecurityGroup(self, 'PublicLoadBalancerSG', vpc=vpc) plbsg.add_ingress_rule(peer=ec2.AnyIPv4(), connection=ec2.TcpPortRange(0, 65535)) plb = elbv2.ApplicationLoadBalancer(self, 'PublicLoadBalancer', internet_facing=True, load_balancer_name='appmeshdemo', security_group=plbsg, vpc=vpc, idle_timeout_secs=30) self._publoadbal = plb healthchk = elbv2.HealthCheck() healthchk['intervalSecs'] = 6 healthchk['healthyThresholdCount'] = 2 healthchk['unhealthyThresholdCount'] = 2 dtg = elbv2.ApplicationTargetGroup( self, 'DummyTargetGroupPublic', vpc=vpc, port=80, protocol=elbv2.ApplicationProtocol.Http, health_check=healthchk, target_group_name='appmeshdemo-drop-1') plbl = elbv2.ApplicationListener( self, 'PublicLoadBalancerListener', load_balancer=plb, port=80, protocol=elbv2.ApplicationProtocol.Http, default_target_groups=[dtg]) cdk.CfnOutput(self, id='External URL', value='http://' + plb.load_balancer_dns_name)