AWS Cloudformation如何重启创建的实例,然后再次安装脚本的一部分

时间:2018-11-25 23:00:36

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

这是我要做的:

  • 我必须使用EC2 cloudformation模板创建一个实例。
  • 在安装某些软件包之后,我想通过云形成模板本身重新启动实例。
  • 实例重新启动后,我必须完成剩余脚本的执行。

请建议我该怎么做。

这是我当前的模板:


    {
  "AWSTemplateFormatVersion" : "2010-09-09",

  "Description" : "",
  "Parameters": {
    "VPCID": {
      "Description": "The VPC for this  instance",
      "Type": "AWS::EC2::VPC::Id",
    },
    "SubnetID": {
      "Description": "The Subnet for this instance",
      "Type": "AWS::EC2::Subnet::Id",
    },
    "AllowedCIDR": {
      "Description": "IP address range (in CIDR notation) of the client that will be allowed to connect to the cluster using SSH e.g., 203.0.113.5/32",
      "AllowedPattern": "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})",
      "Type": "String",
      "MinLength": "9",
      "MaxLength": "18",
      "Default": "10.0.0.0/16",
      "ConstraintDescription": "must be a valid CIDR range of the form x.x.x.x/x"
    },
    "SSHKeyName": {
      "Description": "The EC2 Key Pair to allow SSH access to the instance",
      "Type": "AWS::EC2::KeyPair::KeyName",
    },
    "TypeOfInstance": {
      "Type": "String",
      "Default": "t2.medium",
      "Description": "Enter t2.medium, t2.large, m3.large, m4.large, m4.xlarge, etc.",
      "ConstraintDescription": "Must be a valid EC2 instance type."
    }
  },
  "Resources": {
    "Ec2Instance": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "SecurityGroupIds": [
          {
            "Ref": "InstanceSecurityGroup"
          }
        ],
        "KeyName": {
          "Ref": "SSHKeyName"
        },
        "ImageId": "ami-a8d369c0",
        "SubnetId": { "Ref": "SubnetID" },
        "InstanceType": { "Ref": "TypeOfInstance" },
        "UserData"       : { "Fn::Base64" : { "Fn::Join" : ["", [
          "#!/bin/bash -xe\n",
          "touch /tmp/testfile\n",
          "yum -y install rng-tools\n",
          "systemctl start rngd\n",
          "systemctl enable rngd\n",
          "yum update -y \n",

          "echo \"################### Install Packages #######################\"\n",


          "reboot \n",


          "echo \"################### Install Remaining packages and configuration #######################\"\n",

        ]]}}
    },
    "InstanceSecurityGroup": {
      "Type": "AWS::EC2::SecurityGroup",
      "Properties": {
        "GroupDescription": "Enable SSH access via port 22",
        "VpcId" : {
          "Ref" : "VPCID"
        },
        "GroupName": "my-securitygroup",
        "SecurityGroupIngress": [
          {
            "IpProtocol": "tcp",
            "FromPort": "22",
            "ToPort": "22",
            "CidrIp": "0.0.0.0/0"
          }
        ]
      }
    }
  }
}

2 个答案:

答案 0 :(得分:0)

由于实例停止并重新启动后,cloudformation无法触发用户数据脚本的中途,因此,我可以想到一种解决方法。

在重新启动之前在实例上保存某种标志(例如cfn-userdata-script-continue)。将脚本的其余部分下载到实例,并将其保存到预定义的位置

重新启动后,检查此标志的存在。如果该标志存在,请导航到保存部分脚本的位置。运行脚本。删除标志cfn-userdata-script

答案 1 :(得分:0)

您也可以在 EC2 实例中使用计划任务来完成任务。例如。在 Windows 中,您可以将任务设置为在重新启动后运行一次。

相关问题