From edc334e42bad4436bbbc77101e8efd301d9cd5a9 Mon Sep 17 00:00:00 2001 From: Mitch Garnaat Date: Wed, 3 Jul 2019 06:09:45 -0700 Subject: [PATCH] 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 --- .gitignore | 1 + python/application-load-balancer/app.py | 19 ++++++----- python/classic-load-balancer/app.py | 22 +++++++------ python/custom-resource/app.py | 22 +++++++------ .../custom-resource-handler.py | 6 ++-- python/custom-resource/my_custom_resource.py | 32 +++++++++++-------- python/ecs/cluster/app.py | 14 ++++---- python/ecs/ecs-load-balanced-service/app.py | 12 +++---- .../app.py | 12 +++---- .../ecs-service-with-task-placement/app.py | 17 ++++++---- .../ecs/fargate-load-balanced-service/app.py | 12 +++---- .../fargate-service-with-autoscaling/app.py | 16 +++++----- python/lambda-cron/app.py | 30 +++++++++++------ python/stepfunctions/app.py | 14 ++++---- 14 files changed, 128 insertions(+), 101 deletions(-) diff --git a/.gitignore b/.gitignore index 0acdad54e..ecaf3a667 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ __pycache__ *~ .cdk.staging cdk.out +.vscode diff --git a/python/application-load-balancer/app.py b/python/application-load-balancer/app.py index 815ea35b3..40e0b866d 100644 --- a/python/application-load-balancer/app.py +++ b/python/application-load-balancer/app.py @@ -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") @@ -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]) @@ -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() diff --git a/python/classic-load-balancer/app.py b/python/classic-load-balancer/app.py index d808cd5b7..6c77f8a90 100644 --- a/python/classic-load-balancer/app.py +++ b/python/classic-load-balancer/app.py @@ -1,29 +1,31 @@ -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) @@ -31,6 +33,6 @@ def __init__(self, app: cdk.App, id: str, **kwargs) -> None: 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() diff --git a/python/custom-resource/app.py b/python/custom-resource/app.py index 7ee535c1d..e15fa9989 100644 --- a/python/custom-resource/app.py +++ b/python/custom-resource/app.py @@ -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() diff --git a/python/custom-resource/custom-resource-handler.py b/python/custom-resource/custom-resource-handler.py index f51514d0d..32e95b28b 100644 --- a/python/custom-resource/custom-resource-handler.py +++ b/python/custom-resource/custom-resource-handler.py @@ -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: @@ -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" diff --git a/python/custom-resource/my_custom_resource.py b/python/custom-resource/my_custom_resource.py index f27b7e90d..194f86479 100644 --- a/python/custom-resource/my_custom_resource.py +++ b/python/custom-resource/my_custom_resource.py @@ -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, ) diff --git a/python/ecs/cluster/app.py b/python/ecs/cluster/app.py index faff44fc5..f8d03c1e6 100644 --- a/python/ecs/cluster/app.py +++ b/python/ecs/cluster/app.py @@ -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( @@ -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( @@ -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() diff --git a/python/ecs/ecs-load-balanced-service/app.py b/python/ecs/ecs-load-balanced-service/app.py index 2cff1cf1b..80b645384 100644 --- a/python/ecs/ecs-load-balanced-service/app.py +++ b/python/ecs/ecs-load-balanced-service/app.py @@ -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( @@ -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() diff --git a/python/ecs/ecs-service-with-advanced-alb-config/app.py b/python/ecs/ecs-service-with-advanced-alb-config/app.py index fc857f54c..4bb5013e7 100644 --- a/python/ecs/ecs-service-with-advanced-alb-config/app.py +++ b/python/ecs/ecs-service-with-advanced-alb-config/app.py @@ -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( @@ -32,7 +32,7 @@ container.add_port_mappings( container_port=80, host_port=8080, - protocol=ecs.Protocol.Tcp + protocol=ecs.Protocol.TCP ) # Create Service @@ -66,9 +66,9 @@ } ) -cdk.CfnOutput( +core.CfnOutput( stack, "LoadBalancerDNS", value=lb.load_balancer_dns_name ) -app.run() +app.synth() diff --git a/python/ecs/ecs-service-with-task-placement/app.py b/python/ecs/ecs-service-with-task-placement/app.py index e030d34da..da79c5c82 100644 --- a/python/ecs/ecs-service-with-task-placement/app.py +++ b/python/ecs/ecs-service-with-task-placement/app.py @@ -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( @@ -37,7 +37,7 @@ container.add_port_mappings( container_port=80, host_port=8080, - protocol=ecs.Protocol.Tcp + protocol=ecs.Protocol.TCP ) # Create Service @@ -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() diff --git a/python/ecs/fargate-load-balanced-service/app.py b/python/ecs/fargate-load-balanced-service/app.py index d49ac4dd1..3c9d95808 100644 --- a/python/ecs/fargate-load-balanced-service/app.py +++ b/python/ecs/fargate-load-balanced-service/app.py @@ -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 @@ -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() diff --git a/python/ecs/fargate-service-with-autoscaling/app.py b/python/ecs/fargate-service-with-autoscaling/app.py index e26d98e9d..6a663af81 100644 --- a/python/ecs/fargate-service-with-autoscaling/app.py +++ b/python/ecs/fargate-service-with-autoscaling/app.py @@ -2,13 +2,13 @@ aws_ec2 as ec2, aws_ecs as ecs, aws_ecs_patterns as ecs_patterns, - cdk, + core, ) -class AutoScalingFargateService(cdk.Stack): +class AutoScalingFargateService(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 a cluster @@ -36,15 +36,15 @@ def __init__(self, scope: cdk.Construct, id: str, **kwargs) -> None: scaling.scale_on_cpu_utilization( "CpuScaling", target_utilization_percent=50, - scale_in_cooldown_sec=60, - scale_out_cooldown_sec=60, + scale_in_cooldown=core.Duration.seconds(60), + scale_out_cooldown=core.Duration.seconds(60), ) - cdk.CfnOutput( + core.CfnOutput( self, "LoadBalancerDNS", value=fargate_service.load_balancer.load_balancer_dns_name ) -app = cdk.App() +app = core.App() AutoScalingFargateService(app, "aws-fargate-application-autoscaling") -app.run() +app.synth() diff --git a/python/lambda-cron/app.py b/python/lambda-cron/app.py index 6f1a31934..c40a7865e 100644 --- a/python/lambda-cron/app.py +++ b/python/lambda-cron/app.py @@ -1,30 +1,40 @@ -from aws_cdk import aws_events as events, aws_lambda as lambda_, cdk, aws_events_targets as targets +from aws_cdk import ( + aws_events as events, + aws_lambda as lambda_, + aws_events_targets as targets, + core, +) -class LambdaCronStack(cdk.Stack): - def __init__(self, app: cdk.App, id: str) -> None: +class LambdaCronStack(core.Stack): + def __init__(self, app: core.App, id: str) -> None: super().__init__(app, id) with open("lambda-handler.py", encoding="utf8") as fp: handler_code = fp.read() lambdaFn = lambda_.Function( - self, - "Singleton", + self, "Singleton", code=lambda_.InlineCode(handler_code), handler="index.main", - timeout=300, - runtime=lambda_.Runtime.PYTHON27, + timeout=core.Duration.seconds(300), + runtime=lambda_.Runtime.PYTHON_3_7, ) # Run every day at 6PM UTC # See https://docs.aws.amazon.com/lambda/latest/dg/tutorial-scheduled-events-schedule-expressions.html rule = events.Rule( - self, "Rule", schedule_expression="cron(0 18 ? * MON-FRI *)" + self, "Rule", + schedule=events.Schedule.cron( + minute='0', + hour='18', + month='*', + week_day='MON-FRI', + year='*'), ) rule.add_target(targets.LambdaFunction(lambdaFn)) -app = cdk.App() +app = core.App() LambdaCronStack(app, "LambdaCronExample") -app.run() +app.synth() diff --git a/python/stepfunctions/app.py b/python/stepfunctions/app.py index 887fe5446..78fb3ae6d 100644 --- a/python/stepfunctions/app.py +++ b/python/stepfunctions/app.py @@ -2,12 +2,12 @@ from aws_cdk import ( aws_stepfunctions as sfn, aws_stepfunctions_tasks as sfn_tasks, - cdk, + core, ) -class JobPollerStack(cdk.Stack): - def __init__(self, app: cdk.App, id: str, **kwargs) -> None: +class JobPollerStack(core.Stack): + def __init__(self, app: core.App, id: str, **kwargs) -> None: super().__init__(app, id, **kwargs) submit_job_activity = sfn.Activity( @@ -24,7 +24,7 @@ def __init__(self, app: cdk.App, id: str, **kwargs) -> None: ) wait_x = sfn.Wait( self, "Wait X Seconds", - duration=sfn.WaitDuration.seconds_path('$.wait_time'), + time=sfn.WaitTime.seconds_path('$.wait_time'), ) get_status = sfn.Task( self, "Get Job Status", @@ -59,9 +59,9 @@ def __init__(self, app: cdk.App, id: str, **kwargs) -> None: sfn.StateMachine( self, "StateMachine", definition=definition, - timeout_sec=30, + timeout=core.Duration.seconds(30), ) -app = cdk.App() +app = core.App() JobPollerStack(app, "aws-stepfunctions-integ") -app.run() +app.synth()