Terraform tips: How to create multiple AWS s3 buckets with a single resource config

Mine. VSCode my 4k laptop :)

Intro

I run about 10 SaaS apps and 2 Serverless sites that require an S3 bucket for storage. These 12 s3 buckets have the same lifecycle rules and bucket configs that I managed manually for a couple of years.

Creating multiple S3 buckets with Terraform should be a really simple thing if you don’t mind unstructured and unmanageable code. Just set your “provider” configs and create a “resource” tag for each of your buckets. So if you have 12 s3 buckets, you’d need 12 resource tags that do the exact same thing. This method is a dirty way to write your HCL code and would actually result in more work than actually managing these directly.

So as part of the Cloud automation plan that I've been developing for Rugged I/O, I’ve begun with my s3 bucket automation.

1. Setting up your Provider and variable

Note: Version 0.12+ of Terraform has a simpler Interpolation syntax for variable references

provider reference build

2. Create a single variable for the “s3_bucket_name”

Variable with all the bucket names you need

3. Create a single resource for “aws_s3_bucket”

The “bucket” attribute will contain the name of the bucket from the array in the “s3_bucket_names” variable.

single aws_s3_bucket resource tag

The Complete Code

The complete code

Variable File

This technique can be used for any reusable resources, not just s3 buckets, through a thoughtful combination of a simple variable assignment, resource count and naming prefixes.

Consequently, this abstracted method of resource building has reduced hundreds of lines of code and made the entire HCL readable and understandable.

Thank you!!

--

--

TAM @google | I write about tips, tutorials and impact AI & Machine Learning | https://bio.link/paulkamau

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Paul Kamau

TAM @google | I write about tips, tutorials and impact AI & Machine Learning | https://bio.link/paulkamau