在cloudformation中部署堆栈后如何安装软件

时间:2019-10-18 07:59:24

标签: amazon-web-services amazon-cloudformation

创建堆栈后如何安装软件或创建文件或将文件更新到实例。 AWS::CloudFormation::Init如果我们使用它,我们需要运行Userdata脚本,这将重新启动服务器(或终止第一个实例并创建一个新实例),但是我不想创建或删除第一个实例。

他们可以通过任何方式直接安装CloudFormation形式的软件。

请帮助

1 个答案:

答案 0 :(得分:0)

这里是创建Ceph实例的示例。 userdata脚本用于安装软件,您将看到一个创建文件/etc/logrotate.d/allvarlogs的示例。它只是在cloudformation中编码的bash脚本。

 "CephServerInstance1" : {
  "Type" : "AWS::EC2::Instance",
  "DependsOn" : "StaticIPAddress1",
  "Properties": {
    "BlockDeviceMappings": [
      {
         "DeviceName" : "/dev/sda1",
         "Ebs" : { "VolumeSize" : { "Ref": "EbsDeviceSize" } }
      },
      {
         "DeviceName" : "/dev/sda2",
         "Ebs" : {
           "VolumeSize" : { "Ref": "DataDeviceSize" },
           "DeleteOnTermination" : true,
           "Encrypted" : false
         }
      }
    ],
    "IamInstanceProfile" : { "Ref" : "InstanceProfile"},
    "Tags": [
      {
        "Key" : "Name",
        "Value" : "ceph-1"
      }
    ],
    "ImageId" : { "Ref" : "ImageId" },
    "InstanceType": { "Ref" : "InstanceType" },
    "KeyName"  : { "Ref": "KeyName" },
    "SourceDestCheck": false,
    "NetworkInterfaces": [
      {
        "NetworkInterfaceId": { "Ref": "StaticIPAddress1" },
        "DeviceIndex": 0
      }
    ],
    "CreditSpecification": {
      "CPUCredits": "standard"
    },
    "UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
      "#!/bin/bash\n",
      "yum -y update\n",
      "yum -y install epel-release\n",
      "yum install -y htop\n",
      "yum install -y vim\n",
      "curl -O https://bootstrap.pypa.io/get-pip.py\n",
      "python get-pip.py --user\n",
      "export PATH=~/.local/bin:$PATH\n",
      "pip install awscli --upgrade --user\n",
      "yum erase -y 'ntp*'\n",
      "yum -y install chrony\n",
      "echo 'server 169.254.169.123 prefer iburst' >> /etc/chrony.conf\n",
      "systemctl restart chronyd\n",
      "# Set up log rotation\n",
      "cat <<EOF >/etc/logrotate.d/allvarlogs\n",
      "/var/log/*.log {\n",
          "rotate ${LOGROTATE_FILES_MAX_COUNT:-5}\n",
          "copytruncate\n",
          "missingok\n",
          "notifempty\n",
          "compress\n",
          "maxsize ${LOGROTATE_MAX_SIZE:-100M}\n",
          "daily\n",
          "dateext\n",
          "dateformat -%Y%m%d-%s\n",
          "create 0644 root root\n",
      "}\n",
      "EOF\n",
      "echo done > /home/centos/done.txt\n"
    ]]}}
  }
},