Skip to content

Commit

Permalink
Updating Python examples to 0.36.1 (aws-samples#64)
Browse files Browse the repository at this point in the history
* WIP: Updating Python examples to 0.36.1

* Adding some ECS examples

* Adding more ECS examples

* Adding final ECS examples
  • Loading branch information
garnaat authored Jul 3, 2019
1 parent f8be553 commit edc334e
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 101 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ __pycache__
*~
.cdk.staging
cdk.out
.vscode

19 changes: 11 additions & 8 deletions python/application-load-balancer/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
aws_autoscaling as autoscaling,
aws_ec2 as ec2,
aws_elasticloadbalancingv2 as elbv2,
cdk,
core,
)


class LoadBalancerStack(cdk.Stack):
def __init__(self, app: cdk.App, id: str) -> None:
class LoadBalancerStack(core.Stack):
def __init__(self, app: core.App, id: str) -> None:
super().__init__(app, id)

vpc = ec2.Vpc(self, "VPC")
Expand All @@ -17,13 +17,16 @@ def __init__(self, app: cdk.App, id: str) -> None:
self,
"ASG",
vpc=vpc,
instance_type=ec2.InstanceTypePair(
ec2.InstanceClass.Burstable2, ec2.InstanceSize.Micro
instance_type=ec2.InstanceType.of(
ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO
),
machine_image=ec2.AmazonLinuxImage(),
)

lb = elbv2.ApplicationLoadBalancer(self, "LB", vpc=vpc, internet_facing=True)
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])
Expand All @@ -32,6 +35,6 @@ def __init__(self, app: cdk.App, id: str) -> None:
asg.scale_on_request_count("AModestLoad", target_requests_per_second=1)


app = cdk.App()
app = core.App()
LoadBalancerStack(app, "LoadBalancerStack")
app.run()
app.synth()
22 changes: 12 additions & 10 deletions python/classic-load-balancer/app.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
from aws_cdk import cdk
from aws_cdk import (
aws_autoscaling as autoscaling,
aws_ec2 as ec2,
aws_elasticloadbalancing as elb,
core
)


class LoadBalancerStack(cdk.Stack):
def __init__(self, app: cdk.App, id: str, **kwargs) -> None:
class LoadBalancerStack(core.Stack):
def __init__(self, app: core.App, id: str, **kwargs) -> None:
super().__init__(app, id, **kwargs)

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

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

lb = elb.LoadBalancer(
self, "LB", vpc=vpc, internet_facing=True, health_check={"port": 80}
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")


app = cdk.App()
app = core.App()
LoadBalancerStack(app, "LoadBalancerStack")
app.run()
app.synth()
22 changes: 12 additions & 10 deletions python/custom-resource/app.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
from aws_cdk import cdk
from aws_cdk import core

from my_custom_resource import MyCustomResource


# A Stack that sets up MyCustomResource and shows how to get an attribute from it.
class MyStack(cdk.Stack):
def __init__(self, scope: cdk.App, id: str, **kwargs) -> None:
# A Stack that sets up MyCustomResource and shows how to get an
# attribute from it.

class MyStack(core.Stack):
def __init__(self, scope: core.App, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)

resource = MyCustomResource(
self, "DemoResource", message="CustomResource says hello"
self, "DemoResource",
message="CustomResource says hello",
)

# Publish the custom resource output
cdk.CfnOutput(
self,
"ResponseMessage",
core.CfnOutput(
self, "ResponseMessage",
description="The message that came back from the Custom Resource",
value=resource.response,
)


app = cdk.App()
app = core.App()
MyStack(app, "CustomResourceDemoStack")
app.run()
app.synth()
6 changes: 4 additions & 2 deletions python/custom-resource/custom-resource-handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ def main(event, context):
import cfnresponse
log.getLogger().setLevel(log.INFO)

# This needs to change if there are to be multiple resources in the same stack
# This needs to change if there are to be multiple resources
# in the same stack
physical_id = 'TheOnlyCustomResource'

try:
Expand All @@ -19,7 +20,8 @@ def main(event, context):
'Response': 'You said "%s"' % message
}

cfnresponse.send(event, context, cfnresponse.SUCCESS, attributes, physical_id)
cfnresponse.send(event, context, cfnresponse.SUCCESS,
attributes, physical_id)
except Exception as e:
log.exception(e)
# cfnresponse's error message is always "see CloudWatch"
Expand Down
32 changes: 18 additions & 14 deletions python/custom-resource/my_custom_resource.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
from aws_cdk import cdk
from aws_cdk import aws_cloudformation as cfn, aws_lambda as lambda_
from aws_cdk import (
aws_cloudformation as cfn,
aws_lambda as lambda_,
core
)


class MyCustomResource(cdk.Construct):
def __init__(self, scope: cdk.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id,)
class MyCustomResource(core.Construct):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id)

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

resource = cfn.CustomResource(
self, "Resource",
provider=cfn.CustomResourceProvider.lambda_(lambda_.SingletonFunction(
self,
"Singleton",
uuid="f7d4f730-4ee1-11e8-9c2d-fa7ae01bbebc",
code=lambda_.InlineCode(code_body),
handler="index.main",
timeout=300,
runtime=lambda_.Runtime.PYTHON27,
)),
provider=cfn.CustomResourceProvider.lambda_(
lambda_.SingletonFunction(
self, "Singleton",
uuid="f7d4f730-4ee1-11e8-9c2d-fa7ae01bbebc",
code=lambda_.InlineCode(code_body),
handler="index.main",
timeout=core.Duration.seconds(300),
runtime=lambda_.Runtime.PYTHON_3_7,
)
),
properties=kwargs,
)

Expand Down
14 changes: 7 additions & 7 deletions python/ecs/cluster/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
aws_autoscaling as autoscaling,
aws_ec2 as ec2,
aws_ecs as ecs,
cdk,
core,
)


class ECSCluster(cdk.Stack):
class ECSCluster(core.Stack):

def __init__(self, scope: cdk.Construct, id: str, **kwargs) -> None:
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, *kwargs)

vpc = ec2.Vpc(
Expand All @@ -21,10 +21,10 @@ def __init__(self, scope: cdk.Construct, id: str, **kwargs) -> None:
instance_type=ec2.InstanceType("t2.xlarge"),
machine_image=ecs.EcsOptimizedAmi(),
associate_public_ip_address=True,
update_type=autoscaling.UpdateType.ReplacingUpdate,
update_type=autoscaling.UpdateType.REPLACING_UPDATE,
desired_capacity=3,
vpc=vpc,
vpc_subnets={'subnetType': ec2.SubnetType.Public}
vpc_subnets={'subnetType': ec2.SubnetType.PUBLIC}
)

cluster = ecs.Cluster(
Expand All @@ -36,6 +36,6 @@ def __init__(self, scope: cdk.Construct, id: str, **kwargs) -> None:
cluster.add_capacity("DefaultAutoScalingGroup",
instance_type=ec2.InstanceType("t2.micro"))

app = cdk.App()
app = core.App()
ECSCluster(app, "MyFirstEcsCluster")
app.run()
app.synth()
12 changes: 6 additions & 6 deletions python/ecs/ecs-load-balanced-service/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
aws_ec2 as ec2,
aws_ecs as ecs,
aws_ecs_patterns as ecs_patterns,
cdk,
core,
)


class BonjourECS(cdk.Stack):
class BonjourECS(core.Stack):

def __init__(self, scope: cdk.Construct, id: str, **kwargs) -> None:
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, *kwargs)

vpc = ec2.Vpc(
Expand All @@ -31,11 +31,11 @@ def __init__(self, scope: cdk.Construct, id: str, **kwargs) -> None:
image=ecs.ContainerImage.from_registry("amazon/amazon-ecs-sample")
)

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

app = cdk.App()
app = core.App()
BonjourECS(app, "Bonjour")
app.run()
app.synth()
12 changes: 6 additions & 6 deletions python/ecs/ecs-service-with-advanced-alb-config/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
aws_ec2 as ec2,
aws_ecs as ecs,
aws_elasticloadbalancingv2 as elbv2,
cdk,
core,
)

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

# Create a cluster
vpc = ec2.Vpc(
Expand All @@ -32,7 +32,7 @@
container.add_port_mappings(
container_port=80,
host_port=8080,
protocol=ecs.Protocol.Tcp
protocol=ecs.Protocol.TCP
)

# Create Service
Expand Down Expand Up @@ -66,9 +66,9 @@
}
)

cdk.CfnOutput(
core.CfnOutput(
stack, "LoadBalancerDNS",
value=lb.load_balancer_dns_name
)

app.run()
app.synth()
17 changes: 10 additions & 7 deletions python/ecs/ecs-service-with-task-placement/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
aws_ec2 as ec2,
aws_ecs as ecs,
aws_elasticloadbalancingv2 as elbv2,
cdk,
core,
)

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

# Create a cluster
vpc = ec2.Vpc(
Expand Down Expand Up @@ -37,7 +37,7 @@
container.add_port_mappings(
container_port=80,
host_port=8080,
protocol=ecs.Protocol.Tcp
protocol=ecs.Protocol.TCP
)

# Create Service
Expand All @@ -46,7 +46,10 @@
cluster=cluster,
task_definition=task_definition,
)
service.place_packed_by(ecs.BinPackResource.Memory)
service.place_spread_across(ecs.BuiltInAttributes.AVAILABILITY_ZONE)

app.run()
service.add_placement_strategies(
ecs.PlacementStrategy.packed_by(ecs.BinPackResource.MEMORY))
service.add_placement_strategies(
ecs.PlacementStrategy.spread_across(
ecs.BuiltInAttributes.AVAILABILITY_ZONE))
app.synth()
12 changes: 6 additions & 6 deletions python/ecs/fargate-load-balanced-service/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
aws_ec2 as ec2,
aws_ecs as ecs,
aws_ecs_patterns as ecs_patterns,
cdk,
core,
)


class BonjourFargate(cdk.Stack):
class BonjourFargate(core.Stack):

def __init__(self, scope: cdk.Construct, id: str, **kwargs) -> None:
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, *kwargs)

# Create VPC and Fargate Cluster
Expand All @@ -29,11 +29,11 @@ def __init__(self, scope: cdk.Construct, id: str, **kwargs) -> None:
image=ecs.ContainerImage.from_registry("amazon/amazon-ecs-sample")
)

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

app = cdk.App()
app = core.App()
BonjourFargate(app, "Bonjour")
app.run()
app.synth()
Loading

0 comments on commit edc334e

Please sign in to comment.