如何使用CloudFormation在现有EC2实例上启动容器?

时间:2016-07-20 15:44:56

标签: amazon-web-services amazon-ec2 amazon-cloudformation amazon-ecs

方案如下:我有3个EC2实例(A,B和C)都运行ECS优化的AMI。我想编写一个带有任务定义的CloudFormation模板,该任务定义允许任务仅在A上运行。我该怎么做?

我见过的所有CloudFormation示例都需要创建一个新的EC2实例,这不是我想要的。

1 个答案:

答案 0 :(得分:2)

将任务固定到主机的唯一方法是使用 start-task 通过AWS CLI执行此操作:http://docs.aws.amazon.com/cli/latest/reference/ecs/start-task.html

对于通过Cloudformation运行ECS任务,在CFT模板范围内创建的唯一方法是创建服务。这是一个未经测试的CFT模板:



{
    "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"
                }
            }
        }
    }
}