Amazon web services AWS-cfn init未创建文件

Amazon web services AWS-cfn init未创建文件,amazon-web-services,amazon-cloudformation,Amazon Web Services,Amazon Cloudformation,我是新来的。 我正在使用cfn init创建一个文件。但是没有创建文件,我的堆栈也不会失败。使用必需的资源(如EC2实例)成功创建堆栈。此外,它还安装了用户数据中提到的AWS CLI。 但它并没有创建我想要创建的文件。 我尝试使用不允许堆栈回滚的高级选项。但是没有创建/var/log/cfn-init.log。 请参阅下面的模板?我在这方面做错了什么吗 { "Parameters" : { "KeyName" : { "Description" : "The EC2 Ke

我是新来的。 我正在使用cfn init创建一个文件。但是没有创建文件,我的堆栈也不会失败。使用必需的资源(如EC2实例)成功创建堆栈。此外,它还安装了用户数据中提到的AWS CLI。 但它并没有创建我想要创建的文件。 我尝试使用不允许堆栈回滚的高级选项。但是没有创建/var/log/cfn-init.log。 请参阅下面的模板?我在这方面做错了什么吗

{
  "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"
        } ]
      }
    }
  }
} 
正如在中发现的,资源上的属性要求第一行为
#/bin/bash\n

如AWS EC2文档部分所述,这是必要的,以便将处理的用户数据解释为:

用户数据外壳脚本必须以
#开头字符和要读取脚本的解释器的路径(通常为
/bin/bash

还请注意,用户数据脚本中不需要
sudo
,文档中也提到了这一点:

作为用户数据输入的脚本作为
根用户执行,因此不要在脚本中使用
sudo
命令


最后,默认情况下,AWS CLI预装在Amazon Linux AMI实例上,这就是为什么您注意到,尽管您的用户数据脚本运行不正常,但AWS CLI仍安装在您的实例上。

您到底想在文件内容中插入什么?它是默认区域吗?你可能想检查一下阿加尼是否能够解决这个问题。在“UserData”中添加了第一行“#!/bin/bash\n”,要使任何命令正常工作,我们需要在UserData中提供一个shell环境,否则它无法创建任何文件。谢谢