In today’s cloud-driven world, securing your assets while ensuring efficient delivery is paramount. Amazon S3 is a reliable and scalable storage solution, but serving files directly from S3 might expose your data to unwanted access. AWS CloudFront, a content delivery network (CDN), can help secure and optimize the distribution of your S3 assets. This blog post will guide you through setting up a secure distribution using CloudFront, implementing S3 bucket policies to restrict direct access, and verifying the security of your configuration.

Setting Up Your Secure Distribution: A Walkthrough of CloudFront Configuration

Step 1: Create an S3 Bucket

  1. Log into the AWS Management Console.
  2. Navigate to the S3 service and create a new bucket.
  3. Name your bucket, choose a region, and click “Create Bucket.”

Step 2: Configure CloudFront Distribution

  1. Navigate to the CloudFront console and click “Create Distribution.”
  2. Select “Web” as the delivery method and click “Get Started.”
  3. In the “Origin Settings” section, set your S3 bucket as the origin. Ensure the “Restrict Bucket Access” option is checked to enhance security.
  4. Configure the “Default Cache Behavior Settings” to your requirements. For enhanced security, ensure that the “Viewer Protocol Policy” is set to “Redirect HTTP to HTTPS” or “HTTPS Only.”
  5. Customize other settings such as price class, distribution state, and logging per your needs.
  6. Click “Create Distribution” to finalize the setup.

Step 3: Set Up Origin Access Identity (OAI)

  1. In the CloudFront distribution settings, create a new OAI.
  2. Attach the OAI to your CloudFront distribution, ensuring it has the necessary permissions to access your S3 bucket.
  3. Update the bucket policy to grant the OAI permission to read from the bucket.

Restricting Direct Access: Implementing S3 Bucket Policies for CloudFront-Only Delivery

Step 1: Update the S3 Bucket Policy

  1. Navigate to your S3 bucket and go to the “Permissions” tab.
  2. Under “Bucket Policy,” click “Edit” and add the following policy:

{

    “Version”: “2012-10-17”,

    “Statement”: [

        {

            “Sid”: “AllowCloudFrontServicePrincipalReadOnly”,

            “Effect”: “Allow”,

            “Principal”: {

                “AWS”: “arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity YOUR_OAI_ID”

            },

            “Action”: “s3:GetObject”,

            “Resource”: “arn:aws:s3:::YOUR_BUCKET_NAME/*”

        },

        {

            “Sid”: “DenyAllOtherAccess”,

            “Effect”: “Deny”,

            “Principal”: “*”,

            “Action”: “s3:GetObject”,

            “Resource”: “arn:aws:s3:::YOUR_BUCKET_NAME/*”,

            “Condition”: {

                “StringNotEquals”: {

                    “aws:Referer”: “YOUR_CLOUDFRONT_DISTRIBUTION_URL”

                }

            }

        }

    ]

}

Replace YOUR_OAI_ID, YOUR_BUCKET_NAME, and YOUR_CLOUDFRONT_DISTRIBUTION_URL with the appropriate values.

Step 2: Test the Bucket Policy

  1. Ensure the bucket policy is correctly formatted and applied.
  2. Test accessing the S3 bucket URL directly to confirm that access is denied.
  3. Access the same content via the CloudFront URL to verify that access is granted.

Verifying Your Security: Testing Your S3 Lockdown and CloudFront Setup

Step 1: Access Test

  1. Attempt to access a file in your S3 bucket directly using the S3 URL. You should receive an access denied error.
  2. Access the same file using the CloudFront distribution URL. The file should load correctly, confirming that the policy is working.

Step 2: Check CloudFront Logs

  1. Enable logging in CloudFront to monitor access requests.
  2. Analyze the logs to ensure requests are routed through CloudFront, and no unauthorized access attempts occur.

Step 3: Monitor and Adjust

  1. Regularly review your CloudFront and S3 settings to maintain security.
  2. Adjust your bucket policy or CloudFront configuration to address security concerns or performance issues.

Following these steps, you can leverage AWS CloudFront to securely distribute your S3 assets securely, ensuring efficient delivery while protecting your data from unauthorized access.

References

Security best practices for Amazon S3

Configure secure access and restrict access to content