"""Generating CloudFormation template.""" from troposphere.ecs import ( TaskDefinition, ContainerDefinition ) from troposphere import ecs from awacs.aws import ( Allow, Statement, Principal, Policy ) from troposphere.iam import Role from troposphere import ( Parameter, Ref, Template, Join, ImportValue, Select, Split, ) from awacs.sts import AssumeRole t = Template() t.add_description("Effective DevOps in AWS: ECS service - Helloworld") t.add_parameter(Parameter( "Tag", Type="String", Default="latest", Description="Tag to deploy" )) t.add_resource(TaskDefinition( "task", ContainerDefinitions=[ ContainerDefinition( Image=Join("", [ Ref("AWS::AccountId"), ".dkr.ecr.", Ref("AWS::Region"), ".amazonaws.com", "/", ImportValue("helloworld-repo"), ":", Ref("Tag")]), Memory=32, Cpu=256, Name="helloworld", PortMappings=[ecs.PortMapping( ContainerPort=3000)] ) ], )) t.add_resource(Role( "ServiceRole", AssumeRolePolicyDocument=Policy( Statement=[ Statement( Effect=Allow, Action=[AssumeRole], Principal=Principal("Service", ["ecs.amazonaws.com"]) ) ] ), Path="/", ManagedPolicyArns=[ 'arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceRole'] )) t.add_resource(ecs.Service( "service", Cluster=ImportValue( Join( "-", [Select(0, Split("-", Ref("AWS::StackName"))), "cluster-id"] ) ), DesiredCount=1, TaskDefinition=Ref("task"), LoadBalancers=[ecs.LoadBalancer( ContainerName="helloworld", ContainerPort=3000, TargetGroupArn=ImportValue( Join( "-", [Select(0, Split("-", Ref("AWS::StackName"))), "alb-target-group"] ), ), )], Role=Ref("ServiceRole") )) print(t.to_json())