【问题标题】:Different behavior when create ECS cluster using CloudFormation and AWS Console使用 CloudFormation 和 AWS 控制台创建 ECS 集群时的不同行为
【发布时间】:2018-08-23 05:26:21
【问题描述】:

使用CloudFormation创建时,没有Scale ECS Instances按钮,缩放实例需要找到Auto Scaling Group来缩放不是我的实例想要。

使用 AWS 控制台 创建时,有一个 Scale ECS Instances 按钮。

我希望在使用 CloudFormation 创建时拥有该按钮。

我遗漏了什么或做错了什么?

{
  "AWSTemplateFormatVersion":"2010-09-09",
  "Description":"Create ECS Cluster, ECS Task Definitions, Lambdas, CloudWatchs for different country and environment.",
  "Parameters":{
    "CountryName":{
      "Type":"String",
      "Description":"Auto inclusion launch country name.",
      "AllowedValues":[
        "my",
        "sg"
      ]
    },
    "EnvironmentName":{
      "Type":"String",
      "Description":"An environment name that will be suffixed to resource names.",
      "AllowedValues":[
        "dev",
        "stage",
        "live"
      ]
    },
    "KeyName":{
      "Type":"AWS::EC2::KeyPair::KeyName",
      "Description":"Name of an existing EC2 KeyPair to enable SSH access to the ECS instance."
    },
    "VpcId":{
      "Type":"AWS::EC2::VPC::Id",
      "Description":"Select a VPC to deploy the ECS instance."
    },
    "SubnetId":{
      "Type":"List<AWS::EC2::Subnet::Id>",
      "Description":"Select at least two subnets in your selected VPC to deploy the ECS instance."
    },
    "InstanceType":{
      "Description":"ECS instance type",
      "Type":"String",
      "Default":"t2.micro",
      "AllowedValues":[
        "t2.micro",
        "t2.small",
        "t2.medium",
        "t2.large",
        "m3.medium",
        "m3.large",
        "m3.xlarge",
        "m3.2xlarge",
        "m4.large",
        "m4.xlarge",
        "m4.2xlarge",
        "m4.4xlarge",
        "m4.10xlarge",
        "c4.large",
        "c4.xlarge",
        "c4.2xlarge",
        "c4.4xlarge",
        "c4.8xlarge",
        "c3.large",
        "c3.xlarge",
        "c3.2xlarge",
        "c3.4xlarge",
        "c3.8xlarge",
        "r3.large",
        "r3.xlarge",
        "r3.2xlarge",
        "r3.4xlarge",
        "r3.8xlarge",
        "i2.xlarge",
        "i2.2xlarge",
        "i2.4xlarge",
        "i2.8xlarge"
      ],
      "ConstraintDescription":"Please choose a valid instance type."
    }
  },
  "Mappings":{
    "AWSRegionToAMI":{
      "us-east-1":{
        "AMIID":"ami-a7a242da"
      },
      "us-east-2":{
        "AMIID":"ami-b86a5ddd"
      },
      "us-west-1":{
        "AMIID":"ami-9ad4dcfa"
      },
      "us-west-2":{
        "AMIID":"ami-92e06fea"
      },
      "eu-west-1":{
        "AMIID":"ami-0693ed7f"
      },
      "eu-west-2":{
        "AMIID":"ami-f4e20693"
      },
      "eu-west-3":{
        "AMIID":"ami-698b3d14"
      },
      "eu-central-1":{
        "AMIID":"ami-0799fa68"
      },
      "ap-northeast-1":{
        "AMIID":"ami-68ef940e"
      },
      "ap-northeast-2":{
        "AMIID":"ami-a5dd70cb"
      },
      "ap-southeast-1":{
        "AMIID":"ami-0a622c76"
      },
      "ap-southeast-2":{
        "AMIID":"ami-ee884f8c"
      },
      "ca-central-1":{
        "AMIID":"ami-5ac94e3e"
      },
      "ap-south-1":{
        "AMIID":"ami-2e461a41"
      },
      "sa-east-1":{
        "AMIID":"ami-d44008b8"
      }
    }
  },
  "Resources":{
    "ECSCluster":{
      "Type":"AWS::ECS::Cluster",
      "Properties":{
        "ClusterName":{
          "Fn::Join":[
            "-",
            [
              {
                "Ref":"AWS::StackName"
              },
              {
                "Ref":"EnvironmentName"
              }
            ]
          ]
        }
      }
    },
    "ECSSecurityGroup":{
      "Type":"AWS::EC2::SecurityGroup",
      "Properties":{
        "GroupDescription":"Auto Inclusion Security Group",
        "VpcId":{
          "Ref":"VpcId"
        }
      }
    },
    "ECSSecurityGroupSSHinbound":{
      "Type":"AWS::EC2::SecurityGroupIngress",
      "Properties":{
        "GroupId":{
          "Ref":"ECSSecurityGroup"
        },
        "IpProtocol":"tcp",
        "FromPort":"22",
        "ToPort":"22",
        "CidrIp":"0.0.0.0/0"
      }
    },
    "ECSAutoScalingGroup":{
      "Type":"AWS::AutoScaling::AutoScalingGroup",
      "Properties":{
        "VPCZoneIdentifier":{
          "Ref":"SubnetId"
        },
        "LaunchConfigurationName":{
          "Ref":"ECSLaunchConfiguration"
        },
        "MinSize":"0",
        "MaxSize":"1",
        "DesiredCapacity":"1"
      }
    },
    "ECSLaunchConfiguration":{
      "Type":"AWS::AutoScaling::LaunchConfiguration",
      "Properties":{
        "ImageId":{
          "Fn::FindInMap":[
            "AWSRegionToAMI",
            {
              "Ref":"AWS::Region"
            },
            "AMIID"
          ]
        },
        "InstanceType":{
          "Ref":"InstanceType"
        },
        "IamInstanceProfile":{
          "Ref":"EC2InstanceProfile"
        },
        "KeyName":{
          "Ref":"KeyName"
        },
        "SecurityGroups":[
          {
            "Ref":"ECSSecurityGroup"
          }
        ],
        "UserData":{
          "Fn::Base64":{
            "Fn::Join":[
              "",
              [
                "#!/bin/bash\n",
                "echo ECS_CLUSTER=",
                {
                  "Ref":"ECSCluster"
                },
                " >> /etc/ecs/ecs.config"
              ]
            ]
          }
        }
      }
    },
    "EC2Role":{
      "Type":"AWS::IAM::Role",
      "Properties":{
        "AssumeRolePolicyDocument":{
          "Statement":[
            {
              "Effect":"Allow",
              "Principal":{
                "Service":[
                  "ec2.amazonaws.com"
                ]
              },
              "Action":[
                "sts:AssumeRole"
              ]
            }
          ]
        },
        "Path":"/",
        "ManagedPolicyArns":[
          "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
        ],
        "Policies":[
          {
            "PolicyName":"auto-inclusion",
            "PolicyDocument":{
              "Version":"2012-10-17",
              "Statement":[
                {
                  "Effect":"Allow",
                  "Action":[
                    "s3:GetObject",
                    "s3:ListBucket",
                    "s3:PutObject",
                    "s3:DeleteObject"
                  ],
                  "Resource":[
                    "*"
                  ]
                },
                {
                  "Effect":"Allow",
                  "Action":[
                    "dynamodb:*"
                  ],
                  "Resource":[
                    {
                      "Fn::Join":[
                        "",
                        [
                          "arn:aws:dynamodb:",
                          {
                            "Ref":"AWS::Region"
                          },
                          ":",
                          {
                            "Ref":"AWS::AccountId"
                          },
                          ":table/ai-process-tracking-",
                          {
                            "Ref":"EnvironmentName"
                          }
                        ]
                      ]
                    }
                  ]
                }
              ]
            }
          }
        ]
      }
    },
    "EC2InstanceProfile":{
      "Type":"AWS::IAM::InstanceProfile",
      "Properties":{
        "Path":"/",
        "Roles":[
          {
            "Ref":"EC2Role"
          }
        ]
      }
    }
  },
  "Outputs":{
    "ecscluster":{
      "Value":{
        "Ref":"ECSCluster"
      }
    }
  }
}

【问题讨论】:

  • 当你说“[...] 这不是我想要的。”你能详细说明一下吗?
  • 一般来说,扩展由 CloudFormation 创建的资源的正确方法是修改您的 CloudFormation 模板,然后进行更新。否则,您的资源与模板不匹配,您可能会失去通过 CloudFormation 进行自动化的能力。
  • @LaurentJalbertSimard 我希望在使用 CloudFormation 创建时拥有Scale ECS Instances 按钮。
  • @jarmod 我知道这是最好的做法,但我很好奇为什么会有如此不同
  • 我必须重申@jarmod 所说的内容。你真的应该拥抱 IaC 而忘记蓝色按钮。您甚至可以花时间根据 ECS 的指标设置动态自动缩放。所以这对你来说甚至都不是障碍。

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


【解决方案1】:

更新:如果您的集群不是在 2015 年 11 月 24 日之后使用控制台首次运行体验创建的,则您无法从 Amazon ECS 控制台扩展您的集群。

原文:

这只是如何获取按钮的分步说明,您需要在 CloudFormation 模板中反映这一点。

  • 创建一个空的 ECS 集群,例如 my-test-cluster
  • 转到 CloudFormation,并使用您的模板创建名为 EC2ContainerService-my-test-cluster 的堆栈
  • 模板可能需要具备以下条件
Outputs:
  TemplateVersion:
    Value: '2.0.0'
  UsedByECSCreateCluster:
    Value: 'true'

【讨论】:

  • 如果您的集群不是在 2015 年 11 月 24 日之后使用控制台首次运行体验创建的,则您无法从 Amazon ECS 控制台扩展您的集群。
【解决方案2】:

更新:我在官方documentation 中找到了这个。

如果您的集群是在 2015 年 11 月 24 日之后使用 console first-run experience 创建的,那么与关联的 Auto Scaling 组 为您的集群创建的 AWS CloudFormation 堆栈可以扩展 或向下添加或删除容器实例。您可以执行此操作 在 Amazon ECS 控制台中进行扩展操作。

如果您的集群不是在 2015 年 11 月 24 日之后使用控制台首次运行体验创建的,那么您将无法扩展您的 来自 Amazon ECS 控制台的集群。但是,您仍然可以修改 Auto Scaling 中与您的集群关联的现有 Auto Scaling 组 缩放控制台。


参考:Scaling Cluster

如果出现 Scale ECS Instances 按钮,那么您可以扩展您的 在下一步中进行集群。如果没有,您必须手动调整您的自动 扩展组以扩大或缩小您的实例,或者您可以手动 在 Amazon EC2 中启动或终止您的容器实例 控制台。

【讨论】:

猜你喜欢
  • 2015-08-23
  • 1970-01-01
  • 2019-03-02
  • 2013-03-21
  • 2017-09-13
  • 2020-12-01
  • 1970-01-01
  • 2018-10-08
  • 2018-02-27
相关资源
最近更新 更多