方案如下:我有3个EC2实例(A,B和C)都运行ECS优化的AMI.我想编写一个带有任务定义的CloudFormation模板,该任务定义允许任务仅在A上运行.我该怎么做?
我见过的所有CloudFormation示例都需要创建一个新的EC2实例,这不是我想要的.
最佳答案 将任务固定到主机的唯一方法是使用start-task:
http://docs.aws.amazon.com/cli/latest/reference/ecs/start-task.html通过AWS CLI执行此操作
至于通过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"
}
}
}
}
}