<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Paul Kamau</title>
    <description>The latest articles on DEV Community by Paul Kamau (@paulycloud).</description>
    <link>https://dev.to/paulycloud</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F351405%2Fe77a060b-56d8-4f8f-9880-dc17b12444a9.jpg</url>
      <title>DEV Community: Paul Kamau</title>
      <link>https://dev.to/paulycloud</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/paulycloud"/>
    <language>en</language>
    <item>
      <title>How to create multiple AWS s3 buckets with a single resource config on Terraforrm</title>
      <dc:creator>Paul Kamau</dc:creator>
      <pubDate>Mon, 16 Mar 2020 15:39:34 +0000</pubDate>
      <link>https://dev.to/paulycloud/how-to-create-multiple-aws-s3-buckets-with-a-single-resource-config-on-terraforrm-4d28</link>
      <guid>https://dev.to/paulycloud/how-to-create-multiple-aws-s3-buckets-with-a-single-resource-config-on-terraforrm-4d28</guid>
      <description>&lt;p&gt;When building and managing applications, having repeatable and automated processes can enable you to focus on productive and exiting workflows thereby making the entire experience a pleasure to go through.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Setting up your Provider and variable
Set your AWS Access Key and Secret Key in the Provider Reference.
Note: Version 0.12+ of Terraform has a simpler Interpolation syntax for variable references&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UToAjQ5n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/700/1%2ApjSiAtbe-5_y1l2E5frZEg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UToAjQ5n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/700/1%2ApjSiAtbe-5_y1l2E5frZEg.png" alt="image-1" width="700" height="290"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a single variable for the "s3_bucket_name"
Your "s3_bucket_name" variable will contain an array of all the bucket names you want. This array is passed in the default key.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--H1G-U8nn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/700/1%2Ake_UFiGPrILT0Sexv0O4uQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--H1G-U8nn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/700/1%2Ake_UFiGPrILT0Sexv0O4uQ.png" alt="image-2" width="700" height="362"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a single resource for "aws_s3_bucket"
We only need 1 resource tag that will iterate through the bucket names in the variable.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The "count" attribute calculates the buckets required from the "s3_bucket_names" variable you created.&lt;br&gt;
The "bucket" attribute will contain the name of the bucket from the array in the "s3_bucket_names" variable.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eGPclqPO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/1%2A5sc0TT9kHRg0og_3P8x3jg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eGPclqPO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/1%2A5sc0TT9kHRg0og_3P8x3jg.png" alt="image-2" width="800" height="385"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Complete Code&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Consequently, this abstracted method of resource building has reduced hundreds of lines of code and made the entire HCL readable and understandable.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>saas</category>
      <category>terraform</category>
      <category>s3</category>
    </item>
  </channel>
</rss>
