CloudFormation没有执行我的命令吗?

时间:2018-07-05 06:06:17

标签: amazon-web-services amazon-cloudformation

我正在通过CloudFormation执行命令,但是它不起作用。另外,我在日志中没有任何错误。可以使用此模板创建实例和EBS,但命令未执行。

这是我正在使用的模板:

{
  "Description" : "AWS CloudFormation Sample Template 
   EC2InstanceWithSecurityGroupSampleAndEBS",
"Resources": {
    "Ec2Instance": {
        "Type": "AWS::EC2::Instance",
        "Properties": {
            "KeyName": "123",
            "ImageId": "280a1kk",
            "InstanceType": "t1.micro",
            "NetworkInterfaces" : [{
                  "GroupSet"                 : ["ds"],
                  "AssociatePublicIpAddress" : "true",
                  "DeviceIndex"              : "0",
                  "DeleteOnTermination"      : "true",
                  "SubnetId"                 : "7f"
                  }
                ],
            "BlockDeviceMappings": [
                 {
                  "DeviceName": "/dev/sdm",
                  "Ebs": {
                      "VolumeType": "io1",
                      "Iops": 300,
                      "DeleteOnTermination": true,
                      "VolumeSize": 100
                      }
                }
              ],
           },
        "Metadata": {
          "AWS::CloudFormation::Init": {
             "config": {
               "commands": {
                   "1_mkfs": {
                     "command" : "mkfs -t ext4 /dev/xvdm\n"
                   },
                   "2_mkdir": {
                      "command" : "mkdir /mntfirm\n"
                   },
                   "3_mount": {
                      "command" : "mount /dev/xvdm /mntfirm"
                   }
               }
            }
         }
      }
    }
   }
  }

请帮帮我。

1 个答案:

答案 0 :(得分:0)

您需要安装cfn bootstrap并执行命令,请尝试

   {
   "Description":"AWS CloudFormation Sample Template EC2InstanceWithSecurityGroupSampleAndEBS",
   "Resources":{
      "Ec2Instance":{
         "Metadata":{
            "AWS::CloudFormation::Init":{
               "configSets" : {
            "InstallAndRun" : [ "config" ]
          },
               "config":{
                  "commands":{
                     "1_mkfs":{
                        "command":"mkfs -t ext4 /dev/xvdm\n"
                     },
                     "2_mkdir":{
                        "command":"mkdir /mntfirm\n"
                     },
                     "3_mount":{
                        "command":"mount /dev/xvdm /mntfirm"
                     }
                  }
               }
            }
         },
         "Type":"AWS::EC2::Instance",
         "Properties":{

            "ImageId":"AMI",
            "InstanceType":"t1.micro",
            "KeyName": "KEY",
            "NetworkInterfaces":[
               {
                  "GroupSet":[
                     "sg-xxx"
                  ],
                  "AssociatePublicIpAddress":"true",
                  "DeviceIndex":"0",
                  "DeleteOnTermination":"true",
                  "SubnetId":"subnet-xxx"
               }
            ],
            "BlockDeviceMappings":[
               {
                  "DeviceName":"/dev/sdm",
                  "Ebs":{
                     "VolumeType":"io1",
                     "Iops":300,
                     "DeleteOnTermination":true,
                     "VolumeSize":100
                  }
               }
            ],
            "UserData":{
               "Fn::Base64":{
                  "Fn::Join":[
                     "",
                     [
                        "#!/bin/bash -xe\n",
                        "apt-get install -y python-setuptools\n",
                        "mkdir -p /opt/aws/bin\n",
                        "wget https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz\n",
                        "easy_install --script-dir /opt/aws/bin aws-cfn-bootstrap-latest.tar.gz\n",
                        "apt-get update\n",
                        "# Install the files and packages from the metadata\n",
                        "/opt/aws/bin/cfn-init -v ",
                        "         --stack ",
                        {
                           "Ref":"AWS::StackName"
                        },
                        "         --resource Ec2Instance ",
                        "         --config InstallAndRun ",
                        "         --region ",
                        {
                           "Ref":"AWS::Region"
                        },
                        "\n",
                        "# Signal the status from cfn-init\n",
                        "/opt/aws/bin/cfn-signal -e $? ",
                        "         --stack ",
                        {
                           "Ref":"AWS::StackName"
                        },
                        "         --resource Ec2Instance ",
                        "         --region ",
                        {
                           "Ref":"AWS::Region"
                        },
                        "\n"
                     ]
                  ]
               }
            }
         }
      }
   }
} 

所有事件将记录到系统日志