【问题标题】:AWS - cfn-init not creating a fileAWS - cfn-init 不创建文件
【发布时间】:2017-04-04 02:06:06
【问题描述】:

我是 cloudformation 的新手。 我正在使用 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"
        } ]
      }
    }
  }
} 

【问题讨论】:

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

标签: amazon-web-services amazon-cloudformation


【解决方案1】:

在您的comment 中发现,AWS::EC2::Instance 资源上的UserData 属性要求第一行是#!/bin/bash\n。

为了将cloud-init 处理的用户数据解释为User-Data Script,这是必要的,如AWS EC2 文档部分Running Commands on Your Linux Instance at Launch 中所述:

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

另请注意,sudo 在您的用户数据脚本中不是必需的,如文档中所述:

作为用户数据输入的脚本以root用户身份执行,因此请勿在脚本中使用sudo命令。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 2019-06-13
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 2016-02-14
    • 2018-03-04
    相关资源
    最近更新 更多