作者:私語茶館
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"ParameterslogicalNameThe logical name of the resource or parameter you want to dereference.Return ValueThe 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)資源的屬性: