一说就懂的AWS Cloudformation

来源:互联网 发布:论述java异常处理机制 编辑:程序博客网 时间:2024/05/21 16:18

我是一个系统工程师,经历过HP、IBM等等的工作岁月后投身到云计算的世界里,真感慨以前的时间都活在狗身上了,全都因为云计算里Infrastructure as Code(基础设施即代码)慨念。以前在机房里建系统的活现在可以用一段代码完成,包括接网线,增加储存硬盘,安装基本的包,以至应用层面的资料库,要是配合一些DevOps工具如Chef或Puppet,以前得花三四天来建新架构或管理现有架构都都可以在几分钟之内完成。今天就给大家介绍一下颠覆IT世界的亚马逊AWS Cloudformation。


Cloudformation到底是干嘛用的?

用人类的语言来表达,就是你能把你所需要架构内容、参数、详细定义写成代码然后让Cloudformation帮你建。


Cloudformation好在哪里?

- Cloudformation可建的资源种类几乎包括了所有的AWS云服务。

- 可把需要重覆置备的架构以代码形式表达,而代码的改动能透过版本管理的工具如git或svn进行维护,
例如由于大部份的网页后台或APP后台的架构都离不开公网与私网分离,并把资料库置放于私网内把外来连接隔离,只需要在建第一套架构时用Cloudformation的格式创建出来,后面的新应用建立的工序就都变简单了。


Cloudformation用在哪里?

如果你是一个开发者,你可以用Cloudformation把AWS整个系统里的资源当作自己的应用程序,随时随量添加或移除AWS实例或资源。Cloudformation服务可以让你透过网页介面,AWS命令行或任何AWS SDK如PHP、JAVA或PYTHON等等。

使用网页介面上载template代码


使用AWS CLI
aws cloudformation create-stack --stack-name TEST_STACK --template-body file:////home//local//test//sampletemplate.json

使用AWS SDK
PHP:
http://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-cloudformation.html
Ruby:
http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/CloudFormation.html
JavaScript:
http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CloudFormation.html
.NET
http://docs.aws.amazon.com/AWSSdkDocsNET/latest/V3/DeveloperGuide/welcome.html


如何开始使用Cloudformation?

之前我分享过一篇文章提到如何建用Cloudformation来建VPC, 

一说就懂的AWS VPC亚马逊虚拟私有云, 

这次就分享在已经建好VPC的情况下用Cloudformation建PostgreSQL DB,经过一点的修改你可以用这一个template 来建MySQL

{  "AWSTemplateFormatVersion":"2010-09-09",  "Description":"AWS CloudFormation Template for PostgreSQL, template to create a highly-available, RDS DBInstance version 9.3 with alarming on important metrics that indicate the health of the database **WARNING**  ",  "Parameters":{    "VpcId":{      "Type":"String",      "Description":"VpcId of your existing Virtual Private Cloud(VPC)"    },    "Subnets":{      "Type":"CommaDelimitedList",      "Description":"The list of SubnetIds,for at least two Availability Zones in the region"    },    "DBIdentifier":{      "Type":"String",      "Description":"The identifier of this mysql database"    },    "MyDBName":{      "Default":"MyDatabase",      "Description":"The 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":{      "Description":"The database admin account username",      "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":"The database admin account password",      "Type":"String",      "MinLength":"8",      "MaxLength":"41",      "AllowedPattern":"[a-zA-Z0-9]*",      "ConstraintDescription":"must contain only alphanumeric characters."    },    "DBAllocatedStorage":{      "Default":"5",      "Description":"The size of the database (Gb)",      "Type":"Number",      "MinValue":"5",      "MaxValue":"1024",      "ConstraintDescription":"must be between 5 and 1024Gb."    },    "MyDBInstanceClass":{      "Default":"db.m3.medium",      "Description":"The database instance type",      "Type":"String",      "ConstraintDescription":"must select a valid database instance type."    },    "MultiAZDatabase":{      "Default":"false",      "Description":"Create a multi-AZ RDS database instance",      "Type":"String",      "AllowedValues":[        "true",        "false"      ],      "ConstraintDescription":"must be either true or false."    }  },  "Resources":{    "MyDBSubnetGroup":{      "Type":"AWS::RDS::DBSubnetGroup",      "Properties":{        "DBSubnetGroupDescription":"Subnets available for the RDS DB Instance",        "SubnetIds":{          "Ref":"Subnets"        }      }    },    "SecurityGroup":{      "Type":"AWS::EC2::SecurityGroup",      "Properties":{        "GroupDescription":"Allow access to the mysql from the Web Server",        "VpcId":{          "Ref":"VpcId"        },        "SecurityGroupIngress":[          {            "IpProtocol":"tcp",            "FromPort":"5432",            "ToPort":"5432",            "CidrIp":"10.0.0.0/16"          }        ]      }    },    "MyDB":{      "Type":"AWS::RDS::DBInstance",      "Properties":{        "AllocatedStorage":{          "Ref":"DBAllocatedStorage"        },        "AutoMinorVersionUpgrade":"false",        "VPCSecurityGroups":[          {            "Ref":"SecurityGroup"          }        ],        "DBName":{          "Ref":"MyDBName"        },        "DBInstanceClass":{          "Ref":"MyDBInstanceClass"        },        "DBSubnetGroupName":{          "Ref":"MyDBSubnetGroup"        },        "Engine":"postgres",        "EngineVersion":"9.3.10",        "MasterUsername":{          "Ref":"DBUser"        },        "MasterUserPassword":{          "Ref":"DBPassword"        },        "MultiAZ":{          "Ref":"MultiAZDatabase"        },        "StorageType":"gp2",        "Tags":[          {            "Key":"Name",            "Value":{              "Ref":"DBIdentifier"            }          }        ]      }    }  },  "Outputs":{    "JDBCConnectionString":{      "Description":"JDBC connection string for database",      "Value":{        "Fn::Join":[          "",          [            "jdbc:postgresql://",            {              "Fn::GetAtt":[                "MyDB",                "Endpoint.Address"              ]            },            ":",            {              "Fn::GetAtt":[                "MyDB",                "Endpoint.Port"              ]            },            "/",            {              "Ref":"MyDBName"            }          ]        ]      }    },    "DBAddress":{      "Description":"address of database endpoint",      "Value":{        "Fn::GetAtt":[          "MyDB",          "Endpoint.Address"        ]      }    },    "DBPort":{      "Description":"database endpoint port",      "Value":{        "Fn::GetAtt":[          "MyDB",          "Endpoint.Port"        ]      }    }  }}

有任何问题随时都可以联系我!微信:@tursjackychan

0 0
原创粉丝点击