old_contents: from troposphere import (
      AWS_REGION,
      autoscaling,
      FindInMap,
      iam,
      Parameter,
      Ref,
  )
  
  from troposphere.ecs import (
      Cluster,
  )
  
  from .template import template
  
  
  container_instance_type = Ref(template.add_parameter(Parameter(
      "ContainerInstanceType",
      Description="The container instance type",
      Type="String",
      Default="t2.micro",
      AllowedValues=["t2.micro", "t2.small", "t2.medium"]
  )))
  
  
  template.add_mapping("ECSRegionMap", {
      "eu-west-1": {"AMI": "ami-4e6ffe3d"},
      "us-east-1": {"AMI": "ami-8f7687e2"},
      "us-west-2": {"AMI": "ami-84b44de4"},
  })
  
  
  # ECS cluster
  cluster = Cluster(
      "Cluster",
      template=template,
  )
  
  
  # ECS container role
  container_instance_role = iam.Role(
      "ContainerInstanceRole",
      template=template,
      AssumeRolePolicyDocument=dict(Statement=[dict(
          Effect="Allow",
          Principal=dict(Service=["ec2.amazonaws.com"]),
          Action=["sts:AssumeRole"],
      )]),
      Path="/",
      Policies=[
      ]
  )
  
  
  # ECS container instance profile
  container_instance_profile = iam.InstanceProfile(
      "ContainerInstanceProfile",
      template=template,
      Path="/",
      Roles=[Ref(container_instance_role)],
  )
  
  
  container_instance_configuration_name = "ContainerLaunchConfiguration"
  
  
  container_instance_configuration = autoscaling.LaunchConfiguration(
      container_instance_configuration_name,
      template=template,
      InstanceType=container_instance_type,
      ImageId=FindInMap("ECSRegionMap", Ref(AWS_REGION), "AMI"),
      IamInstanceProfile=Ref(container_instance_profile),
  )
  
new_contents: from troposphere import (
      AWS_REGION,
      autoscaling,
      cloudformation,
      FindInMap,
      iam,
      Join,
      Parameter,
      Ref,
  )
  
  from troposphere.ecs import (
      Cluster,
  )
  
  from .template import template
  
  
  container_instance_type = Ref(template.add_parameter(Parameter(
      "ContainerInstanceType",
      Description="The container instance type",
      Type="String",
      Default="t2.micro",
      AllowedValues=["t2.micro", "t2.small", "t2.medium"]
  )))
  
  
  template.add_mapping("ECSRegionMap", {
      "eu-west-1": {"AMI": "ami-4e6ffe3d"},
      "us-east-1": {"AMI": "ami-8f7687e2"},
      "us-west-2": {"AMI": "ami-84b44de4"},
  })
  
  
  # ECS cluster
  cluster = Cluster(
      "Cluster",
      template=template,
  )
  
  
  # ECS container role
  container_instance_role = iam.Role(
      "ContainerInstanceRole",
      template=template,
      AssumeRolePolicyDocument=dict(Statement=[dict(
          Effect="Allow",
          Principal=dict(Service=["ec2.amazonaws.com"]),
          Action=["sts:AssumeRole"],
      )]),
      Path="/",
      Policies=[
          iam.Policy(
              PolicyName="ECSManagementPolicy",
              PolicyDocument=dict(
                  Statement=[dict(
                      Effect="Allow",
                      Action=[
                          "ecs:*",
                      ],
                      Resource="*",
                  )],
              ),
          ),
      ]
  )
  
  
  # ECS container instance profile
  container_instance_profile = iam.InstanceProfile(
      "ContainerInstanceProfile",
      template=template,
      Path="/",
      Roles=[Ref(container_instance_role)],
  )
  
  
  container_instance_configuration_name = "ContainerLaunchConfiguration"
  
  
  container_instance_configuration = autoscaling.LaunchConfiguration(
      container_instance_configuration_name,
      template=template,
      Metadata=autoscaling.Metadata(
          cloudformation.Init(dict(
              config=cloudformation.InitConfig(
                  commands=dict(
                      register_cluster=dict(command=Join("", [
                          "#!/bin/bash\n",
                          # Register the cluster
                          "echo ECS_CLUSTER=",
                          Ref(cluster),
                          " >> /etc/ecs/ecs.config\n",
                      ]))
                  ),
              )
          ))
      ),
      InstanceType=container_instance_type,
      ImageId=FindInMap("ECSRegionMap", Ref(AWS_REGION), "AMI"),
      IamInstanceProfile=Ref(container_instance_profile),
  )
  
subject: Make container instances to register to the cluster