【问题标题】:AWS CloudFormation User Data \; and new lines (JSON)AWS CloudFormation 用户数据 \;和新行(JSON)
【发布时间】:2020-09-12 17:48:11
【问题描述】:

我正在尝试创建一个自动部署网络服务器的 CloudFormation EC2 部署。 我目前的设置如下:

{
    "AWSTemplateFormatVersion" : "2010-09-09",

    "Description" : "AWS CloudFormation Sample Template EC2InstanceWithSecurityGroupSample: Create an Amazon EC2 instance running the Amazon Linux AMI. The AMI is chosen based on the region in which the stack is run. This example creates an EC2 security group for the instance to give you SSH access. **WARNING** This template creates an Amazon EC2 instance. You will be billed for the AWS resources used if you create a stack from this template.",

    "Parameters" : {
      "KeyName": {
        "Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instance",
        "Type": "AWS::EC2::KeyPair::KeyName",
        "ConstraintDescription" : "must be the name of an existing EC2 KeyPair."
      },

      "InstanceType" : {
        "Description" : "WebServer EC2 instance type",
        "Type" : "String",
        "Default" : "t2.micro",
        "AllowedValues" : [ "t1.micro", "t2.nano", "t2.micro"]
  ,
        "ConstraintDescription" : "must be a valid EC2 instance type."
      },

      "SSHLocation" : {
        "Description" : "The IP address range that can be used to SSH to the EC2 instances",
        "Type": "String",
        "MinLength": "9",
        "MaxLength": "18",
        "Default": "0.0.0.0/0",
        "AllowedPattern": "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})",
        "ConstraintDescription": "must be a valid IP CIDR range of the form x.x.x.x/x."
     }
    },

    "Mappings" : {
      "AWSInstanceType2Arch" : {
        "t2.micro"    : { "Arch" : "HVM64"  }
      },

      "AWSInstanceType2NATArch" : {
        "t1.micro"    : { "Arch" : "NATHVM64"  },
        "t2.nano"     : { "Arch" : "NATHVM64"  },
        "t2.micro"    : { "Arch" : "NATHVM64"  }
      }
  ,
      "AWSRegionArch2AMI" : {
        "us-east-1"        : {"HVM64" : "ami-0080e4c5bc078760e", "HVMG2" : "ami-0aeb704d503081ea6"},
        "us-west-2"        : {"HVM64" : "ami-01e24be29428c15b2", "HVMG2" : "ami-0fe84a5b4563d8f27"},
        "us-west-1"        : {"HVM64" : "ami-0ec6517f6edbf8044", "HVMG2" : "ami-0a7fc72dc0e51aa77"},
        "eu-west-1"        : {"HVM64" : "ami-08935252a36e25f85", "HVMG2" : "ami-0d5299b1c6112c3c7"},
        "eu-west-2"        : {"HVM64" : "ami-01419b804382064e4", "HVMG2" : "NOT_SUPPORTED"},
        "eu-west-3"        : {"HVM64" : "ami-0dd7e7ed60da8fb83", "HVMG2" : "NOT_SUPPORTED"},
        "eu-central-1"     : {"HVM64" : "ami-0cfbf4f6db41068ac", "HVMG2" : "ami-0aa1822e3eb913a11"},
        "eu-north-1"       : {"HVM64" : "ami-86fe70f8", "HVMG2" : "ami-32d55b4c"}
      }

    },

    "Resources" : {
      "EC2Instance" : {
        "Type" : "AWS::EC2::Instance",
        "Metadata" : {
            "AWS::CloudFormation::Init" : {
                "configSets" : {
                    "Install" : [ "Install" ]
                },

                "Install" : {
                    "packages" : {         
                        "yum": {"httpd":[], "php":[], "mysql-server":[], "php-mysql":[]}      
                    },
                    "sources" : {              
                    },
                    "files" : {               
                    },
                    "commands" : {               
                    },
                    "services" : {  
                        "sysvinit" : {"mysqld"  : 
                            { "enabled" : "true", "ensureRunning" : "true" },
                        "httpd"   : { "enabled" : "true", "ensureRunning" : "true" }}             
                    }
              }
            }
          },
        "Properties" : {
          "Tags" : [{"Key" : "StudentID", "Value" : "something"},
          {"Key" : "StudentName", "Value" : "someone"}],    
          "InstanceType" : { "Ref" : "InstanceType" },
          "SecurityGroups" : [ { "Ref" : "WebServerSG" } ],
          "KeyName" : { "Ref" : "KeyName" },
          "ImageId" : "ami-01d025118d8e760db",
          "UserData": {"Fn::Base64":{"Fn::Join":["", [
          "#!/bin/bash",
          "yum update -y",
          "yum install -y httpd24 php70 mysql56-server php70-mysqlnd",
          "service httpd start",
          "chkconfig httpd on",
          "usermod -a -G apache ec2-user",
          "chown -R ec2-user:apache /var/www",
          "chmod 2775 /var/www",
          "find /var/www -type d -exec sudo chmod 2775 {} +",
          "find /var/www -type f -exec sudo chmod 0664 {} +",
          "echo '<?php echo '<h2>Welcome to COS80001. Installed PHP version: ' . phpversion() . '</h2>'; ?>' > /var/www/html/phpinfo.php"
          ]]}}

        }
      },

      "WebServerSG" : {
        "Type" : "AWS::EC2::SecurityGroup",
        "Properties" : { 
          "GroupDescription" : "Web DMZ",
          "SecurityGroupIngress" : [ 
              {
            "IpProtocol" : "tcp",
            "FromPort" : "22",
            "ToPort" : "22",
            "CidrIp" : { "Ref" : "SSHLocation"}
            },
            {
                "IpProtocol" : "tcp",
                "FromPort" : "80",
                "ToPort" : "80",
                "CidrIp" : { "Ref" : "SSHLocation"}
            },
            {
                "IpProtocol" : "tcp",
                "FromPort" : "443",
                "ToPort" : "443",
                "CidrIp" : { "Ref" : "SSHLocation"}
            }
            ],

          "Tags" : [{"Key" : "StudentID", "Value" : "something"},
            {"Key" : "StudentName", "Value" : "someone"}
        ]  
        }
      }
    },

    "Outputs" : {
      "InstanceId" : {
        "Description" : "InstanceId of the newly created EC2 instance",
        "Value" : { "Ref" : "EC2Instance" }
      },
      "AZ" : {
        "Description" : "Availability Zone of the newly created EC2 instance",
        "Value" : { "Fn::GetAtt" : [ "EC2Instance", "AvailabilityZone" ] }
      },
      "PublicDNS" : {
        "Description" : "Public DNSName of the newly created EC2 instance",
        "Value" : { "Fn::GetAtt" : [ "EC2Instance", "PublicDnsName" ] }
      },
      "PublicIP" : {
        "Description" : "Public IP address of the newly created EC2 instance",
        "Value" : { "Fn::GetAtt" : [ "EC2Instance", "PublicIp" ] }
      }
    }
  }

我的最终目标是实现以下 bash 脚本


#!/bin/bash
yum update -y
yum install -y httpd24 php70 mysql56-server php70-mysqlnd
service httpd start
chkconfig httpd on
usermod -a -G apache ec2-user
chown -R ec2-user:apache /var/www
chmod 2775 /var/www
find /var/www -type d -exec sudo chmod 2775 {} \;
find /var/www -type f -exec sudo chmod 0664 {} \;
echo "<?php echo '<h2>Welcome to COS80001. Installed PHP version: ' .
phpversion() . '</h2>'; ?>" > /var/www/html/phpinfo.php

我在连接默认网页时遇到了一些问题。我对如何实现嵌入 3 个撇号感到困惑。 我也对 find \; 感到困惑因为 JSON 文件对其使用不满意。

【问题讨论】:

  • 能否提供所有实例模板?据我所知,无论您的引号如何,您的用户数据都是不正确的。
  • 您也可以发布您收到的任何错误消息吗?
  • 我添加了整个模板。如何查看我的错误消息?我通过添加模板。 CloudFormation 并且在导入时没有收到任何错误。我应该使用 CLI 吗?
  • 谢谢。我会看看的。你可以开始思考,AWS::CloudFormation::Init" 永远不会被执行。您必须在用户数据中明确使用cfn-init 才能执行AWS::CloudFormation::Init"。调试用户数据和初始化部分很痛苦。只需运行它,转到实例,然后查找实例本身的错误。修复模板,重新部署堆栈,转到实例,查找错误,然后重复。
  • 我对自己做错了什么感到困惑。所以 AWS::CloudFormation::Init 是 cfn-init 我应该只使用它,所以我应该删除用户数据?据我所知,init 在更新时更新 EC2 实例,而用户数据将完成擦除实例

标签: json amazon-web-services amazon-ec2 amazon-cloudformation


【解决方案1】:

我很早就学会了使用 CloudFormation 模板:

我的典型UserData

      UserData:
        - !Base64 
          'Fn::Sub': |
            Content-Type: multipart/mixed; boundary="==BOUNDARY=="
            MIME-Version: 1.0

            --==BOUNDARY==
            Content-Type: text/x-shellscript; charset="us-ascii"

            #!/bin/bash
            # Set any ECS agent configuration options
            cat <<'EOF' >> /etc/ecs/ecs.config
            ECS_CLUSTER=${AnonymisierungCluster}
            ECS_ENGINE_TASK_CLEANUP_WAIT_DURATION=15m
            EOF

            --==BOUNDARY==--

【讨论】:

    【解决方案2】:

    模板固定。我删除了AWS::CloudFormation::Init。在您的用例中,我没有看到这一点。我修改了它的键、ami 和参数部分,所以你必须将它们调整回原来的样子。 UserData 有效phpinfo.php 已正确部署。

    {
      "AWSTemplateFormatVersion": "2010-09-09",
    
      "Description": "AWS CloudFormation Sample Template EC2InstanceWithSecurityGroupSample: Create an Amazon EC2 instance running the Amazon Linux AMI. The AMI is chosen based on the region in which the stack is run. This example creates an EC2 security group for the instance to give you SSH access. **WARNING** This template creates an Amazon EC2 instance. You will be billed for the AWS resources used if you create a stack from this template.",
    
      "Parameters": {
    
        "InstanceType": {
          "Description": "WebServer EC2 instance type",
          "Type": "String",
          "Default": "t2.micro",
          "AllowedValues": ["t1.micro", "t2.nano", "t2.micro"],
          "ConstraintDescription": "must be a valid EC2 instance type."
        },
    
        "SSHLocation": {
          "Description": "The IP address range that can be used to SSH to the EC2 instances",
          "Type": "String",
          "MinLength": "9",
          "MaxLength": "18",
          "Default": "0.0.0.0/0",
          "AllowedPattern": "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})",
          "ConstraintDescription": "must be a valid IP CIDR range of the form x.x.x.x/x."
        }
      },
    
      "Mappings": {
        "AWSInstanceType2Arch": {
          "t2.micro": {
            "Arch": "HVM64"
          }
        },
    
        "AWSInstanceType2NATArch": {
          "t1.micro": {
            "Arch": "NATHVM64"
          },
          "t2.nano": {
            "Arch": "NATHVM64"
          },
          "t2.micro": {
            "Arch": "NATHVM64"
          }
        },
        "AWSRegionArch2AMI": {
          "us-east-1": {
            "HVM64": "ami-0080e4c5bc078760e",
            "HVMG2": "ami-0aeb704d503081ea6"
          },
          "us-west-2": {
            "HVM64": "ami-01e24be29428c15b2",
            "HVMG2": "ami-0fe84a5b4563d8f27"
          },
          "us-west-1": {
            "HVM64": "ami-0ec6517f6edbf8044",
            "HVMG2": "ami-0a7fc72dc0e51aa77"
          },
          "eu-west-1": {
            "HVM64": "ami-08935252a36e25f85",
            "HVMG2": "ami-0d5299b1c6112c3c7"
          },
          "eu-west-2": {
            "HVM64": "ami-01419b804382064e4",
            "HVMG2": "NOT_SUPPORTED"
          },
          "eu-west-3": {
            "HVM64": "ami-0dd7e7ed60da8fb83",
            "HVMG2": "NOT_SUPPORTED"
          },
          "eu-central-1": {
            "HVM64": "ami-0cfbf4f6db41068ac",
            "HVMG2": "ami-0aa1822e3eb913a11"
          },
          "eu-north-1": {
            "HVM64": "ami-86fe70f8",
            "HVMG2": "ami-32d55b4c"
          }
        }
    
      },
    
      "Resources": {
        "EC2Instance": {
          "Type": "AWS::EC2::Instance",
          "Properties": {
            "Tags": [{
                "Key": "StudentID",
                "Value": "something"
              },
              {
                "Key": "StudentName",
                "Value": "someone"
              }
            ],
            "InstanceType": "t2.micro",
            "SecurityGroups": [{
              "Ref": "WebServerSG"
            }],
            "KeyName": "MyKeyPair",
            "ImageId": "ami-0323c3dd2da7fb37d",
            "UserData": {
              "Fn::Base64": {
                "Fn::Join": ["", [
                  "#!/bin/bash -ex\n",
                  "yum update -y\n",
                  "yum install -y httpd php mysql-server php-mysqlnd\n",
                  "systemctl enable httpd\n",
                  "systemctl start httpd\n",
                  "usermod -a -G apache ec2-user\n",
                  "chown -R ec2-user:apache /var/www\n",
                  "chmod 2775 /var/www\n",
                  "find /var/www -type d -exec sudo chmod 2775 {} \\;\n",
                  "find /var/www -type f -exec sudo chmod 0664 {} \\;\n",
                  "echo \"<?php echo '<h2>Welcome to COS80001. Installed PHP version: ' . phpversion() . '</h2>'; ?>\" > /var/www/html/phpinfo.php\n"
                ]]
              }
            }
    
          }
        },
    
        "WebServerSG": {
          "Type": "AWS::EC2::SecurityGroup",
          "Properties": {
            "GroupDescription": "Web DMZ",
            "SecurityGroupIngress": [{
                "IpProtocol": "tcp",
                "FromPort": "22",
                "ToPort": "22",
                "CidrIp": {
                  "Ref": "SSHLocation"
                }
              },
              {
                "IpProtocol": "tcp",
                "FromPort": "80",
                "ToPort": "80",
                "CidrIp": {
                  "Ref": "SSHLocation"
                }
              },
              {
                "IpProtocol": "tcp",
                "FromPort": "443",
                "ToPort": "443",
                "CidrIp": {
                  "Ref": "SSHLocation"
                }
              }
            ],
    
            "Tags": [{
                "Key": "StudentID",
                "Value": "something"
              },
              {
                "Key": "StudentName",
                "Value": "someone"
              }
            ]
          }
        }
      },
    
      "Outputs": {
        "InstanceId": {
          "Description": "InstanceId of the newly created EC2 instance",
          "Value": {
            "Ref": "EC2Instance"
          }
        },
        "AZ": {
          "Description": "Availability Zone of the newly created EC2 instance",
          "Value": {
            "Fn::GetAtt": ["EC2Instance", "AvailabilityZone"]
          }
        },
        "PublicDNS": {
          "Description": "Public DNSName of the newly created EC2 instance",
          "Value": {
            "Fn::GetAtt": ["EC2Instance", "PublicDnsName"]
          }
        },
        "PublicIP": {
          "Description": "Public IP address of the newly created EC2 instance",
          "Value": {
            "Fn::GetAtt": ["EC2Instance", "PublicIp"]
          }
        }
      }
    }
    

    【讨论】:

    • 如果我是对的。问题是同时拥有 init 和 userdata?因为他们本质上做同样的事情?
    • @PandaPlaysAll 真的没有。您的用户数据存在许多问题。我把它们都修好了。在您的场景中不需要Init。您可以将我的用户数据与您的进行比较。该模板现在可以使用。我在 Amazon Linux 2 上对其进行了测试。
    • 除了粘贴默认网页外,我一切正常。收到错误?你的财产有效吗? \"\" 有问题
    • @PandaPlaysAll 粘贴什么?我通过/phpinfo.php 进行了测试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-10
    • 1970-01-01
    • 2021-05-25
    • 2019-07-06
    • 1970-01-01
    • 2019-06-24
    相关资源
    最近更新 更多