gpt4 book ai didi

json - CloudFormation 脚本 - 从 AMI 创建实例

转载 作者:行者123 更新时间:2023-12-03 07:37:41 25 4
gpt4 key购买 nike

我很好奇是否有人有 CloudFormation 的经验。我正在尝试创建一个脚本,该脚本采用加密的私有(private) AMI 并从中创建一个实例。该脚本必须分配安全组,将脚本保存到 s3 中的特定位置并添加 IAM 角色。

我一直在使用 LAMP_Single_Instance 作为基础,但我无法理解它。我很好奇是否有人可以提供帮助。

在脚本中分配安全组的位置和方式,以及如何在 s3 中设置模板的默认保存位置。

{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "AWS CloudFormation AITS Script, thats creates ec2 instances",
"Parameters": {
"KeyName": {
"Description": "Name of an existing EC2 KeyPair to enable SSH access to the instance",
"Type": "AWS::EC2::KeyPair::KeyName",
"ConstraintDescription": "EC2 KeyPair."
},
"DBName": {
"Default": "MyDatabase",
"Description": "MySQL database name",
"Type": "String",
"MinLength": "1",
"MaxLength": "64",
"AllowedPattern": "[a-zA-Z][a-zA-Z0-9]*",
"ConstraintDescription": "must begin with a letter and contain only alphanumeric characters."
},
"DBUser": {
"NoEcho": "true",
"Description": "Username for MySQL database access",
"Type": "String",
"MinLength": "1",
"MaxLength": "16",
"AllowedPattern": "[a-zA-Z][a-zA-Z0-9]*",
"ConstraintDescription": "must begin with a letter and contain only alphanumeric characters."
},
"DBPassword": {
"NoEcho": "true",
"Description": "Password for MySQL database access",
"Type": "String",
"MinLength": "1",
"MaxLength": "41",
"AllowedPattern": "[a-zA-Z0-9]*",
"ConstraintDescription": "must contain only alphanumeric characters."
},
"DBRootPassword": {
"NoEcho": "true",
"Description": "Root password for MySQL",
"Type": "String",
"MinLength": "1",
"MaxLength": "41",
"AllowedPattern": "[a-zA-Z0-9]*",
"ConstraintDescription": "must contain only alphanumeric characters."
},
"InstanceType": {
"Description": "WebServer EC2 instance type",
"Type": "String",
"Default": "t2.large",
"AllowedValues": [
"t2.large"
],
"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.large": {
"Arch": "HVM64"
}
},
"AWSInstanceType2NATArch": {
"t2.large": {
"Arch": "NATHVM64"
}
},
"AWSRegionArch2AMI": {
"eu-west-1a": {
"PV64": "NOT_SUPPORTED",
"HVM64": "ami-0aae836c",
"HVMG2": "NOT_SUPPORTED"
}
}
},
"Resources": {
"WebServerInstance": {
"Type": "AWS::EC2::Instance",
"Metadata": {
"AWS::CloudFormation::Init": {
"configSets": {
"InstallAndRun": [
"Install",
"Configure"
]
},
"Install": {
"packages": {
"yum": {
"mysql": [],
"mysql-server": [],
"mysql-libs": [],
"httpd": [],
"php": [],
"php-mysql": []
}
},
"files": {
"/var/www/html/index.php": {
"content": {
"Fn::Join": [
"",
[
"<html>\n",
" <head>\n",
" <title>AITS CloudFormation</title>\n",
" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">\n",
" </head>\n",
" <body>\n",
" <h1>Welcome to the AITS Cloud Formation</h1>\n",
" <p/>\n",
" <?php\n",
" // Print out the current data and time\n",
" print \"The Current Date and Time is: <br/>\";\n",
" print date(\"g:i A l, F j Y.\");\n",
" ?>\n",
" <p/>\n",
" <?php\n",
" // Setup a handle for CURL\n",
" $curl_handle=curl_init();\n",
" curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);\n",
" curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);\n",
" // Get the hostname of the intance from the instance metadata\n",
" curl_setopt($curl_handle,CURLOPT_URL,'http://169.254.169.254/latest/meta-data/public-hostname');\n",
" $hostname = curl_exec($curl_handle);\n",
" if (empty($hostname))\n",
" {\n",
" print \"Sorry, for some reason, we got no hostname back <br />\";\n",
" }\n",
" else\n",
" {\n",
" print \"Server = \" . $hostname . \"<br />\";\n",
" }\n",
" // Get the instance-id of the intance from the instance metadata\n",
" curl_setopt($curl_handle,CURLOPT_URL,'http://169.254.169.254/latest/meta-data/instance-id');\n",
" $instanceid = curl_exec($curl_handle);\n",
" if (empty($instanceid))\n",
" {\n",
" print \"Sorry, for some reason, we got no instance id back <br />\";\n",
" }\n",
" else\n",
" {\n",
" print \"EC2 instance-id = \" . $instanceid . \"<br />\";\n",
" }\n",
" $Database = \"localhost\";\n",
" $DBUser = \"",
{
"Ref": "DBUser"
},
"\";\n",
" $DBPassword = \"",
{
"Ref": "DBPassword"
},
"\";\n",
" print \"Database = \" . $Database . \"<br />\";\n",
" $dbconnection = mysql_connect($Database, $DBUser, $DBPassword)\n",
" or die(\"Could not connect: \" . mysql_error());\n",
" print (\"Connected to $Database successfully\");\n",
" mysql_close($dbconnection);\n",
" ?>\n",
" <h2>PHP Information</h2>\n",
" <p/>\n",
" <?php\n",
" phpinfo();\n",
" ?>\n",
" </body>\n",
"</html>\n"
]
]
},
"mode": "000600",
"owner": "apache",
"group": "apache"
},
"/tmp/setup.mysql": {
"content": {
"Fn::Join": [
"",
[
"CREATE DATABASE ",
{
"Ref": "DBName"
},
";\n",
"GRANT ALL ON ",
{
"Ref": "DBName"
},
".* TO '",
{
"Ref": "DBUser"
},
"'@localhost IDENTIFIED BY '",
{
"Ref": "DBPassword"
},
"';\n"
]
]
},
"mode": "000400",
"owner": "root",
"group": "root"
},
"/etc/cfn/cfn-hup.conf": {
"content": {
"Fn::Join": [
"",
[
"[main]\n",
"stack=",
{
"Ref": "AWS::StackId"
},
"\n",
"region=",
{
"Ref": "AWS::Region"
},
"\n"
]
]
},
"mode": "000400",
"owner": "root",
"group": "root"
},
"/etc/cfn/hooks.d/cfn-auto-reloader.conf": {
"content": {
"Fn::Join": [
"",
[
"[cfn-auto-reloader-hook]\n",
"triggers=post.update\n",
"path=Resources.WebServerInstance.Metadata.AWS::CloudFormation::Init\n",
"action=/opt/aws/bin/cfn-init -v ",
" --stack ",
{
"Ref": "AWS::StackName"
},
" --resource WebServerInstance ",
" --configsets InstallAndRun ",
" --region ",
{
"Ref": "AWS::Region"
},
"\n",
"runas=root\n"
]
]
}
}
},
"services": {
"sysvinit": {
"mysqld": {
"enabled": "true",
"ensureRunning": "true"
},
"httpd": {
"enabled": "true",
"ensureRunning": "true"
},
"cfn-hup": {
"enabled": "true",
"ensureRunning": "true",
"files": [
"/etc/cfn/cfn-hup.conf",
"/etc/cfn/hooks.d/cfn-auto-reloader.conf"
]
}
}
}
},
"Configure": {
"commands": {
"01_set_mysql_root_password": {
"command": {
"Fn::Join": [
"",
[
"mysqladmin -u root password '",
{
"Ref": "DBRootPassword"
},
"'"
]
]
},
"test": {
"Fn::Join": [
"",
[
"$(mysql ",
{
"Ref": "DBName"
},
" -u root --password='",
{
"Ref": "DBRootPassword"
},
"' >/dev/null 2>&1 </dev/null); (( $? != 0 ))"
]
]
}
},
"02_create_database": {
"command": {
"Fn::Join": [
"",
[
"mysql -u root --password='",
{
"Ref": "DBRootPassword"
},
"' < /tmp/setup.mysql"
]
]
},
"test": {
"Fn::Join": [
"",
[
"$(mysql ",
{
"Ref": "DBName"
},
" -u root --password='",
{
"Ref": "DBRootPassword"
},
"' >/dev/null 2>&1 </dev/null); (( $? != 0 ))"
]
]
}
}
}
}
},
"AWS::CloudFormation::Designer": {
"id": "882df2be-e2b7-4778-8f2f-f7fad73e484a"
}
},
"Properties": {
"ImageId": {
"Fn::FindInMap": [
"AWSRegionArch2AMI",
{
"Ref": "AWS::Region"
},
{
"Fn::FindInMap": [
"AWSInstanceType2Arch",
{
"Ref": "InstanceType"
},
"Arch"
]
}
]
},
"InstanceType": {
"Ref": "InstanceType"
},
"SecurityGroups": [
{
"Ref": "WebServerSecurityGroup"
}
],
"KeyName": {
"Ref": "KeyName"
},
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"#!/bin/bash -xe\n",
"yum update -y aws-cfn-bootstrap\n",
"# Install the files and packages from the metadata\n",
"/opt/aws/bin/cfn-init -v ",
" --stack ",
{
"Ref": "AWS::StackName"
},
" --resource WebServerInstance ",
" --configsets InstallAndRun ",
" --region ",
{
"Ref": "AWS::Region"
},
"\n",
"# Signal the status from cfn-init\n",
"/opt/aws/bin/cfn-signal -e $? ",
" --stack ",
{
"Ref": "AWS::StackName"
},
" --resource WebServerInstance ",
" --region ",
{
"Ref": "AWS::Region"
},
"\n"
]
]
}
}
},
"CreationPolicy": {
"ResourceSignal": {
"Timeout": "PT5M"
}
}
},
"WebServerSecurityGroup": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"GroupDescription": "Enable HTTP access via port 80",
"SecurityGroupIngress": [
{
"IpProtocol": "tcp",
"FromPort": "80",
"ToPort": "80",
"CidrIp": "0.0.0.0/0"
},
{
"IpProtocol": "tcp",
"FromPort": "22",
"ToPort": "22",
"CidrIp": {
"Ref": "SSHLocation"
}
}
]
},
"Metadata": {
"AWS::CloudFormation::Designer": {
"id": "88a4f6dd-8489-4f6e-a3a7-ce92b350e672"
}
}
}
},
"Outputs": {
"WebsiteURL": {
"Description": "URL for newly created LAMP stack",
"Value": {
"Fn::Join": [
"",
[
"http://",
{
"Fn::GetAtt": [
"WebServerInstance",
"PublicDnsName"
]
}
]
]
}
}
},
"Metadata": {
"AWS::CloudFormation::Designer": {
"88a4f6dd-8489-4f6e-a3a7-ce92b350e672": {
"size": {
"width": 60,
"height": 60
},
"position": {
"x": 60,
"y": 90
},
"z": 1,
"embeds": []
},
"882df2be-e2b7-4778-8f2f-f7fad73e484a": {
"size": {
"width": 60,
"height": 60
},
"position": {
"x": 180,
"y": 90
},
"z": 1,
"embeds": [],
"ismemberof": [
"88a4f6dd-8489-4f6e-a3a7-ce92b350e672"
]
}
}
}
}

最佳答案

从您的问题来看,我相信您只是希望创建一个基于 AMI 的实例,并确保该实例附加了一个您也定义的安全组。

您需要创建一个包含以下组件的新模板

AWS::EC2::实例

AWS::EC2::安全组

AWS::IAM::角色

AWS::IAM::InstanceProfile

通过使用Ref命令将它们链接起来,您将能够实现您所需要的。

希望对您有所帮助。

祝你好运!

关于json - CloudFormation 脚本 - 从 AMI 创建实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42577140/

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