簡體   English   中英

用於在 ec2 實例上運行 bash 腳本的 AWS cloudformation json 腳本

[英]AWS cloudformation json script to run bash script on ec2 instance

我創建了一個 cloudformation 腳本,該腳本創建了一個 ec2 實例,以及一些基於其他功能的卷。 初始化 ec2 實例后,我希望它自動運行 bash 腳本以將腳本中創建的卷(不是根卷)安裝到 /var/www/vhosts。 但目前它創建了目錄,所以我知道它正在運行 bash 腳本但沒有成功執行其他命令。 這是我的 cloudformation 腳本。 當我分別運行下面列出的 3 個命令時,當 ssh 到 ec2 實例時,它們工作正常,並且卷已安裝到正確的位置。

    "Ec2Instance": {
  "Type": "AWS::EC2::Instance",
  "Properties": {
    "ImageId": {
      "Ref": "ImageIdentification"
    },
    "AvailabilityZone" : "eu-west-1a",
    "InstanceType": {
      "Ref": "InstanceType"
    },
    "SecurityGroupIds" : [
       {
         "Ref": "SecurityGroup"
       }
    ],
    "DisableApiTermination" : "true",
    "KeyName": {
      "Ref": "keyPairName"
    },
    "Tags" : [
      {"Key" : "Name", "Value" : { "Ref" : "EC2InstanceName"}}
    ],
    "UserData" : {"Fn::Base64" : {"Fn::Join" : ["", [
     "#!/bin/bash -v\n",
      "sudo mkdir -p /var/www/vhosts\n",
      "sudo mkfs -t ext4 /dev/xvdh\n",
      "sudo mount /dev/xvdh /var/www/vhosts\n"
    ]]}}
  }
},

謝謝你的幫助。

在 CloudFormation 中運行 bash 腳本需要在用戶數據部分指定遠程鏈接或在用戶數據中一一記下命令。

Tags:
    - Key: Name
      Value: test
  UserData:
    Fn::Base64: !Sub |
      #!/bin/bash -xe
      cd /var/tmp
      wget https://raw.githubusercontent.com/installer/cloudformation/install.sh
      sudo bash /var/tmp/install.sh 4  '${CORE.PrivateIp}'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM