Below you will find pages that utilize the taxonomy term “cloudformation”
Posts
Deploy s3 bucket by cloudformation
預先準備 需要先把 aws cli 給裝起來
利用CLI可以把預先寫好的基礎建設結構佈署到AWS上,下面是簡單部署一個S3的bucket範例。
aws cloudformation deploy --stack-name paulteststack --template-file ./s3-create-template.json --stack-name: 想要deploy到哪一個cloudformation stack裡 --template-file: template 檔案的所在位置 還有很多參數可以設定,詳情可以參考 create-stack
{ "AWSTemplateFormatVersion": "2010-09-09", "Description": "Create S3 bucket on AWS", "Resources": { "S3Test": { "Type": "AWS::S3::Bucket", "Properties": { "BucketName": "paul-test-1495611707" } } } } template 可以依照情境進行更進階的調整,例如調整bucket的CROS讓外部的前端程式可以進行存取。 { "AWSTemplateFormatVersion": "2010-09-09", "Description": "Create S3 bucket on AWS", "Resources": { "S3Test": { "Type": "AWS::S3::Bucket", "Properties": { "BucketName": "paul-test-1495611707", "AccessControl": "PublicReadWrite", "CorsConfiguration": { "CorsRules": [ { "AllowedHeaders": [ "*" ], "AllowedMethods": [ "GET" ], "AllowedOrigins": [ "*" ], "ExposedHeaders": [ "Date" ], "Id": "myCORSRuleId1", "MaxAge": "3600" }, { "AllowedHeaders": [ "x-amz-*" ], "AllowedMethods": [ "DELETE" ], "AllowedOrigins": [ "http://www.
Posts
Deploy s3 bucket by cloudformation - 進階版
開發系統時部分的程式碼會參考到基礎建設的名稱,例如:S3 bucket自動部署創建出來的名稱,或是需要利用aws resource arn去做trigger lambda事件的綁定,都需要輸出佈建後的結果。 { "AWSTemplateFormatVersion": "2010-09-09", "Description": "Create S3 bucket on AWS", "Parameters": { "StageName": { "Type": "String" } }, "Resources": { "S3Test": { "Type": "AWS::S3::Bucket", "Properties": { "BucketName": { "Fn::Sub": [ "paul-test-${StageName}", { "StageName": { "Ref": "StageName" } } ] }, "AccessControl": "PublicReadWrite", "CorsConfiguration": { "CorsRules": [ { "AllowedHeaders": [ "*" ], "AllowedMethods": [ "GET" ], "AllowedOrigins": [ "*" ], "ExposedHeaders": [ "Date" ], "Id": "myCORSRuleId1", "MaxAge": "3600" } ] } } } }, "Outputs": { "S3Test": { "Description": "Information about s3 bucket name", "Value": { "Fn::Sub": [ "paul-test-${StageName}", { "StageName": { "Ref": "StageName" } } ] }, "Export": { "Name": { "Fn::Sub": [ "${StackName}-S3Test-bucketname", { "StackName": { "Ref": "AWS::StackName" } } ] } } } } } Fn::Sub: 代表字串裡的變數,可用接下來的變數給取代。當字串不需要有變數取代時,可以簡單的利用{ "Fn::Sub" : String }描述即可。需要由變數替換的模板為{ "Fn::Sub" : [ String, { Var1Name: Var1Value, Var2Name: Var2Value } ] }。