2016-11-20 46 views
1

我是雲技術新手。 我正在使用cfn-init創建一個文件。但不創建文件,也不會失敗。堆棧成功使用EC2實例等所需資源創建。此外,它還會按用戶數據中所述安裝AWS CLI。 但它只是不創建我想創建的文件。 我嘗試使用不允許回滾堆棧的高級選項。但/var/log/cfn-init.log不會被創建。 請參閱下面的模板?我在這做什麼不對嗎?AWS - cfn-init不創建文件

{ 
    "Parameters" : { 
    "KeyName" : { 
     "Description" : "The EC2 Key Pair to allow SSH access to the instance", 
     "Type" : "AWS::EC2::KeyPair::KeyName" 
    } 
    }, 
    "Resources" : { 
    "Ec2Instance" : { 
     "Type" : "AWS::EC2::Instance", 
     "Metadata" : { 
     "Comment" : "Install a simple application", 
     "AWS::CloudFormation::Init" : { 
      "config" : { 
      "files" : { 
       "/tmp/setup.mysql" : { 
       "content" : { "Fn::Join" : ["", ["[default]\n","region=",{"Ref": "AWS::Region"}]]}, 
       "mode" : "000775", 
       "owner" : "ec2-user", 
       "group" : "ec2-user" 
       }  
      } 
      } 
      } }, 

     "Properties" : { 
     "SecurityGroups" : [ { 
       "Ref" : "InstanceSecurityGroup" } 
       ], 
     "IamInstanceProfile" : {"Ref" : "RootInstanceProfile"} , 
     "KeyName" : { "Ref" : "KeyName"}, 
     "InstanceType" : "t2.micro", 
     "ImageId" : "ami-58277d3d", 
     "UserData": { 
        "Fn::Base64": { 
         "Fn::Join": [ 
          "", 
          [ 
           "curl https://s3.amazonaws.com/aws-cli/awscli-bundle.zip -o awscli-bundle.zip\n", 
           "unzip awscli-bundle.zip\n", 
           "sudo ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws\n", 
           "/opt/aws/bin/cfn-init -v ", 
           "   --stack ", { "Ref" : "AWS::StackName" }, 
           "   --resource Ec2Instance ", 
           "   --region ", { "Ref" : "AWS::Region" }, "\n", 
           "cfn-signal -e 0", 
           " --stack ", 
           { 
            "Ref": "AWS::StackName" 
           }, 
           " --region ", 
           { 
            "Ref": "AWS::Region" 
           }, 
           " --resource ", 
           "Ec2Instance", 
           "\n" 
          ] 
         ] 
        } 
       } 
     } 
    }, 


     "RootRole": { 
     "Type": "AWS::IAM::Role", 
     "Properties": { 
      "AssumeRolePolicyDocument": { 
       "Version" : "2012-10-17", 
       "Statement": [ { 
        "Effect": "Allow", 
        "Principal": { 
        "Service": [ "ec2.amazonaws.com" ] 
        }, 
        "Action": [ "sts:AssumeRole" ] 
       } ] 
      }, 
      "Path": "/", 
      "Policies": [ { 
       "PolicyName": "root", 
       "PolicyDocument": { 
        "Version" : "2012-10-17", 
        "Statement": [ { 
        "Effect": "Allow", 
        "Action": ["cloudwatch:PutMetricData"], 
        "Resource": "*" 
        } ] 
       } 
       } ] 
      } 
     }, 
     "RootInstanceProfile": { 
     "Type": "AWS::IAM::InstanceProfile", 
     "Properties": { 
      "Path": "/", 
      "Roles": [ { 
       "Ref": "RootRole" 
      } ] 
     } 
     }, 



    "InstanceSecurityGroup" : { 
     "Type" : "AWS::EC2::SecurityGroup", 
     "Properties" : { 
     "GroupDescription" : "Enable SSH access via port 22", 
     "Tags" : [{ "Key" : "Name", "Value" : "SecurityGr_EC2WithParam" }], 
     "SecurityGroupIngress" : [ { 
      "IpProtocol" : "tcp", 
      "FromPort" : "22", 
      "ToPort" : "22", 
      "CidrIp" : "0.0.0.0/0" 
     } ] 
     } 
    } 
    } 
} 
+0

你想在文件內容中完全插入什麼?它是默認區域嗎?你可能想再次檢查那個塊 – Ali

+0

我能解決這個阿里。將「UserData」中的第一行添加爲「#!/ bin/bash \ n」,對於任何需要工作的命令,我們需要在Userdata中提供一個shell環境,否則無法創建任何文件。謝謝 – Wills

回答

0

在你的comment發現,在你的AWS::EC2::Instance資源的UserData屬性要求的第一行是#!/bin/bash\n

這是必要的,以便用於通過cloud-init處理以被解釋爲User-Data Script用戶數據,如在AWS EC2文檔部分所指出的,Running Commands on Your Linux Instance at Launch

用戶數據外殼腳本必須從#!開始字符和要讀取腳本的解釋器的路徑(通常爲/bin/bash)。也

注意sudo是不是在你的用戶數據腳本必要的,因爲文件中還指出:

腳本輸入作爲爲root用戶執行的用戶數據,所以不使用sudo命令在腳本中。

最後,note的AWS CLI默認情況下,這就是爲什麼你注意到AWS CLI仍然對您的實例安裝,儘管您的用戶數據腳本沒有正確地運行來自亞馬遜的Linux AMI實例預裝。