1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

リソースをTerraform管理下から外す方法と管理下に取り込む方法

Posted at

はじめに

Terraformのコードをリファクタリングする時、リソースの削除や置き換えが発生してしまうことがあります。そんな時、リソースを一時的にTerraform管理下から外すと修正しやすくなる場合があるのでそのやり方をまとめます。

対象リソースを用意

下記のようなmain.tfを作成し、terraform apply を実行して、S3バケットを作成します。

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

resource "aws_s3_bucket" "my_bucket" {
  bucket = "akis-unique-bucket-name"
}

作成できました。
image.png

Terraformで作成したリソース一覧をterraform state list コマンドで確認可能です。

コマンドを実行すると、今作ったaws_s3_bucket.my_bucketが表示されます。
これで作成したS3バケットがTerraform管理にあることが確認できました。

$ terraform state list
aws_s3_bucket.my_bucket

Terraform管理下からリソースを外す方法

下記コマンドを実行します。

terraform state rm <リソース名>

実際に実行すると下記のような結果が得られます。

$ terraform state rm aws_s3_bucket.my_bucket
Removed aws_s3_bucket.my_bucket
Successfully removed 1 resource instance(s).

terraform state listを再実行すると、何も表示されなくなりました。
aws_s3_bucket.my_bucketがTerraform管理から外れ、マネジメントコンソールやAWS CLIで作成した時と同じ状態になりました。

リソースをTerraform管理下に取り込む方法

importコマンドを使って、作成済みリソースをTerraform管理下にすることができます。

各リソースページの一番下にImportという項目があり、参考情報が書かれています。

importブロックの追記
importしたいリソースのimportブロックをtfファイルに記載します。

import.tf というファイル作成して記述するか、import先のresouceのそばに書くのが一般的なようです。

You can add an import block to any Terraform configuration file. A common pattern is to create an imports.tf file, or to place each import block beside the resource block it imports into.

今回は後者を採用して、main.tfaws_s3_bucket.my_bucketの真下に追記します。

resource "aws_s3_bucket" "my_bucket" {
  bucket        = "akis-unique-bucket-name"
  force_destroy = true
}

import {
  to = aws_s3_bucket.bucket
  id = "akis-unique-bucket-name"
}

importブロックの書き方は下記です。
リソースのIDはリソースによって異なります。何がIDになるかは各リソースページのImportに記載されています。aws_s3_bucketの場合はバケット名がIDになります。

import {
  to = <リソースの種類.リソースの名前>
  id = <リソースのID>
}

importコマンド実行

下記のようにterraform importを実行します。

terraform import <リソースの種類.リソースの名前> <リソースのID>

main.tfに記載したリソースに関して実行してみると下記のような結果が得られました。

$ terraform import aws_s3_bucket.my_bucket akis-unique-bucket-name
aws_s3_bucket.my_bucket: Importing from ID "akis-unique-bucket-name"...
aws_s3_bucket.my_bucket: Import prepared!
  Prepared aws_s3_bucket for import
aws_s3_bucket.my_bucket: Refreshing state... [id=akis-unique-bucket-name]

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

terraform state listを実行すると、aws_s3_bucket.my_bucketが表示されているため、再度Terraform管理下に戻せたことが確認できました。

$ terraform state list
aws_s3_bucket.my_bucket
1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?