[{"data":1,"prerenderedAt":708},["ShallowReactive",2],{"post-terraform-workspace-management":3},{"id":4,"title":5,"body":6,"date":698,"description":699,"extension":700,"meta":701,"navigation":118,"path":704,"seo":705,"stem":706,"__hash__":707},"blog\u002Fblog\u002Fterraform-workspace-management.md","Terraform workspaceで個人ごとのAWSリソースを効率管理",{"type":7,"value":8,"toc":678},"minimark",[9,13,17,20,23,26,37,40,44,68,72,151,154,157,220,227,231,234,237,267,346,349,352,367,409,493,496,499,545,548,578,581,588,626,629,654,657,660,671,674],[10,11,12],"h2",{"id":12},"はじめに",[14,15,16],"p",{},"Terraformのworkspace機能を使うと、同じ定義ファイルで複数の独立した環境を管理できます。",[14,18,19],{},"個人ごとに定義ファイルを分ける運用は、1〜2名なら問題ありませんが、10名規模になると管理が煩雑になります。workspaceを使えば、この問題を解決できます。",[10,21,22],{"id":22},"workspace機能とは",[14,24,25],{},"workspaceは、同じTerraform定義で異なる状態ファイル（tfstate）を管理する機能です。各workspaceは独立したリソースセットを持つため、個人開発環境の分離に最適です。",[14,27,28,29,36],{},"詳細は",[30,31,35],"a",{"href":32,"rel":33},"https:\u002F\u002Fdeveloper.hashicorp.com\u002Fterraform\u002Flanguage\u002Fstate\u002Fworkspaces",[34],"nofollow","公式ドキュメント","を参照してください。",[10,38,39],{"id":39},"環境構築",[41,42,43],"h3",{"id":43},"前提条件",[45,46,47,51,60],"ul",{},[48,49,50],"li",{},"AWS CLIがインストール済み",[48,52,53,54,59],{},"IAMユーザーが作成済み（",[30,55,58],{"href":56,"rel":57},"https:\u002F\u002Fdocs.aws.amazon.com\u002Fja_jp\u002FIAM\u002Flatest\u002FUserGuide\u002Fid_users_create.html",[34],"IAMユーザー作成手順","）",[48,61,62,63,59],{},"Terraformがインストール済み（tfenv推奨: ",[30,64,67],{"href":65,"rel":66},"https:\u002F\u002Fgithub.com\u002Ftfutils\u002Ftfenv",[34],"tfenv",[41,69,71],{"id":70},"aws-cliの設定","AWS CLIの設定",[73,74,79],"pre",{"className":75,"code":76,"language":77,"meta":78,"style":78},"language-shell shiki shiki-themes github-light github-dark","aws configure --profile \u003Cprofile名>\n\n# 以下を入力\n# AWS Access Key ID: アクセスキー ID\n# AWS Secret Access Key: シークレットアクセスキー\n# Default region name: ap-northeast-1\n# Default output format: json\n","shell","",[80,81,82,113,120,127,133,139,145],"code",{"__ignoreMap":78},[83,84,87,91,95,99,103,106,110],"span",{"class":85,"line":86},"line",1,[83,88,90],{"class":89},"sScJk","aws",[83,92,94],{"class":93},"sZZnC"," configure",[83,96,98],{"class":97},"sj4cs"," --profile",[83,100,102],{"class":101},"szBVR"," \u003C",[83,104,105],{"class":93},"profile",[83,107,109],{"class":108},"sVt8B","名",[83,111,112],{"class":101},">\n",[83,114,116],{"class":85,"line":115},2,[83,117,119],{"emptyLinePlaceholder":118},true,"\n",[83,121,123],{"class":85,"line":122},3,[83,124,126],{"class":125},"sJ8bj","# 以下を入力\n",[83,128,130],{"class":85,"line":129},4,[83,131,132],{"class":125},"# AWS Access Key ID: アクセスキー ID\n",[83,134,136],{"class":85,"line":135},5,[83,137,138],{"class":125},"# AWS Secret Access Key: シークレットアクセスキー\n",[83,140,142],{"class":85,"line":141},6,[83,143,144],{"class":125},"# Default region name: ap-northeast-1\n",[83,146,148],{"class":85,"line":147},7,[83,149,150],{"class":125},"# Default output format: json\n",[10,152,153],{"id":153},"workspaceの作成",[14,155,156],{},"新しいworkspaceを作成します。",[73,158,160],{"className":75,"code":159,"language":77,"meta":78,"style":78},"terraform workspace new test\n\n# 出力例\n# Created and switched to workspace \"test\"!\n\n# workspace一覧を確認\nterraform workspace list\n#   default\n# * test\n",[80,161,162,176,180,185,190,194,199,208,214],{"__ignoreMap":78},[83,163,164,167,170,173],{"class":85,"line":86},[83,165,166],{"class":89},"terraform",[83,168,169],{"class":93}," workspace",[83,171,172],{"class":93}," new",[83,174,175],{"class":93}," test\n",[83,177,178],{"class":85,"line":115},[83,179,119],{"emptyLinePlaceholder":118},[83,181,182],{"class":85,"line":122},[83,183,184],{"class":125},"# 出力例\n",[83,186,187],{"class":85,"line":129},[83,188,189],{"class":125},"# Created and switched to workspace \"test\"!\n",[83,191,192],{"class":85,"line":135},[83,193,119],{"emptyLinePlaceholder":118},[83,195,196],{"class":85,"line":141},[83,197,198],{"class":125},"# workspace一覧を確認\n",[83,200,201,203,205],{"class":85,"line":147},[83,202,166],{"class":89},[83,204,169],{"class":93},[83,206,207],{"class":93}," list\n",[83,209,211],{"class":85,"line":210},8,[83,212,213],{"class":125},"#   default\n",[83,215,217],{"class":85,"line":216},9,[83,218,219],{"class":125},"# * test\n",[14,221,222,223,36],{},"詳細コマンドは",[30,224,35],{"href":225,"rel":226},"https:\u002F\u002Fdeveloper.hashicorp.com\u002Fterraform\u002Fcli\u002Fcommands\u002Fworkspace",[34],[10,228,230],{"id":229},"terraform定義の作成","Terraform定義の作成",[41,232,233],{"id":233},"プロバイダー設定",[14,235,236],{},"作業ディレクトリを作成し、AWSプロバイダーを設定します。",[73,238,240],{"className":75,"code":239,"language":77,"meta":78,"style":78},"mkdir terraform && cd terraform\ntouch main.tf\n",[80,241,242,259],{"__ignoreMap":78},[83,243,244,247,250,253,256],{"class":85,"line":86},[83,245,246],{"class":89},"mkdir",[83,248,249],{"class":93}," terraform",[83,251,252],{"class":108}," && ",[83,254,255],{"class":97},"cd",[83,257,258],{"class":93}," terraform\n",[83,260,261,264],{"class":85,"line":115},[83,262,263],{"class":89},"touch",[83,265,266],{"class":93}," main.tf\n",[73,268,271],{"className":269,"code":270,"language":166,"meta":78,"style":78},"language-terraform shiki shiki-themes github-light github-dark","# main.tf\nterraform {\n  required_providers {\n    aws = {\n      source  = \"hashicorp\u002Faws\"\n      version = \"~> 4.0\"\n    }\n  }\n}\n\nprovider \"aws\" {\n  region  = \"ap-northeast-1\"\n  profile = \"default\"\n}\n",[80,272,273,278,283,288,293,298,303,308,313,318,323,329,335,341],{"__ignoreMap":78},[83,274,275],{"class":85,"line":86},[83,276,277],{},"# main.tf\n",[83,279,280],{"class":85,"line":115},[83,281,282],{},"terraform {\n",[83,284,285],{"class":85,"line":122},[83,286,287],{},"  required_providers {\n",[83,289,290],{"class":85,"line":129},[83,291,292],{},"    aws = {\n",[83,294,295],{"class":85,"line":135},[83,296,297],{},"      source  = \"hashicorp\u002Faws\"\n",[83,299,300],{"class":85,"line":141},[83,301,302],{},"      version = \"~> 4.0\"\n",[83,304,305],{"class":85,"line":147},[83,306,307],{},"    }\n",[83,309,310],{"class":85,"line":210},[83,311,312],{},"  }\n",[83,314,315],{"class":85,"line":216},[83,316,317],{},"}\n",[83,319,321],{"class":85,"line":320},10,[83,322,119],{"emptyLinePlaceholder":118},[83,324,326],{"class":85,"line":325},11,[83,327,328],{},"provider \"aws\" {\n",[83,330,332],{"class":85,"line":331},12,[83,333,334],{},"  region  = \"ap-northeast-1\"\n",[83,336,338],{"class":85,"line":337},13,[83,339,340],{},"  profile = \"default\"\n",[83,342,344],{"class":85,"line":343},14,[83,345,317],{},[41,347,348],{"id":348},"リソース定義",[14,350,351],{},"VPCと個人ごとのALB target groupを作成します。",[73,353,355],{"className":75,"code":354,"language":77,"meta":78,"style":78},"touch vpc.tf alb.tf\n",[80,356,357],{"__ignoreMap":78},[83,358,359,361,364],{"class":85,"line":86},[83,360,263],{"class":89},[83,362,363],{"class":93}," vpc.tf",[83,365,366],{"class":93}," alb.tf\n",[73,368,370],{"className":269,"code":369,"language":166,"meta":78,"style":78},"# vpc.tf\nresource \"aws_vpc\" \"test\" {\n  cidr_block = \"10.0.0.0\u002F16\"\n\n  tags = {\n    Name = \"test\"\n  }\n}\n",[80,371,372,377,382,387,391,396,401,405],{"__ignoreMap":78},[83,373,374],{"class":85,"line":86},[83,375,376],{},"# vpc.tf\n",[83,378,379],{"class":85,"line":115},[83,380,381],{},"resource \"aws_vpc\" \"test\" {\n",[83,383,384],{"class":85,"line":122},[83,385,386],{},"  cidr_block = \"10.0.0.0\u002F16\"\n",[83,388,389],{"class":85,"line":129},[83,390,119],{"emptyLinePlaceholder":118},[83,392,393],{"class":85,"line":135},[83,394,395],{},"  tags = {\n",[83,397,398],{"class":85,"line":141},[83,399,400],{},"    Name = \"test\"\n",[83,402,403],{"class":85,"line":147},[83,404,312],{},[83,406,407],{"class":85,"line":210},[83,408,317],{},[73,410,412],{"className":269,"code":411,"language":166,"meta":78,"style":78},"# alb.tf\nvariable \"personal_name_prefix\" {\n  type        = string\n  description = \u003C\u003C-EOF\n    個人環境ごとのPrefixを指定してください。\n    指定方法：[firstname]-[lastname]\n    注意：ALBの名前は32文字以内\n  EOF\n}\n\nresource \"aws_alb_target_group\" \"test\" {\n  name     = var.personal_name_prefix\n  port     = 80\n  protocol = \"HTTP\"\n  vpc_id   = aws_vpc.test.id\n}\n",[80,413,414,419,424,429,434,439,444,449,454,458,462,467,472,477,482,488],{"__ignoreMap":78},[83,415,416],{"class":85,"line":86},[83,417,418],{},"# alb.tf\n",[83,420,421],{"class":85,"line":115},[83,422,423],{},"variable \"personal_name_prefix\" {\n",[83,425,426],{"class":85,"line":122},[83,427,428],{},"  type        = string\n",[83,430,431],{"class":85,"line":129},[83,432,433],{},"  description = \u003C\u003C-EOF\n",[83,435,436],{"class":85,"line":135},[83,437,438],{},"    個人環境ごとのPrefixを指定してください。\n",[83,440,441],{"class":85,"line":141},[83,442,443],{},"    指定方法：[firstname]-[lastname]\n",[83,445,446],{"class":85,"line":147},[83,447,448],{},"    注意：ALBの名前は32文字以内\n",[83,450,451],{"class":85,"line":210},[83,452,453],{},"  EOF\n",[83,455,456],{"class":85,"line":216},[83,457,317],{},[83,459,460],{"class":85,"line":320},[83,461,119],{"emptyLinePlaceholder":118},[83,463,464],{"class":85,"line":325},[83,465,466],{},"resource \"aws_alb_target_group\" \"test\" {\n",[83,468,469],{"class":85,"line":331},[83,470,471],{},"  name     = var.personal_name_prefix\n",[83,473,474],{"class":85,"line":337},[83,475,476],{},"  port     = 80\n",[83,478,479],{"class":85,"line":343},[83,480,481],{},"  protocol = \"HTTP\"\n",[83,483,485],{"class":85,"line":484},15,[83,486,487],{},"  vpc_id   = aws_vpc.test.id\n",[83,489,491],{"class":85,"line":490},16,[83,492,317],{},[10,494,495],{"id":495},"実行",[41,497,498],{"id":498},"実行計画の確認",[73,500,502],{"className":75,"code":501,"language":77,"meta":78,"style":78},"terraform plan -var personal_name_prefix=test\n\n# 出力例\n# Terraform will perform the following actions:\n#   # aws_alb_target_group.test will be created\n#   # aws_vpc.test will be created\n# Plan: 2 to add, 0 to change, 0 to destroy.\n",[80,503,504,517,521,525,530,535,540],{"__ignoreMap":78},[83,505,506,508,511,514],{"class":85,"line":86},[83,507,166],{"class":89},[83,509,510],{"class":93}," plan",[83,512,513],{"class":97}," -var",[83,515,516],{"class":93}," personal_name_prefix=test\n",[83,518,519],{"class":85,"line":115},[83,520,119],{"emptyLinePlaceholder":118},[83,522,523],{"class":85,"line":122},[83,524,184],{"class":125},[83,526,527],{"class":85,"line":129},[83,528,529],{"class":125},"# Terraform will perform the following actions:\n",[83,531,532],{"class":85,"line":135},[83,533,534],{"class":125},"#   # aws_alb_target_group.test will be created\n",[83,536,537],{"class":85,"line":141},[83,538,539],{"class":125},"#   # aws_vpc.test will be created\n",[83,541,542],{"class":85,"line":147},[83,543,544],{"class":125},"# Plan: 2 to add, 0 to change, 0 to destroy.\n",[41,546,547],{"id":547},"リソースの作成",[73,549,551],{"className":75,"code":550,"language":77,"meta":78,"style":78},"terraform apply -var personal_name_prefix=test\n\n# 確認後、yesを入力\n# Apply complete! Resources: 2 added, 0 changed, 0 destroyed.\n",[80,552,553,564,568,573],{"__ignoreMap":78},[83,554,555,557,560,562],{"class":85,"line":86},[83,556,166],{"class":89},[83,558,559],{"class":93}," apply",[83,561,513],{"class":97},[83,563,516],{"class":93},[83,565,566],{"class":85,"line":115},[83,567,119],{"emptyLinePlaceholder":118},[83,569,570],{"class":85,"line":122},[83,571,572],{"class":125},"# 確認後、yesを入力\n",[83,574,575],{"class":85,"line":129},[83,576,577],{"class":125},"# Apply complete! Resources: 2 added, 0 changed, 0 destroyed.\n",[41,579,580],{"id":580},"結果確認",[14,582,583,584,587],{},"各workspaceの状態ファイルは ",[80,585,586],{},"terraform.tfstate.d"," ディレクトリに分離されます。",[73,589,591],{"className":75,"code":590,"language":77,"meta":78,"style":78},"terraform workspace list\n#   default\n# * test\n\nls terraform.tfstate.d\u002F\n# test\n",[80,592,593,601,605,609,613,621],{"__ignoreMap":78},[83,594,595,597,599],{"class":85,"line":86},[83,596,166],{"class":89},[83,598,169],{"class":93},[83,600,207],{"class":93},[83,602,603],{"class":85,"line":115},[83,604,213],{"class":125},[83,606,607],{"class":85,"line":122},[83,608,219],{"class":125},[83,610,611],{"class":85,"line":129},[83,612,119],{"emptyLinePlaceholder":118},[83,614,615,618],{"class":85,"line":135},[83,616,617],{"class":89},"ls",[83,619,620],{"class":93}," terraform.tfstate.d\u002F\n",[83,622,623],{"class":85,"line":141},[83,624,625],{"class":125},"# test\n",[41,627,628],{"id":628},"リソースの削除",[73,630,632],{"className":75,"code":631,"language":77,"meta":78,"style":78},"terraform destroy -var personal_name_prefix=test\n\n# Destroy complete! Resources: 1 destroyed.\n",[80,633,634,645,649],{"__ignoreMap":78},[83,635,636,638,641,643],{"class":85,"line":86},[83,637,166],{"class":89},[83,639,640],{"class":93}," destroy",[83,642,513],{"class":97},[83,644,516],{"class":93},[83,646,647],{"class":85,"line":115},[83,648,119],{"emptyLinePlaceholder":118},[83,650,651],{"class":85,"line":122},[83,652,653],{"class":125},"# Destroy complete! Resources: 1 destroyed.\n",[10,655,656],{"id":656},"まとめ",[14,658,659],{},"Terraform workspaceを使うことで、以下のメリットがあります。",[45,661,662,665,668],{},[48,663,664],{},"定義ファイルの重複を防ぎ、メンテナンスコストを削減",[48,666,667],{},"個人ごとの環境を独立して管理",[48,669,670],{},"チームスケールに応じた柔軟な運用",[14,672,673],{},"EC2やRDSなど、他のリソースにも同様のパターンを適用できます。",[675,676,677],"style",{},"html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":78,"searchDepth":115,"depth":115,"links":679},[680,681,682,686,687,691,697],{"id":12,"depth":115,"text":12},{"id":22,"depth":115,"text":22},{"id":39,"depth":115,"text":39,"children":683},[684,685],{"id":43,"depth":122,"text":43},{"id":70,"depth":122,"text":71},{"id":153,"depth":115,"text":153},{"id":229,"depth":115,"text":230,"children":688},[689,690],{"id":233,"depth":122,"text":233},{"id":348,"depth":122,"text":348},{"id":495,"depth":115,"text":495,"children":692},[693,694,695,696],{"id":498,"depth":122,"text":498},{"id":547,"depth":122,"text":547},{"id":580,"depth":122,"text":580},{"id":628,"depth":122,"text":628},{"id":656,"depth":115,"text":656},"2023-04-01","Terraform workspaceを活用すれば、10名規模のチームでも定義ファイルを分けずに個人ごとのAWSリソースを効率的に管理できます。実践的な設定例とともに解説します。","md",{"tags":702},[90,166,703],"infrastructure","\u002Fblog\u002Fterraform-workspace-management",{"title":5,"description":699},"blog\u002Fterraform-workspace-management","rtaYAetrRX7QEY_Bm8fUUxCKQxrTbrHAFUijot8UNd4",1773664053938]