AWS云编排详解-Cloud Formation
作者:私语茶馆
1.关键概念
名词 |
说明 |
软件: |
|
CloudFormation |
描述AWS 资源、配置值和互连关系。借助集成设施即代码加快云部署 |
CloudFormation Designer |
拖拽式图形化模板编辑界面。 |
Amazon Simple Notification Service (SNS) |
SNS可通过电子邮件跟踪堆栈的创建和删除进度,以及以编程方式和其他流程集成。 |
概念: |
|
Stack |
实例化模板所产生的资源集合 |
2. CloudFormation介绍
Cloud Formation使用模板和堆栈简化资源管理。
CloudFormation的特征:
(1)文件格式:JSON(Javascript 数据元表示法)格式的文本文件。
(2) 管理服务间关系:模板能简要地获取资源间的相互关系,例如 EC2 实例必须与 Elastic Load Balancing 负载均衡器相关联,或者 EBS 卷必须与其连接的实例位于同一 EC2 可用区域内。
(3)反复使用:使用模板参数可使单个模板用于具有不同配置值的多个基础设施部署,例如要为该应用程序部署多少实例的模板。
(4)获取帮助反馈:模板还提供输出属性,以便向用户返回部署结果或配置信息。例如,完成实例化后,模板可能向客户提供 Elastic Load Balancing 终端节点的 URL,用于连接最新实例化的应用程序。
(5)避免冲突:模板中的所有 AWS 资源通过逻辑名称进行标识,以便在一个模板内创建多个堆栈时 AWS 资源之间不会产生命名冲突。
(6)即写即用:使用任何方法启动堆栈,无需提前使用 AWS CloudFormation 注册模板。
(7)可视化您的堆栈:CloudFormation Designer 让您能够以图表形式显示模板。您可以轻松地查看 AWS 资源及其之间的关系并安排布局,使图表更符合您的意图。您可以借助拖放界面和集成的 JSON 编辑器编辑模板。您对图表所做的修改会自动反映到模板的 JSON 中。
(8)查询资源:AWS CloudFormation 保留堆栈模板的副本,以便您可以使用 AWS 管理控制台、命令行工具或 API 查询堆栈创建期间应用的确切资源配置。
(9)自动化:您可以选择使用编程语言或所选工具自动生成模板。您还可以选择使用 CloudFormation API、AWS SDK 或 AWS CLI 从模板中自动创建堆栈。
3.CloudFormation模板
资料:http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-guide.html
3.1. CloudFormation模板格式介绍
格式:
{
"AWSTemplateFormatVersion" : "2010-09-09"
"Description" : "模板使用方法的文本描述",
"Parameters": { // 用于根据每个部署自定义模板的一组输入值 },
"Resources" : { // 一组 AWS 资源及其相互关系,必选},
“Mappings”:{// 声明条件值,条件值的求值方式与查找表语句相似}
"Outputs" : { // 堆栈创建者可以看到的一组值 }
}
EC2样例:
{
"AWSTemplateFormatVersion" : "2010-09-09"
"Description" : "创建运行 Amazon Linux 32 位 AMI 的 EC2 实例。",
"Parameters" : {
"KeyPair" : {
"Description" : "允许 SSH 访问此实例的 EC2 密钥对",
"Type" : "String"
}
},
"Resources" : {
"Ec2Instance" : {
"Type" : "AWS::EC2::Instance",
"Properties" : {
"KeyName" : { "Ref" : "KeyPair" },
"ImageId" : "ami-3b355a52"
}
}
},
"Outputs" : {
"InstanceId" : {
"Description" : "新创建的 EC2 实例的实例 ID",
"Value" : {
"Ref" : "Ec2Instance"
}
}
}
}
完整Template见附件:WordPress_Single_Instance_With_RDS.template,下载地址:
详细语法如下:
- AWSTemplateFormatVersion:本模板满足的AWS CloudFormation模板的版本,目前只有2010-09-09有效。
- Parameters:用来指定创建堆栈时,由用户指定的参数及有效性检验规则。
- Mapping:相当于Switch Case语句,按照条件确定一个值。
- OutPut:描述Stack的属性。
- Description:定义提示。用于在Stack创建Wizard时显示在“Specify Parameter page”。
- 内嵌函数:
- 通过Ref和Fn::GetAt 函数可以在模板中引用其他资源的属性。
- 通过Fn::Join函数可以根据parameters、资源属性和其他字符串构建一个值。
- 附加属性:
- DependsOn 属性:指定资源创建和删除的依赖关系。例如被依赖的资源需要先创建。
- DeletionPolicy 属性:定义删除Stack时,如何处理资源,可以同步删除资源或者备份资源。
元数据:该属性为资源指定一个结构化数据。
3.1.模板-Resources
Resources用于定义模板中包括的资源。
3.1.1. 资源模板样例
样例1:
{
"Resources" : {
"HelloBucket" : {
"Type" : "AWS::S3::Bucket", //资源类型,格式:AWS::ProductIdentifier::ResourceType
"Properties" : { //定义资源的属性,不同的资源类型有不同的属性。
"AccessControl" : "PublicRead",
"WebsiteConfiguration" : {
"IndexDocument" : "index.html", //IndexDocument-WebSiteConfiguration的子属性,subProperty
"ErrorDocument" : "error.html"
}
}
}
}
}
样例2(Ref函数):
{
"Parameters" : { //定义了KeyName作为创建Instance时必须输入的参数。
"KeyName" : {
"Description" : "The EC2 Key Pair to allow SSH access to the instance",
"Type" : "AWS::EC2::KeyPair::KeyName"
}
},
"Resources" : {
"Ec2Instance" : {
"Type" : "AWS::EC2::Instance",
"Properties" : {
"SecurityGroups" : [ { "Ref" : "InstanceSecurityGroup" }, "MyExistingSecurityGroup" ],// REF指定的安全组来自模板定义,“MyExistingSecurityGroup”则是一个已部署的安全组。
"KeyName" : { "Ref" : "KeyName"}, //KeyName通过Ref函数说明是创建Instance时必须输入的,
"ImageId" : "ami-7a11e213"
}
},
"InstanceSecurityGroup" : {
"Type" : "AWS::EC2::SecurityGroup",
"Properties" : {
"GroupDescription" : "Enable SSH access via port 22",
"SecurityGroupIngress" : [ {
"IpProtocol" : "tcp",
"FromPort" : "22",
"ToPort" : "22",
"CidrIp" : "0.0.0.0/0"
} ]
}
}
}
}
样例3(GetAtt函数)
"Resources" : {
"myBucket" : {
"Type" : "AWS::S3::Bucket"
},
"myDistribution" : {
"Type" : "AWS::CloudFront::Distribution", //定义一个CloudFront的CDN
"Properties" : {
"DistributionConfig" : {
"Origins" : [ {
"DomainName": {"Fn::GetAtt" : ["myBucket", "DomainName"]}, //提供Bucket的Domain名称。
"Id" : "myS3Origin",
"S3OriginConfig" : { }
} ],
"Enabled" : "true",
"DefaultCacheBehavior" : {
"TargetOriginId" : "myS3Origin",
"ForwardedValues" : {
"QueryString" : "false"
},
"ViewerProtocolPolicy" : "allow-all"
}
}
}
}
}
3.1.2.关键特性:
(1)模板中的资源.name是逻辑名,当CloudFormation创建资源时,会产生资源实例的物理名称。资源的物理名称= logical name + stack name + unique ID。
(2)CloudFormation有内嵌函数,例如Ref函数,格式如下:
Fn::Ref:
The intrinsic function Ref returns the value of the specified parameter or resource.
When you specify a parameter's logical name, it returns the value of the parameter.
When you specify a resource's logical name, it returns a value that you can typically use to refer to that resource, such as a physical ID.
Declaration
"Ref" : "logicalName"
Parameters
logicalName
The logical name of the resource or parameter you want to dereference.
Return Value
The physical ID of the resource or the value of the parameter.
样例如下:
"MyEIP" : { //定义一个Elastic IP
"Type" : "AWS::EC2::EIP",
"Properties" : {
"InstanceId" : { "Ref" : "MyEC2Instance" } //应用到一个MyEC2Instance的实例。
}
}
Ref函数适合于其返回的资源恰好是你想要的情况。但有的情况你需要资源的其他属性,需要使用GetAtt函数,格式如下:
Fn::GetAtt
The intrinsic function Fn::GetAtt returns the value of an attribute from a resource in the template.
Declaration
"Fn::GetAtt" : [ "logicalNameOfResource", "attributeName" ]
Parameters:
logicalNameOfResource
The logical name of the resource that contains the attribute you want.
attributeName
The name of the resource-specific attribute whose value you want. See the resource's reference page for details about the attributes available for that resource type.
Return Value
The attribute value.
Example
This example returns a string containing the DNS name of the LoadBalancer with the logical name MyLB.
"Fn::GetAtt" : [ "MyLB" , "DNSName" ]
其他参考资料:
1)资源的属性: