What you will learn
- Create s3 bucket using Terraform
- Enable s3 versioning using terraform
- Set s3 lifecycle policy using terraform
- Destroy s3 using terraform
Create s3 bucket using Terraform
We need to define our terraform provider as
AWS first to get started
Let's define terraform resource to create s3 bucket
Note: I set ACL to
privateso this bucket is not accessible from the internet. If you need your bucket to public accessible set
acl = "public-read".
Enable s3 versioning using terraform
When we enable versioning in s3 bucket, when ever the file is updated it will have move the current version as the
Set s3 lifecycle policy using terraform
When you have files which is not accessed after 15 days, it can be either moved to low cost storage or it can be removed from the storage. This can be done with the help of lifecycle policy.
Set s3 expiration to 15 days
Note: To learn more about lifecycle configuration, visit terraform aws docs
This will remove all the non current files from the storage after 15 days from it's creation date.
Note: for complex rules you can visit AWS s3 docs
Destroy s3 using terraform
This will remove all the created resource
We have learned how to setup s3 bucket with terraform and enabling versioning with lifecycle management.
Learn how to manage terraform state with s3