繁体   English   中英

AWS CloudFormation 用户数据 \; 和新行(JSON)

[英]AWS CloudFormation User Data \; and new lines (JSON)

我正在尝试创建一个自动部署网络服务器的 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 文件对其使用不满意。

模板固定 我删除了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"]
      }
    }
  }
}

我很早就学会了使用 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==--

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM