0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Terraform】default_tagsを使ってリソースのタグ付け運用を楽にしよう

Posted at

はじめに

default_tagsというTerraformの機能を使って、AWSリソースのタグ付けを簡素化する方法について紹介します。

Terraformでのタグ付けの基本

AWSの多くのリソースは tags ブロックでタグを指定できます。
例えば以下のように記述することで、S3バケットにタグを付けることができます。

resource "aws_s3_bucket" "example" {
  bucket = "eureka-example-bucket-20250830"

  tags = {
    Environment = "dev"
    DeployedBy  = "terraform"
    Owner       = "eureka"
  }
}

上記の例で言うと、EnvironmentDeployedByなどのタグは多くのリソースで共通して使われることが多いです。
ただ、リソースごとに同様の記述を繰り返すのは少し面倒だし、ヒューマンエラーの原因にもなりかねません。

default_tagsでリソースのタグ付けを簡素化する

TerraformのAWSプロバイダーには default_tags という機能があり、これを使うといちいちリソースごとにタグを指定しなくても、共通のタグを自動的に付与できます。

provider "aws" {
  region = "ap-northeast-1"

  default_tags {
    tags = {
      Environment = "dev"
      DeployedBy  = "terraform"
    }
  }
}

resource "aws_s3_bucket" "example" {
  bucket = "eureka-example-bucket-20250830"

  tags = {
    Owner = "eureka"
  }
}

resource "aws_s3_bucket" "example2" {
  bucket = "eureka-example-bucket-20250830-2"

  tags = {
    Owner = "eureka2"
  }
}

この設定を行うことで、作成するすべてのAWSリソースには default_tagsで指定したタグが自動的に付与されるようになります。リソース固有のタグだけを追加で指定すればOKです。

  • eureka-example-bucket-20250830
    image.png

  • eureka-example-bucket-20250830-2
    image.png

ちゃんと両方のバケットに EnvironmentDeployedBy のタグが付与されていますね。

個別に指定したタグが default_tags と同じキーを持つ場合、個別に指定したタグが優先されます

まとめ

default_tags を使うと共通タグを一括で設定でき、タグ付け運用が楽になります。
タグ付けはコスト管理や運用効率に直結するので、Terraformを使っている場合はぜひ活用してみてください。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?