Skip to content

Commit 5eda018

Browse files
authored
Updating Python examples to 0.36.1 (#64)
* WIP: Updating Python examples to 0.36.1 * Adding some ECS examples * Adding more ECS examples * Adding final ECS examples
1 parent 35ab76d commit 5eda018

File tree

14 files changed

+128
-101
lines changed

14 files changed

+128
-101
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ __pycache__
88
*~
99
.cdk.staging
1010
cdk.out
11+
.vscode
1112

python/application-load-balancer/app.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
aws_autoscaling as autoscaling,
44
aws_ec2 as ec2,
55
aws_elasticloadbalancingv2 as elbv2,
6-
cdk,
6+
core,
77
)
88

99

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

1414
vpc = ec2.Vpc(self, "VPC")
@@ -17,13 +17,16 @@ def __init__(self, app: cdk.App, id: str) -> None:
1717
self,
1818
"ASG",
1919
vpc=vpc,
20-
instance_type=ec2.InstanceTypePair(
21-
ec2.InstanceClass.Burstable2, ec2.InstanceSize.Micro
20+
instance_type=ec2.InstanceType.of(
21+
ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO
2222
),
2323
machine_image=ec2.AmazonLinuxImage(),
2424
)
2525

26-
lb = elbv2.ApplicationLoadBalancer(self, "LB", vpc=vpc, internet_facing=True)
26+
lb = elbv2.ApplicationLoadBalancer(
27+
self, "LB",
28+
vpc=vpc,
29+
internet_facing=True)
2730

2831
listener = lb.add_listener("Listener", port=80)
2932
listener.add_targets("Target", port=80, targets=[asg])
@@ -32,6 +35,6 @@ def __init__(self, app: cdk.App, id: str) -> None:
3235
asg.scale_on_request_count("AModestLoad", target_requests_per_second=1)
3336

3437

35-
app = cdk.App()
38+
app = core.App()
3639
LoadBalancerStack(app, "LoadBalancerStack")
37-
app.run()
40+
app.synth()
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,38 @@
1-
from aws_cdk import cdk
21
from aws_cdk import (
32
aws_autoscaling as autoscaling,
43
aws_ec2 as ec2,
54
aws_elasticloadbalancing as elb,
5+
core
66
)
77

88

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

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

1515
asg = autoscaling.AutoScalingGroup(
16-
self,
17-
"ASG",
16+
self, "ASG",
1817
vpc=vpc,
19-
instance_type=ec2.InstanceTypePair(
20-
ec2.InstanceClass.Burstable2, ec2.InstanceSize.Micro
18+
instance_type=ec2.InstanceType.of(
19+
ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO
2120
),
2221
machine_image=ec2.AmazonLinuxImage(),
2322
)
2423

2524
lb = elb.LoadBalancer(
26-
self, "LB", vpc=vpc, internet_facing=True, health_check={"port": 80}
25+
self, "LB",
26+
vpc=vpc,
27+
internet_facing=True,
28+
health_check={"port": 80}
2729
)
2830
lb.add_target(asg)
2931

3032
listener = lb.add_listener(external_port=80)
3133
listener.connections.allow_default_port_from_any_ipv4("Open to the world")
3234

3335

34-
app = cdk.App()
36+
app = core.App()
3537
LoadBalancerStack(app, "LoadBalancerStack")
36-
app.run()
38+
app.synth()

python/custom-resource/app.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
1-
from aws_cdk import cdk
1+
from aws_cdk import core
22

33
from my_custom_resource import MyCustomResource
44

55

6-
# A Stack that sets up MyCustomResource and shows how to get an attribute from it.
7-
class MyStack(cdk.Stack):
8-
def __init__(self, scope: cdk.App, id: str, **kwargs) -> None:
6+
# A Stack that sets up MyCustomResource and shows how to get an
7+
# attribute from it.
8+
9+
class MyStack(core.Stack):
10+
def __init__(self, scope: core.App, id: str, **kwargs) -> None:
911
super().__init__(scope, id, **kwargs)
1012

1113
resource = MyCustomResource(
12-
self, "DemoResource", message="CustomResource says hello"
14+
self, "DemoResource",
15+
message="CustomResource says hello",
1316
)
1417

1518
# Publish the custom resource output
16-
cdk.CfnOutput(
17-
self,
18-
"ResponseMessage",
19+
core.CfnOutput(
20+
self, "ResponseMessage",
1921
description="The message that came back from the Custom Resource",
2022
value=resource.response,
2123
)
2224

2325

24-
app = cdk.App()
26+
app = core.App()
2527
MyStack(app, "CustomResourceDemoStack")
26-
app.run()
28+
app.synth()

python/custom-resource/custom-resource-handler.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ def main(event, context):
33
import cfnresponse
44
log.getLogger().setLevel(log.INFO)
55

6-
# This needs to change if there are to be multiple resources in the same stack
6+
# This needs to change if there are to be multiple resources
7+
# in the same stack
78
physical_id = 'TheOnlyCustomResource'
89

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

22-
cfnresponse.send(event, context, cfnresponse.SUCCESS, attributes, physical_id)
23+
cfnresponse.send(event, context, cfnresponse.SUCCESS,
24+
attributes, physical_id)
2325
except Exception as e:
2426
log.exception(e)
2527
# cfnresponse's error message is always "see CloudWatch"

python/custom-resource/my_custom_resource.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
1-
from aws_cdk import cdk
2-
from aws_cdk import aws_cloudformation as cfn, aws_lambda as lambda_
1+
from aws_cdk import (
2+
aws_cloudformation as cfn,
3+
aws_lambda as lambda_,
4+
core
5+
)
36

47

5-
class MyCustomResource(cdk.Construct):
6-
def __init__(self, scope: cdk.Construct, id: str, **kwargs) -> None:
7-
super().__init__(scope, id,)
8+
class MyCustomResource(core.Construct):
9+
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
10+
super().__init__(scope, id)
811

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

1215
resource = cfn.CustomResource(
1316
self, "Resource",
14-
provider=cfn.CustomResourceProvider.lambda_(lambda_.SingletonFunction(
15-
self,
16-
"Singleton",
17-
uuid="f7d4f730-4ee1-11e8-9c2d-fa7ae01bbebc",
18-
code=lambda_.InlineCode(code_body),
19-
handler="index.main",
20-
timeout=300,
21-
runtime=lambda_.Runtime.PYTHON27,
22-
)),
17+
provider=cfn.CustomResourceProvider.lambda_(
18+
lambda_.SingletonFunction(
19+
self, "Singleton",
20+
uuid="f7d4f730-4ee1-11e8-9c2d-fa7ae01bbebc",
21+
code=lambda_.InlineCode(code_body),
22+
handler="index.main",
23+
timeout=core.Duration.seconds(300),
24+
runtime=lambda_.Runtime.PYTHON_3_7,
25+
)
26+
),
2327
properties=kwargs,
2428
)
2529

python/ecs/cluster/app.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
aws_autoscaling as autoscaling,
33
aws_ec2 as ec2,
44
aws_ecs as ecs,
5-
cdk,
5+
core,
66
)
77

88

9-
class ECSCluster(cdk.Stack):
9+
class ECSCluster(core.Stack):
1010

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

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

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

39-
app = cdk.App()
39+
app = core.App()
4040
ECSCluster(app, "MyFirstEcsCluster")
41-
app.run()
41+
app.synth()

python/ecs/ecs-load-balanced-service/app.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
aws_ec2 as ec2,
33
aws_ecs as ecs,
44
aws_ecs_patterns as ecs_patterns,
5-
cdk,
5+
core,
66
)
77

88

9-
class BonjourECS(cdk.Stack):
9+
class BonjourECS(core.Stack):
1010

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

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

34-
cdk.CfnOutput(
34+
core.CfnOutput(
3535
self, "LoadBalancerDNS",
3636
value=ecs_service.load_balancer.load_balancer_dns_name
3737
)
3838

39-
app = cdk.App()
39+
app = core.App()
4040
BonjourECS(app, "Bonjour")
41-
app.run()
41+
app.synth()

python/ecs/ecs-service-with-advanced-alb-config/app.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
aws_ec2 as ec2,
33
aws_ecs as ecs,
44
aws_elasticloadbalancingv2 as elbv2,
5-
cdk,
5+
core,
66
)
77

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

1111
# Create a cluster
1212
vpc = ec2.Vpc(
@@ -32,7 +32,7 @@
3232
container.add_port_mappings(
3333
container_port=80,
3434
host_port=8080,
35-
protocol=ecs.Protocol.Tcp
35+
protocol=ecs.Protocol.TCP
3636
)
3737

3838
# Create Service
@@ -66,9 +66,9 @@
6666
}
6767
)
6868

69-
cdk.CfnOutput(
69+
core.CfnOutput(
7070
stack, "LoadBalancerDNS",
7171
value=lb.load_balancer_dns_name
7272
)
7373

74-
app.run()
74+
app.synth()

python/ecs/ecs-service-with-task-placement/app.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
aws_ec2 as ec2,
33
aws_ecs as ecs,
44
aws_elasticloadbalancingv2 as elbv2,
5-
cdk,
5+
core,
66
)
77

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

1111
# Create a cluster
1212
vpc = ec2.Vpc(
@@ -37,7 +37,7 @@
3737
container.add_port_mappings(
3838
container_port=80,
3939
host_port=8080,
40-
protocol=ecs.Protocol.Tcp
40+
protocol=ecs.Protocol.TCP
4141
)
4242

4343
# Create Service
@@ -46,7 +46,10 @@
4646
cluster=cluster,
4747
task_definition=task_definition,
4848
)
49-
service.place_packed_by(ecs.BinPackResource.Memory)
50-
service.place_spread_across(ecs.BuiltInAttributes.AVAILABILITY_ZONE)
5149

52-
app.run()
50+
service.add_placement_strategies(
51+
ecs.PlacementStrategy.packed_by(ecs.BinPackResource.MEMORY))
52+
service.add_placement_strategies(
53+
ecs.PlacementStrategy.spread_across(
54+
ecs.BuiltInAttributes.AVAILABILITY_ZONE))
55+
app.synth()

0 commit comments

Comments
 (0)