2017-03-23 60 views
0

下面是我爲創建一個簡單的S3存儲桶而編寫的cloudformation模板,如何指定存儲桶的名稱?這是正確的方式嗎?Cloudformation S3bucket創建

{ 
    "AWSTemplateFormatVersion": "2010-09-09", 
    "Description": "Simple S3 Bucket", 
    "Parameters": { 
    "OwnerService": { 
     "Type": "String", 
     "Default": "CloudOps", 
     "Description": "Owner or service name. Used to identify the owner of the vpc stack" 
    }, 
    "ProductCode": { 
     "Type": "String", 
     "Default": "cloudops", 
     "Description": "Lowercase version of the product code (i.e. jem). Used for tagging" 
    }, 
    "StackEnvironment": { 
     "Type": "String", 
     "Default": "stage", 
     "Description": "Lowercase version of the environment name (i.e. stage). Used for tagging" 
    } 
    }, 
    "Mappings": { 
    "RegionMap": { 
     "us-east-1": { 
     "ShortRegion": "ue1" 
     }, 
     "us-west-1": { 
     "ShortRegion": "uw1" 
     }, 
     "us-west-2": { 
     "ShortRegion": "uw2" 
     }, 
     "eu-west-1": { 
     "ShortRegion": "ew1" 
     }, 
     "ap-southeast-1": { 
     "ShortRegion": "as1" 
     }, 
     "ap-northeast-1": { 
     "ShortRegion": "an1" 
     }, 
     "ap-northeast-2": { 
     "ShortRegion": "an2" 
     } 
    } 
    }, 
    "Resources": { 
    "JenkinsBuildBucket": { 
     "Type": "AWS::S3::Bucket", 
     "Properties": { 
     "BucketName": { 
      "Fn::Join": [ 
      "-", 
      [ 
       { 
       "Ref": "ProductCode" 
       }, 
       { 
       "Ref": "StackEnvironment" 
       }, 
       "deployment", 
       { 
       "Fn::FindInMap": [ 
        "RegionMap", 
        { 
        "Ref": "AWS::Region" 
        }, 
        "ShortRegion" 
       ] 
       } 
      ] 
      ] 
     }, 
     "AccessControl": "Private" 
     }, 
     "DeletionPolicy": "Delete" 
    } 
    }, 
    "Outputs": { 
    "DeploymentBucket": { 
     "Description": "Bucket Containing Chef files", 
     "Value": { 
     "Ref": "DeploymentBucket" 
     } 
    } 
    } 
} 
+0

語法看起來不錯。運行後會發生什麼錯誤? –

+0

檢查您傳入的值作爲參數來構建存儲桶的名稱。存儲桶名稱必須符合此處記錄的AWS S3命名策略:http://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html#bucketnamingrules – Aditya

回答

0

你的代碼有BucketName已經規定:

"BucketName": { 
     "Fn::Join": [ 
     "-", 
     [ 
      { 
      "Ref": "ProductCode" 
      }, 
      { 
      "Ref": "StackEnvironment" 
      }, 
      "deployment", 
      { 
      "Fn::FindInMap": [ 
       "RegionMap", 
       { 
       "Ref": "AWS::Region" 
       }, 
       "ShortRegion" 
      ] 
      } 
     ] 
     ] 
    }, 

的BucketName是一個字符串,因爲你使用的是「FN加入」,它會結合您要加入的功能。 「內部函數Fn :: Join將一組值附加到由指定分隔符分隔的單個值中。如果分隔符是空字符串,則該值集合將不帶分隔符。」 你斗的名字,如果你不改變默認設置是: cloudops級-deplyment-yourAwsRegion

如果要更改默認的參數,那麼這兩個cloudops,舞臺是可以改變的,部署硬編碼,yourAWSRegion會從堆棧運行的地方拉出,並通過映射以短格式返回。

0

下面是一個非常簡單的Cloudformation模板,用於創建S3存儲桶,包括定義存儲桶名稱。

AWSTemplateFormatVersion: '2010-09-09' 
Description: create a single S3 bucket 

Resources: 
    SampleBucket: 
    Type: AWS::S3::Bucket 
    Properties: 
     BucketName: sample-bucket-0827-cc 

如果您希望AWS爲您命名存儲桶,也可以關閉「Properties:BucketName」行。然後它將看起來像$ StackName-SampleBucket- $ uniqueIdentifier。

希望這會有所幫助。