Practice in your account · 30 min
An Amazon S3 bucket with versioning and a lifecycle rule
In this lab you will create an S3 bucket, upload two versions of a file and add a lifecycle rule for the previous versions, and as an extra you will be able to publish it as a static website.
In this lab you will create a bucket in Amazon S3, the AWS service for storing files on the internet, upload a file and then upload another version of the same file. Next you will add a lifecycle rule to the bucket, which moves the previous versions to a cheaper storage class and later deletes them. At the end, if you want, you can publish the bucket as a static website.

In S3 you will work with three parts: the bucket is the container, the object is the file and the key is the full name of that file inside the bucket. For example, an online store can have a bucket for its product photos, where each photo is an object and products/blue-shoe.jpg is the key of one of them.
Versioning is a bucket option that keeps every change to a file instead of replacing it. With versioning enabled, the same object can have several versions with the same key. For example, if the store uploads a new photo of the blue shirt with the same key and the photo came out wrong, it can still recover the previous one.
Before you start
Do the zero spend budget lab first, so the email warns you if something starts charging. Also have a small file ready to upload, such as a photo or a text document. If you are going to do the static website part, also create an index.html file, which is the main page of a website, written in HTML, with one line, for example <h1>Mi primer sitio en S3</h1>.
In the full account, choose the Region before you create the bucket. In the new experience, the bucket ends up in us-east-2.
Steps in the console
Create the bucket
The bucket is created with versioning enabled and public access blocked, which means nobody outside your account can see what you store. For example, if someone guesses the name of your bucket and the address of a file, they get an error instead of the file. Follow these steps:
- Open the Amazon S3 console (https://console.aws.amazon.com/s3/).
- In the left pane, choose General purpose buckets and then Create bucket.
- In Bucket name, enter a name of 3 to 63 characters with lowercase letters, numbers and hyphens, for example
cloudpath-lab06-tunombre-2026. Don't include personal data, because the name appears in URLs, and choose carefully, because you won't be able to change it later. - Leave Block all public access turned on.
- In Bucket Versioning, choose Enable.
- In Tags, add the key
cloudpath:labwith the valuelab06. That tag is a note you use later to recognize the resources from this lab. - Leave the default encryption and choose Create bucket.
Upload a file and a second version
With versioning enabled, every time you upload a file with the same name, S3 stores a new version and keeps the previous one. The new version becomes the current one, which is the one S3 delivers when someone asks for the file, and the previous one becomes noncurrent. Upload them with these steps:
- Choose the name of your bucket. On the Objects tab, choose Upload, then Add files, choose your file and then Upload.
- Change something in the file and upload it again with the same name.
- On the Objects tab, turn on Show versions. You will see two versions with the same key: the current one and the previous one.
Add the lifecycle rule

A storage class is a way of storing objects in S3 with its own price. S3 Standard-IA, for example, costs less to store and is meant for files that are rarely opened. A lifecycle rule tells S3 when to move objects to another storage class and when to delete them, so you don't have to do it by hand. In the example in the S3 guide, an object moves to cheaper classes as the months go by and in the end S3 deletes it.
The rule in this lab does something similar, but only with the previous versions. For example, the store hardly ever looks at the old photos of its products again, so with this rule it moves them to a cheaper class after a while and later deletes them, while the current photos stay where they are. Create the rule with these steps:
- In your bucket, choose the Management tab and then Create lifecycle rule.
- Rule name:
versiones-viejas-a-standard-ia. - Choose Apply to all objects in the bucket and check I acknowledge that this rule will apply to all objects in the bucket.
- In Lifecycle rule actions, check Transition noncurrent versions of objects between storage classes and Permanently delete noncurrent versions of objects.
- In the transition, choose S3 Standard-IA and 30 days after the object becomes noncurrent.
- In the permanent deletion, enter 60 days.
- Choose Create rule.
The rule applies to the objects already in the bucket and to the ones you upload later. Even so, your previous version doesn't move today, because it first has to spend 30 days as noncurrent, and it is deleted when it reaches 60.
When does it happen?
Today you uploaded the second version of your file, so the first one became noncurrent. With your rule, what happens to that previous version?
The rule uses Standard-IA because of a restriction in the new experience. In an account of that type on the free plan, the AWS policy denies uploading objects with the GLACIER or DEEP_ARCHIVE class, and S3 Glacier Deep Archive only exists on the paid plan. For example, if in an account like that you choose Glacier Deep Archive in the rule, AWS doesn't let you move your old versions there.
Extra: publish it as a static website
A static website is a set of pages that look the same for everyone, so S3 only has to deliver the files just as they are. For example, the store's welcome page, with its hours and its address, can be a static website, while the shopping cart can't, because it changes for each customer.
Publishing the bucket as a static website makes everything in it public, so use a bucket only for the website and don't upload anything private. If you want to do it, follow these steps:
- Upload your
index.html. - In Properties, in Static website hosting, choose Edit and then Enable. Enter
index.htmlin Index document and choose Save changes. The index document is the page S3 shows when someone opens the website's address without asking for a specific file. - In Permissions, in Block public access (bucket settings), choose Edit, turn off Block all public access and choose Save changes. If your full account has account-level block public access turned on, you will see a note here, and the website won't open until you change it. That block protects all your buckets, so think it over before you remove it.
- In Permissions, in Bucket policy, choose Edit, paste this policy with your bucket name and choose Save changes. A bucket policy is a text with rules about who can do what with the bucket, and this one says that anyone (
"Principal": "*") can read (s3:GetObject) the files in the bucket, but not change or delete them:
json
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "LecturaPublicaDelSitio",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::NOMBRE-DE-TU-BUCKET/*"
}
]
}
- Go back to Properties and open the Bucket website endpoint that appears in Static website hosting, which is your website's address. In us-east-2 it has the form
http://NOMBRE.s3-website.us-east-2.amazonaws.com.
In the new experience you can also publish the website, because its documentation gives this case, a public bucket for a simple static website, as an example of the public access you can configure.
CloudFormation option
CloudFormation is the AWS service that creates resources from a text file called a template. The [lab06-s3-sitio.yaml](/roadmaps/plantillas/lab06-s3-sitio.yaml) template creates a bucket with versioning and the same lifecycle rule, which moves the previous versions to Standard-IA after 30 days and deletes them after 60. It also leaves the static website enabled with index.html and the public read-only policy, although ACLs stay blocked. Everything it creates carries the tag cloudpath:lab = lab06, and you use it with these steps:
- In the CloudFormation console, choose Create stack and With new resources (standard).
- Choose Upload a template file, upload
lab06-s3-sitio.yamland choose Next. - Stack name:
cloudpath-lab06-sitio. Choose Next twice and then Submit. - In Outputs you will find
NombreDelBucket, the bucket where you upload yourindex.html, andUrlDelSitio.
How to know you are done
In the console you have to see the bucket with two versions of your file and the rule on the Management tab. You can also check it from CloudShell, a window inside the console where you type commands, with these commands, using your bucket name:
BUCKET=NOMBRE-DE-TU-BUCKET
aws s3api get-bucket-versioning --bucket "$BUCKET"
aws s3api list-object-versions --bucket "$BUCKET" --query "Versions[].[Key,VersionId,IsLatest]" --output table
aws s3api get-bucket-lifecycle-configuration --bucket "$BUCKET"
The first command has to say "Status": "Enabled", which means versioning is turned on. The second one shows the two versions of your file, one of them with True, which is the current version, and the third one shows your rule with STANDARD_IA. If you did the website, check it with these other two:
aws s3api get-bucket-website --bucket "$BUCKET"
curl -s "http://$BUCKET.s3-website.us-east-2.amazonaws.com"
The second of those commands requests your page just like a browser does and shows you its text, so your line of HTML has to appear. Change us-east-2 if your bucket is in another Region. Some Regions use a hyphen instead of a dot (s3-website-us-east-1), and the exact URL for yours is in Properties.
Delete what you created
A bucket with versioning keeps every version of its objects, and S3 doesn't let you delete it until you empty it. For example, even if you delete your file from the list, its previous versions are still stored and the bucket still isn't empty. That is why deleting it takes two steps:
- In the list of buckets, choose yours and then Empty. Type
permanently deleteto confirm and choose Empty. This deletes every version and can't be undone. - Choose the bucket again and then Delete. Type its name and choose Delete bucket.
If you used the template, empty the bucket first and then delete the stack cloudpath-lab06-sitio, because CloudFormation can't delete a bucket that has objects.
True or false?
0 of 3You can delete a bucket with versioning without emptying it first.
Emptying the bucket deletes every version and can't be undone.
CloudFormation can delete the stack even if the bucket has objects.
Next
- Getting started with Amazon S3: https://docs.aws.amazon.com/AmazonS3/latest/userguide/GetStartedWithS3.html
- cloud path practice exams: /examenes?lang=en