Home > Software design >  How is S3 bucket name being derived in CloudFormation?
How is S3 bucket name being derived in CloudFormation?

Time:12-23

I've this cloudformation script template.js that creates a bucket. I'm bit unsure how the bucket name is being assembled.

Assuming my stackname is my-service I'm getting bucket name created as my-service-s3bucket-1p3s4szy5bomf

  1. I want to know how this name was derived
  2. I also want to get rid of that arn at the end. -1p3s4szy5bomf
  3. Can I skip Outputs at the end, Not sure what they do

Code in template.js

var stackTemplate = {

    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "with S3",
    "Resources": {
        "S3Bucket": {
            "Type": "AWS::S3::Bucket",
            "DeletionPolicy": "Retain",
            "Properties": {},
            "Metadata": {
                "AWS::CloudFormation::Designer": {
                    "id": "bba483af-4ae6-4d3d-b37d-435f66c42e44"
                }
            }
        },
        "S3BucketAccessPolicy": {
            "Type": "AWS::IAM::Policy",
            "Properties": {
                "PolicyName": "S3BucketAccessPolicy",
                "Roles": [
                    {
                        "Ref": "IAMServiceRole"
                    }
                ],
                "PolicyDocument": {
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Effect": "Allow",
                            "Action": [
                                "s3:DeleteObject",
                                "s3:GetObject",
                                "s3:PutObject",
                                "s3:PutObjectAcl",
                                "s3:List*"
                            ],
                            "Resource": [
                                {
                                    "Fn::Sub": [
                                        "${S3BucketArn}",
                                        {
                                            "S3BucketArn": {
                                                "Fn::GetAtt": ["S3Bucket", "Arn"]
                                            }
                                        }
                                    ]
                                },
                                {
                                    "Fn::Sub": [
                                        "${S3BucketArn}/*",
                                        {
                                            "S3BucketArn": {
                                                "Fn::GetAtt": ["S3Bucket", "Arn"]
                                            }
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            }
        }
    },
    "Outputs": {
        "s3Bucket": {
            "Description": "The created S3 bucket.",
            "Value": {
                "Ref": "S3Bucket"
            },
            "Export": {
                "Name": {
                    "Fn::Sub": "${AWS::StackName}-S3Bucket"
                }
            }
        },
        "s3BucketArn": {
            "Description": "The ARN of the created S3 bucket.",
            "Value": {
                "Fn::GetAtt": ["S3Bucket", "Arn"]
            },
            "Export": {
                "Name": {
                    "Fn::Sub": "${AWS::StackName}-S3BucketArn"
                }
            }
        }
    }
};

stackUtils.assembleStackTemplate(stackTemplate, module);

CodePudding user response:

I want to know how this name was derived

If you don't specify a name for your bucket, CloudFormation generate a new one based on the pattern $name-of-stack-s3bucket-$generatedId
from documentation https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-s3-bucket.html BucketName A name for the bucket. If you don't specify a name, AWS CloudFormation generates a unique ID and uses that ID for the bucket name.

I also want to get rid of that arn at the end. -1p3s4szy5bomf

You can assign a name of you bucket, but AWS recommand to let it empty to generate a new one, to avoid creation with the same name (stackset...) by CloudFormation example :

"Resources": {
        "S3Bucket": {
            "Type": "AWS::S3::Bucket",
            "DeletionPolicy": "Retain",
            "Properties": {
               "BucketName": "DesiredNameOfBucket" <== 
                },
            "Metadata": {
                "AWS::CloudFormation::Designer": {
                    "id": "bba483af-4ae6-4d3d-b37d-435f66c42e44"
                }
            }
        },

Can I skip Outputs at the end, Not sure what they do

It is used to have the information, name and the ARN of the bucket created, if you want you can delete the Outputs part from your template

  • Related