This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 15k traffic Daily!!!

Basics of AWS Tags & Terraform with S3 – Part 1


Managing AWS resources could be a particularly arduous course of. AWS would not have logical useful resource teams and different niceties that Azure and GCP have. This nonwithstanding, AWS remains to be far and away the most well-liked cloud supplier on this planet. Subsequently, it is nonetheless essential to seek out methods to prepare your assets successfully.

One of the crucial necessary methods to prepare and filter your assets is by utilizing AWS tags. Whereas tagging is usually a tedious course of, Terraform may also help ease the ache by offering several ways to tag your AWS assets. On this weblog and accompanying video sequence, we’re going to check out varied strategies and methods to tag your assets and preserve them organized effectively.

These posts are written so as to observe alongside. You’ll simply want an setting that has entry to the AWS API in your area. I sometimes use AWS Cloud9 for this function, however any setting with entry will do.


Github repo: https://github.com/CloudForecast/aws-tagging-with-terraform



Tag Blocks

The primary technique we are able to use to tag assets is by utilizing a fundamental tag block. Let’s create a primary.tf file and configure an S3 bucket to check out this.



Configure Terraform to make use of the AWS supplier

terraform {
  required_providers {
    aws = {
      supply  = "hashicorp/aws"
      model = "~> 4.0"
    }
  }
}
Enter fullscreen mode

Exit fullscreen mode



Configure the AWS Supplier

supplier "aws" {
  area = "us-west-2"
}
Enter fullscreen mode

Exit fullscreen mode



Create a random ID to forestall bucket identify clashes

useful resource "random_id" "s3_id" {
    byte_length = 2
}
Enter fullscreen mode

Exit fullscreen mode

We make the most of the random_id perform:
https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/id
to create the entropy wanted in our bucket names to make sure we don’t overlap with the identify of one other S3 bucket.



Create an S3 Bucket w/ Terraform and Tag It

useful resource "aws_s3_bucket" "devops_bucket" {
  bucket = "devops-bucket-${random_id.s3_id.dec}"

  tags = {
      Env = "dev"
      Service = "s3"
      Staff = "devops"
  }
}
Enter fullscreen mode

Exit fullscreen mode

Now, let’s run terraform apply -auto-approve.

As soon as the apply is completed, let’s run terraform console after which run aws_s3_bucket.devops_bucket.tags to confirm the tags:

> aws_s3_bucket.devops_bucket.tags
tomap({
  "Env" = "dev"
  "Service" = "s3"
  "Staff" = "devops"
})
Enter fullscreen mode

Exit fullscreen mode

To exit the console, run exit or ctrl+c. You can even simply run terraform state present aws_s3_bucket.devops_bucket.tags, terraform present, or simply scroll up via the output to see the tags.

As you possibly can see, AWS tags could be specified on AWS assets by using a tags block inside a useful resource. This can be a easy approach to make sure every s3 bucket has tags, however it’s by no means environment friendly. Tagging each useful resource in AWS like this isn’t solely tedious and the exact opposite of the DRY (Do not Repeat Your self) precept, nevertheless it’s additionally avoidable to an extent!



Default AWS Tags & Terraform

With a purpose to specify deployment-wide tags, you possibly can specify a default_tags block inside the supplier block. This may will let you specify fallback tags for any useful resource that has no tags outlined. If, nevertheless, you do specify tags on a selected useful resource, these tags will take priority. Let’s have a look:



Utilizing Terraform to Create a Second S3 bucket

useful resource "aws_s3_bucket" "finance_bucket" {
  bucket = "cloudforecast-finance-${random_id.s3_id.dec)"

  tags = {
    Env = "dev"
    Service = "s3"
    Staff = "finance"
  }
}
Enter fullscreen mode

Exit fullscreen mode

After you have added the second bucket definition and saved the file, go forward and apply the configuration with terraform apply -auto-approve.
After you have utilized, you possibly can run terraform console and entry each buckets by their useful resource identify:

> aws_s3_bucket.devops_bucket.tags
tomap({
  "Env" = "dev"
  "Service" = "s3"
  "Staff" = "devops"
})
> aws_s3_bucket.finance_bucket.tags
tomap({
  "Env" = "dev"
  "Service" = "s3"
  "Staff" = "finance"
})
Enter fullscreen mode

Exit fullscreen mode

If we have been to deploy 10s, 100s, and even 1000s of assets, this is able to not be very environment friendly. Let’s add default tags to make this extra environment friendly:



Add Default AWS Tags w/ Terraform

Inside the supplier block of our configuration, add the default tag as a way to assign each assets the Env tag:

supplier "aws" {
  area = "us-west-2"
    default_tags {
      tags = {
          Env = "dev"
    }
  }
}
Enter fullscreen mode

Exit fullscreen mode



Take away Env tags w/ Terraform

Now that we have added the default tags, let’s take away the Env tag from the AWS S3 buckets:

useful resource "aws_s3_bucket" "devops_bucket" {
    bucket = "devops-bucket-${random_id.s3_id.dec}"

    tags = {
        Service = "s3"
        Staff = "devops"
    }
}

useful resource "aws_s3_bucket" "finance_bucket" {
    bucket = "finance-bucket-${random_id.s3_id.dec}"

    tags = {
        Service = "s3"
        Staff = "finance"
    }
}
Enter fullscreen mode

Exit fullscreen mode

Run terraform apply -auto-approve once more and, as soon as it is completed deploying,
run terraform console. Inside the console, sort the useful resource deal with of every S3 bucket and examine the output:

> aws_s3_bucket.devops_bucket.tags
tomap({
  "Service" = "s3"
  "Staff" = "devops"
})
> aws_s3_bucket.finance_bucket.tags
tomap({
  "Service" = "s3"
  "Staff" = "finance"
})
Enter fullscreen mode

Exit fullscreen mode

Do you discover one thing lacking? Default tags aren’t displayed inside the tags attribute. Default tags are discovered inside the tags_all attribute, so re-run the earlier instructions with tags_all changing tags:

> aws_s3_bucket.devops_bucket.tags_all
tomap({
  "Env" = "dev"
  "Service" = "s3"
  "Staff" = "devops"
})
> aws_s3_bucket.finance_bucket.tags_all
tomap({
  "Env" = "dev"
  "Service" = "s3"
  "Staff" = "finance"
})
Enter fullscreen mode

Exit fullscreen mode

There they’re! Preserve this in thoughts. If you’re querying the state to carry out actions based mostly on tags, it would be best to use the tags_all attribute as an alternative of simply tags by themselves.



Tag Priority

Now, for one final fast check to see the tag priority in motion, let’s add the Env tag again to our finance bucket, however outline it as prod as an alternative of dev:

useful resource "aws_s3_bucket" "finance_bucket" {
  bucket = "finance-bucket-${random_id.s3_id.dec}"

  tags = {
    Env = "prod"
    Service = "s3"
    Staff    = "finance"
  }
}
Enter fullscreen mode

Exit fullscreen mode

Run terraform apply -auto-approve once more:

  # aws_s3_bucket.finance_bucket will likely be up to date in-place
  ~ useful resource "aws_s3_bucket" "finance_bucket" {
        id                                   = "finance-bucket-52680"
      ~ tags                                 = {
          + "Env"     = "prod"
            # (2 unchanged parts hidden)
        }
      ~ tags_all                             = {
          ~ "Env"     = "dev" -> "prod"
            # (2 unchanged parts hidden)
        }
        # (17 unchanged attributes hidden)
    }
Enter fullscreen mode

Exit fullscreen mode

Discover the modifications made, then run terraform console:

> aws_s3_bucket.finance_bucket.tags_all
tomap({
  "Env" = "prod"
  "Service" = "s3"
  "Staff" = "finance"
})
Enter fullscreen mode

Exit fullscreen mode

Discover the Env tag has now been modified to prod, our up to date worth, overriding the default tags.



Destroy Assets

Now, when you’re prepared, go forward and destroy your assets!

terraform destroy -auto-approve



Conclusion

Alright, so now that we’ve got an concept of tips on how to assign customized tags and default tags, be part of me on the subsequent half on this sequence the place we dive deeper!

The Article was Inspired from tech community site.
Contact us if this is inspired from your article and we will give you credit for it for serving the community.

This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 10k Tech related traffic daily !!!

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?