gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:36:26 26 4
gpt4 key购买 nike

我正在尝试创建一个自动部署网络服务器的 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"]
}
}
}
}

关于json - AWS CloudFormation 用户数据\;和新行 (JSON),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62013428/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com