4
votes

The scenario is the following: I have 3 EC2 instances (A, B and C) all running the ECS-optimized AMI. I would like to write a CloudFormation template with a task definition that lets the task only run on A. How do I do that?

All CloudFormation examples I've seen require the creation of a new EC2 instance, which is not what I want.

1

1 Answers

2
votes

The only way to pin a task to a host is to do it through the AWS CLI with start-task: http://docs.aws.amazon.com/cli/latest/reference/ecs/start-task.html

As for running an ECS task through Cloudformation, the only way to both create and start it all in the scope of a CFT template is to create a service. Here's an untested CFT template:

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "Curator runner",
    "Parameters": {
        "CpuUnits": {
            "Type": "Number",
            "Default": 0,
            "Description": "The number of CPU Units to allocate."
        },
        "Memory": {
            "Type": "Number",
            "Default": 256,
            "Description": "The amount of Memory (MB) to allocate."
        },
        "ClusterName": {
            "Type": "String",
            "Description": "The cluster to run the ecs tasks on."
        },
        "DockerImageUrl": {
            "Type": "String",
            "Description": "The URL for the docker image. Example: 354500939573.dkr.ecr.us-east-1.amazonaws.com/something:latest"
        }
    },
    "Resources": {
        "SomeTask": {
            "Type": "AWS::ECS::TaskDefinition",
            "Properties": {
                "ContainerDefinitions": [{
                    "Memory": {
                        "Ref": "Memory"
                    },
                    "Name": "something",
                    "Image": {
                        "Ref": "DockerImageUrl"
                    },
                    "Cpu": {
                        "Ref": "CpuUnits"
                    }
                }],
                "Volumes": []
            }
        },
        "service": {
            "Type": "AWS::ECS::Service",
            "Properties": {
                "Cluster": {
                    "Ref": "ClusterName"
                },
                "DesiredCount": "1",
                "TaskDefinition": {
                    "Ref": "SomeTask"
                }
            }
        }
    }
}