Cloudformation S3bucket创建

时间:2017-03-23 14:27:50

标签: amazon-s3 amazon-cloudformation

这是我编写的用于创建简单S3存储桶的cloudformation模板,如何指定存储桶的名称?这是正确的方法吗?

{
  "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"
      }
    }
  }
}

3 个答案:

答案 0 :(得分:1)

要扩展“ cloudquiz”答案,这是它在yaml格式中的外观:

Resources:
  SomeS3Bucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName:
        Fn::Join: ["-", ["yourbucketname", {'Fn::Sub': '${AWS::Region}'}, {'Fn::Sub': '${Stage}'}]]

答案 1 :(得分:0)

您的代码已指定BucketName:

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

BucketName是一个字符串,由于您使用的是“Fn Join”,它将与您加入的函数组合在一起。 “内部函数Fn :: Join将一组值附加到单个值中,由指定的分隔符分隔。如果分隔符是空字符串,则值集合没有分隔符。” 如果不更改默认值,则您的存储桶名称为: cloudops级-deplyment-yourAwsRegion

如果您更改默认参数,则可以更改cloudop和stage,部署是硬编码的,您的AWSRegion将从堆栈运行的位置拉出,并将通过Mapping以短格式返回。

答案 2 :(得分: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。

希望这有帮助。