2017-03-23 37 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。

希望这会有所帮助。