Dynamic Lifecycle Rules for S3 Bucket Using Terraform

--

Problem Space :

Terraform is really a cool tool , if it is used wisely , let me present one use case where if the tool is not used wisely , we may have to spend more time in maintenance. Lets say an org has around 10 to 20 s3 bucket resources to be terraformed , terraform allows you to define each resource , however if we can use a pattern to make use of loops concept in terraform , it makes our code maintenance way better .

Requirement :

Creating a number of s3 buckets with some set of configurations for Lifecyle rules.

Solving the problem without using loops :

Above is an example where you have define the resource for n such items , and the code would be mostly repetitive , except the configurations. Now lets take a look how we can solve the above pattern using Loops.

Solving the problem using loops :

  1. Define Local Variables for Custom Configurations.

2. Making use of Loop for each to iterate over the list of buckets.

3. The tricky part comes here , as we know Lifecycle rules defined in a normal bucket terraform resource , has to be repeated with the number of rules available, and the dynamic functionality comes for a lending hand.

Lets take a deep dive on dynamic content

Since we have defined lifecycle as an in array in local variable , the second for each loop traverses through all the available rules in the list , there could be cases where a bucket may not have lifecycle rule for those , we can eliminate using the following line. As we already know if we don’t have a lifecycle rule , terraform expects the block of code to be empty.

for_each = length(each.value.lifecycle_rules) > 0 ? each.value.lifecycle_rules : []

If there is enough list to traverse it will proceed or else we can default to empty list , I know this is a simple logic , however terraform has modified usage of these concepts which sometimes confuses.

Reference : https://www.terraform.io/docs/language/expressions/dynamic-blocks.html

Conclusion :

The easiest way to define Lifecycle rule is on the UI , however, if the same rule has to be applied across many prefixes / bucket , terraform comes in handy with a dynamic content and for loop to solve the problem, however, sometimes we end up of manually entering the lifecycle rule which the above blog helps to make the maintenance on terraform easier.

Terraform Version : ≥ 0.12.6

Join FAUN: Website 💻|Podcast 🎙️|Twitter 🐦|Facebook 👥|Instagram 📷|Facebook Group 🗣️|Linkedin Group 💬| Slack 📱|Cloud Native News 📰|More.

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇

--

--