今天豆子嘗試着使用Cloudformation Designer來建立一個Template 文件。Designer對於初學者仍是頗有幫助的,一方面能夠經過可視界面拖動Resource,另外能夠按住Ctrl+Space 來提示對應的Property裏面能夠配置哪些東西。還能夠直接拖動線條來實現依賴關係。固然最重要的一點是能夠幫忙進行語法檢測。sass
下面是一個小例子。ide
日常豆子須要常常建立S3的bucket進行備份,對於每一個Bucket須要配置lifecycle rule,建立對應的user policy,attach到一個新的User,而後配置這個新用戶的Secretkey,最後在客戶端寫個小腳本按期備份。code
如今把AWS的這一部分都改爲用Cloudformation實現。orm
基本的resource就這4個部分。雙擊rerouce能夠進行編輯對應的內容,在空格處隨便單擊一下 就能夠編輯Parameter等section
blog
編輯過程當中,能夠從對應的Resource裏面點開他的幫助文檔,這個是一個很方便的功能ip
提交以前,記得檢查語法。ci
{ "AWSTemplateFormatVersion": "2010-09-09", "Metadata": { "AWS::CloudFormation::Designer": { "5f48a1ce-1a7b-44e3-b9b6-b83719bded79": { "size": { "width": 60, "height": 60 }, "position": { "x": 510, "y": 120 }, "z": 0, "embeds": [] }, "e0e4e707-9bea-46b5-ab39-588be81cb5e3": { "size": { "width": 60, "height": 60 }, "position": { "x": -20, "y": 110 }, "z": 0, "embeds": [] }, "b81b1033-9cc0-4248-b807-fb575fd54687": { "size": { "width": 60, "height": 60 }, "position": { "x": 160, "y": 240 }, "z": 0, "embeds": [], "isassociatedwith": [ "e0e4e707-9bea-46b5-ab39-588be81cb5e3" ], "dependson": [ "e0e4e707-9bea-46b5-ab39-588be81cb5e3" ] }, "83bda625-f2d4-4de1-8b66-d475476308ed": { "size": { "width": 60, "height": 60 }, "position": { "x": -90, "y": 210 }, "z": 0, "embeds": [] }, "fc691126-aba2-4041-a755-93a1a15ac969": { "source": { "id": "b81b1033-9cc0-4248-b807-fb575fd54687", "selector": "g:nth-child(1) g:nth-child(4) g:nth-child(1) circle:nth-child(1) ", "port": "AWS::RefLink-AWS::IAM::User-Users" }, "target": { "id": "e0e4e707-9bea-46b5-ab39-588be81cb5e3" }, "z": 12 }, "55264e1b-ae17-48b2-9508-fef96a0ca753": { "source": { "id": "b81b1033-9cc0-4248-b807-fb575fd54687", "selector": "g:nth-child(1) g:nth-child(4) g:nth-child(1) circle:nth-child(1) ", "port": "AWS::RefLink-AWS::IAM::User-Users" }, "target": { "id": "e0e4e707-9bea-46b5-ab39-588be81cb5e3" }, "z": 12 }, "5585399a-b5be-4e8e-8779-6bc1471da1f5": { "source": { "id": "b81b1033-9cc0-4248-b807-fb575fd54687" }, "target": { "id": "e0e4e707-9bea-46b5-ab39-588be81cb5e3" }, "z": 11 } } }, "Resources": { "IAMUser": { "Type": "AWS::IAM::User", "Properties": { "UserName": { "Ref": "UserName" } }, "Metadata": { "AWS::CloudFormation::Designer": { "id": "e0e4e707-9bea-46b5-ab39-588be81cb5e3" } } }, "Bucket": { "Type": "AWS::S3::Bucket", "Properties": { "BucketName": { "Ref": "S3BucketName" }, "LifecycleConfiguration": { "Rules": [ { "Id": "GlacierRule", "Prefix": "glacier", "Status": "Enabled", "ExpirationInDays": "365", "Transitions": [ { "TransitionInDays": "1", "StorageClass": "GLACIER" } ] } ] } }, "Metadata": { "AWS::CloudFormation::Designer": { "id": "5f48a1ce-1a7b-44e3-b9b6-b83719bded79" } } }, "IAMPolicy": { "Type": "AWS::IAM::Policy", "Properties": { "Users": [ { "Ref": "IAMUser" } ], "PolicyName": "Test", "PolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": "s3:ListBucket", "Resource": { "Fn::Join": [ ":", [ "arn:aws:s3::", { "Ref": "Bucket" } ] ] } }, { "Sid": "VisualEditor1", "Effect": "Allow", "Action": [ "s3:ListAllMyBuckets", "s3:HeadBucket" ], "Resource": "*" }, { "Sid": "VisualEditor2", "Effect": "Allow", "Action": "s3:*", "Resource": { "Fn::Join": [ "/", [ { "Fn::Join": [ ":", [ "arn:aws:s3::", { "Ref": "Bucket" } ] ] }, "*" ] ] } } ] } }, "Metadata": { "AWS::CloudFormation::Designer": { "id": "b81b1033-9cc0-4248-b807-fb575fd54687" } }, "DependsOn": [ "IAMUser" ] }, "IAMKey": { "Type": "AWS::IAM::AccessKey", "Properties": { "UserName": { "Ref": "IAMUser" } }, "Metadata": { "AWS::CloudFormation::Designer": { "id": "83bda625-f2d4-4de1-8b66-d475476308ed" } }, "DependsOn": [ "IAMUser" ] } }, "Parameters": { "S3BucketName": { "Type": "String" }, "UserName": { "Type": "String" }, "PolicyName": { "Type": "String" } }, "Outputs": { "BucketName": { "Value": { "Ref": "Bucket" }, "Description": "S3 Bucket Name" }, "SecretAccessKey": { "Value": { "Fn::GetAtt": [ "IAMKey", "SecretAccessKey" ] }, "Description": "Secret Access Key" }, "AccessKeyID": { "Value": { "Ref": "IAMKey" }, "Description": "Access ID" } } }
下面是stack建立的過程,由於我設置了依賴關係,因此順序是S3 Bucket, User, UserPolicy和UserKey
文檔
最後輸出個人AccessID和SecretKey,這樣我在客戶端才能調用
get