Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

S3 Creation CloudFormation results in 400 Bad Request

I have this problem when creating S3 bucket using CloudFormation. I get a 400 Bad request. Would appreciate if anyone can help.

aws cloudformation deploy --profile DEV --stack-name testBucket --template-file create_bucket.yml --region us-east-1 --capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM --parameter-overrides BucketName=myBucket

Template:

    AWSTemplateFormatVersion: 2010-09-09
    Parameters:
      BucketName:
        Description: Provisioned read throughput for each table
        Type: String
    Resources:
      MYBUCKET:
        Type: AWS::S3::Bucket
        Properties:
          BucketName: ${BucketName}
      MYBUCKETPOLICY:
        Type: AWS::S3::BucketPolicy
        Properties:
          Bucket: !Ref MYBUCKET
          PolicyDocument:
            Id: ReportPolicy
            Version: "2012-10-17"
            Statement:
              - Sid: ReportBucketPolicyDoc
                Effect: Allow
                Action: "s3:*"
                Principal:
                  AWS: !Join ['', ["arn:aws:iam::", !Ref "AWS::AccountId", ":root"]]
                Resource: !Join ['', ['arn:aws:s3:::', !Ref MYBUCKET, '/*']]

Error

Bad Request (Service: Amazon S3; Status Code: 400; Error Code: 400 Bad Request; Request ID: B4AEAA3C454B7868; S3 Extended Request ID: ATFscTA4dQw8J8AYUfkIARYhiT4/BpVWRcD172WnR75Uzm+i5dlHOTC2HCb9drkO16dzYiELJZc=)

like image 947
Ben Avatar asked Jan 21 '19 15:01

Ben


2 Answers

The following thread is relevant for anyone trying to change the region of an S3 bucket just after they have deleted a bucket in another region.

Essentially there's a delay between a bucket being deleted in one region and the name being available in all other regions. So if you deleted a bucket in ap-southeast-2 and then tried to create a bucket with the same name is us-east-1 you should expect to receive this kind of error.

https://github.com/aws/aws-cdk/issues/6646#issuecomment-597905903

like image 105
Boris Avatar answered Oct 06 '22 01:10

Boris


You did not reference the BucketName parameter correctly:

MYBUCKET:
  Type: AWS::S3::Bucket
    Properties:
    BucketName: !Sub ${BucketName}  # Or !Ref BucketName
like image 43
Baris Avatar answered Oct 06 '22 02:10

Baris