User Tools

Site Tools


aws:aws-cloudformation-s3

S3 buckets

S3 is an Object store not a filesystem although at first glance, it can seem like one.

Although S3 buckets are tied to a region, the name for a bucket must be globally unique. This can pose a problem in deploying code developed in Dev to Prod as if bucket names are hard coded, there will be a conflict beween Prod and Dev on deployment. Editing code between deployments to change a bucket name is not a great idea, but introducing a limited amount of randomness to the name can avoid this.

Using a Env Variable or Parameter !Join-ed to a bucket name can help, but it still requires some setup outside of the deployed code.

Developing an idea I have used in BASH for adding a process id ($$) to a temp file to avoid clashes if a script is launched multiple times so there are several copies running at the same time.

#!/bin/bash
REPORT_FILE="/tmp/email_queue_report-$$"

If we are running a Cloudformation stack, the stack id is guaranteed to be unique in our account and probably is globally too,
(arn:aws:cloudformation:eu-XXX-1:123498762345:stack/TagsTest/cf735210-f0f1-11ee-913e-0694f5f53649)

We can !Split this up and then !Select the 5th field (as it's the longest and most likely to be unique) for the AWS::StackId:-

AWS::StackId is an AWS pseudo parameter, meaning it is set by AWS.

!Select
  - 4
  - !Split
    - "/"
    - !Ref "AWS::StackId"

Gives cf735210-f0f1-11ee-913e-0694f5f53649, then we can split this on - to pick out the 1st, 2nd or 3rd part as desired. I've used field 4 to make this the highest chance of being unique as it's the longest. But is reality, the likekihood of a clash on any field is low.

- !Select
  - 4
  - !Split
    - "-"
      - !Select
      - 2
      - !Split
        - "/"
        - !Ref "AWS::StackId"

Then we can !Join the bucket name to make a predictable but unique name. As this is an unpredictable name, we probably need to store it somewhere so we can use it elsewhere. Here I'm using Parameter Store so the bucket name can be available to coe in a Lambda function. We could of course set an OS environment variable for Lambda, but Parameter Store is more generally usable.

---
AWSTemplateFormatVersion: "2010-09-09"
Description: Test for random S3 bucketname and parameter store.

Resources:
  ReportsBucket:
      Type: AWS::SSM::Parameter
      Properties:
        Name: UniqueNameBucket
        Type: String
        Value: !Ref  UniqueNameBucket
 

  UniqueNameBucket:
    Type: "AWS::S3::Bucket"
    Properties:
      BucketName: !Join
        - "-"
        - "uniquebucket"
          - !Select
            - 4
            - !Split
              - "-"
              - !Select
                - 2
                - !Split
                  - "/"
                  - !Ref "AWS::StackId"

Outputs:
  UniqueNameBucket:
    Description: Name of bucket created for Uniqueness test
    Value: !Ref UniqueNameBucket    

This page has been accessed for:-
Today: 1
Yesterday: 0
Until now: 110

aws/aws-cloudformation-s3.txt · Last modified: by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki