← Solutions Architect Associate study path

Practice in your account · 30 min

A site in a private bucket served by Amazon CloudFront

In this lab you will serve a page from an S3 bucket that nobody can read directly, only your CloudFront distribution, and you will see how caching and an invalidation work.

In this lab you will build a site with two pieces:

  • A private S3 bucket, which is a container for files in Amazon S3 and which in this lab stores the site's page.
  • An Amazon CloudFront distribution, which delivers that file from its edge locations, which are the AWS servers spread around the world that keep copies close to your users.

These two pieces are used together because a file arrives faster from a nearby server than from the bucket, which is in a single Region. For example, if an online store keeps its product photos in a bucket in Ohio, a customer in Lima gets them from an edge location close to them and doesn't have to wait for them to travel from Ohio. These topics belong to domain 1 of the exam, in the part about protecting the origin, and to domain 3, in the part about edge caching, which is what keeping those copies close to users is called.

What connects the two pieces is origin access control (OAC). With OAC, CloudFront signs every request it makes to the bucket, that is, it adds proof that the request comes from your distribution, and the bucket only lets whoever brings that proof read. For example, the store can let everyone see its photos through CloudFront without anyone being able to download them directly from the bucket. At the end of the lab, your bucket's direct URL will respond 403, the that means access denied, and the CloudFront URL will show the page, which is the sign that the bucket is protected.

How does CloudFront deliver an object?

When a request arrives and the edge location (POP) has the object, it responds right there. An object is each file you store in S3, such as a page or a photo. If the edge location doesn't have the object, it asks the regional cache for it, which is a bigger server that serves several edge locations, and if that cache doesn't have it either, it goes to the origin, which in this lab is your bucket.

From the first request the copy stays in the cache, so a change in the bucket doesn't show up in CloudFront until the copy expires or you invalidate it. For example, if the store changes the price shown on its page and uploads the new page to the bucket, customers will keep seeing the old price while the old copy is still in the cache.

Before you start

First do the budget lab, and have a short index.html file ready, for example one that says <h1>Hola desde CloudFront</h1>.

If your account is a new experience account, create the stack in us-east-2, your project's . The template doesn't use an or a custom domain, which is a site name you buy, like a store's, so you don't need anything in us-east-1. CloudFront gives your distribution its own address ending in cloudfront.net, and that is the one you will use.

What the template creates

The [lab13-cloudfront-oac.yaml](/roadmaps/plantillas/lab13-cloudfront-oac.yaml) template is a file that tells CloudFormation what to create, and CloudFormation creates everything together as a stack. This template creates four resources:

  • A bucket, which has Block Public Access turned on and disabled.
  • An origin access control, which signs every request from CloudFront to the bucket.
  • A distribution, which has index.html as its default root object, required (it redirects HTTP) and the CachingOptimized managed cache policy.
  • A bucket policy, which allows s3:GetObject only to the cloudfront.amazonaws.com service and only if the request comes from your distribution (the AWS:SourceArn condition).

The default root object is the file CloudFront delivers when someone opens the address without a file name, and the cache policy decides how long each copy is kept. The bucket policy is the bucket's list of permissions, and the AWS:SourceArn condition holds your distribution's . For example, when a store customer opens the distribution's address, they get index.html over HTTPS, and CloudFront reads that file from the bucket because the policy allows it.

The AWS:SourceArn condition is what limits the permission to your distribution. Without it, the policy would grant permission to the CloudFront service in general, and any distribution in any account could read your bucket. For example, someone else could create their own distribution pointing to the store's bucket and serve its photos from there.

What happens without the condition?

You remove the AWS:SourceArn condition from the bucket policy and keep the permission for cloudfront.amazonaws.com. Who can read your bucket?

Create the distribution and test access

When the stack finishes being created, it gives you its outputs, which are the data you will use in the next steps. For example, UrlCloudFront is the address the store's customers would open, and UrlDirectaS3 is the file's address in the bucket, which nobody should be able to open.

  1. In CloudFormation, choose Create stack, With new resources (standard), Upload a template file and upload lab13-cloudfront-oac.yaml.
  2. Name the stack cloudpath-saa-lab13a-oac. Leave ClaseDePrecio set to PriceClass_All, which includes the South America locations, and choose Submit. The distribution takes a few minutes to deploy.
  3. In Outputs, copy Bucket, UrlCloudFront, UrlDirectaS3 and IdDistribucion.
  4. Open the S3 console, go to the bucket from the output and choose Upload to upload your index.html.
  5. Open UrlCloudFront in the browser. You should see your page.
  6. Open UrlDirectaS3. S3 responds AccessDenied: the file exists, but the bucket only lets your distribution read.
  7. In S3, open the bucket's Permissions tab and read the policy. The principal is cloudfront.amazonaws.com and the AWS:SourceArn condition holds your distribution's ARN.

In a policy, the principal is who receives the permission, and the condition says in which case that permission applies. For example, the store's policy reads like a sentence: the CloudFront service can read the bucket's files, only when the request comes from the store's distribution.

Change the page and create an invalidation

An invalidation tells CloudFront to throw away the stored copy of a file, so that the next request goes to the bucket for the new version. For example, when the store changes its home page, it creates an invalidation for /index.html and its customers see the change as soon as it finishes, without waiting for the old copy to expire.

  1. Change the text of your index.html, upload it again and reload the CloudFront URL. You will still see the old text, because CachingOptimized keeps objects for 24 hours by default.
  2. In the CloudFront console, open your distribution, go to the Invalidations tab and choose Create invalidation. Enter /index.html and choose Create invalidation. When it finishes, reload and you will see the new text.

How to know you're done

To check the lab from , use the URLs from the outputs in place of what goes between < and >:

curl -s -o /dev/null -w "%{http_code}\n" <UrlCloudFront>
curl -s -o /dev/null -w "%{http_code}\n" <UrlDirectaS3>
aws cloudfront list-invalidations --distribution-id <IdDistribucion> \
  --query "InvalidationList.Items[].[Id,Status]" --output table

The first two commands request each address and show only the status code, and the third lists your distribution's invalidations. The first command has to return 200 and the second 403, and the invalidation has to show up as Completed. For example, if the second command returned 200, anyone could read the bucket directly and the policy wouldn't be doing its job.

Delete what you created

So you don't leave anything in the account, delete the lab in this order:

  1. Empty the bucket. CloudFormation doesn't delete a bucket that has objects:

bash aws s3 rm s3://<Bucket> --recursive

  1. In CloudFormation, select cloudpath-saa-lab13a-oac and choose Delete. The distribution takes a few minutes to be disabled and deleted; wait until you see DELETE_COMPLETE.

On the exam

If the question asks for users to read the content only through CloudFront and not directly from S3, the answer is origin access control (OAC) plus a bucket policy. Origin access identity (OAI) is the previous version of that mechanism, and AWS recommends OAC instead.

This answer has two cases the exam can ask about separately. If the bucket is configured as an S3 static website, which is an S3 option for serving a site directly from the bucket, OAC doesn't apply, because that endpoint needs public access. And if users keep seeing old content after an update, it is solved with an invalidation or with versioned file names, which are names that change with each version. For example, if the store uploads its new stylesheet as estilos-v2.css instead of replacing estilos.css, CloudFront has no old copy under that name and delivers the new version from the first request.

Match each case with its solution

0 of 3 pairs

Tap a case and then its solution.

0 of 3 pairs.

Next

  • Get started with a CloudFront standard distribution: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/GettingStarted.SimpleDistribution.html
  • cloud path practice exams: /examenes?lang=en