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

问题描述 投票:0回答:1

我正试图创建一个 CloudForm EC2 部署,在该部署中会自动部署 Web 服务器。我当前的设置如下。

{
    "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文件对其使用不满意。

json amazon-web-services amazon-ec2 amazon-cloudformation
1个回答
1
投票

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

0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.