Post

AWS - CodeDevelop - CloudFormation Template - S3 Create

[toc]


Template - setup S3_Website_Bucket_With_No_Retain_On_Delete

S3_Website_Bucket_With_No_Retain_On_Delete:

  • create a publicly accessible S3 bucket configured for website access
  • with no deletion policy

WARNING This template creates an S3 bucket that will be deleted when the stack is deleted.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Resources": {
        "S3Bucket": {
            "Type": "AWS::S3::Bucket",
            "Properties": {
                "AccessControl": "PublicRead",
                "WebsiteConfiguration": {
                    "IndexDocument": "index.html",
                    "ErrorDocument": "error.html"
                }
            }
        },
        "BucketPolicy": {
            "Type": "AWS::S3::BucketPolicy",
            "Properties": {
                "PolicyDocument": {
                    "Id": "MyPolicy",
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Sid": "PublicReadForGetBucketObjects",
                            "Effect": "Allow",
                            "Principal": "*",
                            "Action": "s3:GetObject",
                            "Resource": { "Fn::Join": [ "", [  "arn:aws:s3:::", { "Ref": "S3Bucket" }, "/*" ] ] }
                        }
                    ]
                },
                "Bucket": { "Ref": "S3Bucket" }
            }
        }
    },
    "Outputs": {
        "WebsiteURL": {
            "Value": { "Fn::GetAtt": [ "S3Bucket", "WebsiteURL" ] },
            "Description": "URL for website hosted on S3"
        },
        "S3BucketSecureURL": {
            "Value": { "Fn::Join": [ "", [ "https://", { "Fn::GetAtt": [ "S3Bucket", "DomainName" ] } ] ] },
            "Description": "Name of S3 bucket to hold website content"
        }
    }
}

Template - setup S3_Website_Bucket_With_Retain_On_Delete

S3_Website_Bucket_With_Retain_On_Delete:

  • create a publicly accessible S3 bucket configured for website access
  • with a deletion policy of retail on delete.

WARNING This template creates an S3 bucket that will NOT be deleted when the stack is deleted.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
AWSTemplateFormatVersion: 2010-09-09
Description: >-
  AWS CloudFormation Sample Template S3_Website_Bucket_With_Retain_On_Delete:
  Sample template showing how to create a publicly accessible S3 bucket
  configured for website access with a deletion policy of retail on delete.
  **WARNING** This template creates an S3 bucket that will NOT be deleted when
  the stack is deleted. You will be billed for the AWS resources used if you
  create a stack from this template.
Resources:
  S3Bucket:
    Type: 'AWS::S3::Bucket'
    Properties:
      AccessControl: PublicRead
      WebsiteConfiguration:
        IndexDocument: index.html
        ErrorDocument: error.html
    DeletionPolicy: Retain
Outputs:
  WebsiteURL:
    Value: !GetAtt
      - S3Bucket
      - WebsiteURL
    Description: URL for website hosted on S3
  S3BucketSecureURL:
    Value: !Join
      - ''
      - - 'https://'
        - !GetAtt
          - S3Bucket
          - DomainName
    Description: Name of S3 bucket to hold website content
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Resources": {
        "S3Bucket": {
            "Type": "AWS::S3::Bucket",
            "Properties": {
                "AccessControl": "PublicRead",
                "WebsiteConfiguration": {
                    "IndexDocument": "index.html",
                    "ErrorDocument": "error.html"
                }
            },
            "DeletionPolicy": "Retain"
        },
        "BucketPolicy": {
            "Type": "AWS::S3::BucketPolicy",
            "Properties": {
                "PolicyDocument": {
                    "Id": "MyPolicy",
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Sid": "PublicReadForGetBucketObjects",
                            "Effect": "Allow",
                            "Principal": "*",
                            "Action": "s3:GetObject",
                            "Resource": {
                                "Fn::Join": [
                                    "",
                                    [
                                        "arn:aws:s3:::",
                                        {
                                            "Ref": "S3Bucket"
                                        },
                                        "/*"
                                    ]
                                ]
                            }
                        }
                    ]
                },
                "Bucket": { "Ref": "S3Bucket"}
            }
        }
    },
    "Outputs": {
        "WebsiteURL": {
            "Value": { "Fn::GetAtt": [ "S3Bucket", "WebsiteURL" ] },
            "Description": "URL for website hosted on S3"
        },
        "S3BucketSecureURL": {
            "Value": { "Fn::Join": [ "",[ "https://", { "Fn::GetAtt": [ "S3Bucket", "DomainName" ] } ] ] },
            "Description": "Name of S3 bucket to hold website content"
        }
    }
}
This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.