Cloudformation S3bucket 创建

Cloudformation S3bucket creation

这是我为创建一个简单的 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"
      }
    }
  }
}

您的代码已经指定了 BucketName:

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

BucketName 是一个字符串,由于您使用的是 'Fn Join',它将结合您要加入的函数。 "The intrinsic function Fn::Join appends a set of values into a single value, separated by the specified delimiter. If a delimiter is the empty string, the set of values are concatenated with no delimiter." 如果您不更改默认值,您的存储桶名称是: cloudops-stage-deplyment-yourAwsRegion

如果更改默认参数,则可以更改 cloudops 和 stage,部署是硬编码的,yourAWSRegion 将从堆栈所在的位置拉取 运行,并将以短格式返回映射。

这是一个非常简单的 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.

希望对您有所帮助。

要扩展 'cloudquiz' 答案,这是它在 yaml 格式中的样子:

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