Terraformで管理するAWSリソースを手動で作り直してしまった場合 (terraform import)

記事タイトルとURLをコピーする

こんにちは!イーゴリです。

今日はTerraformで管理するAWSリソースを手動で作り直してしまった場合の対策方法を紹介したいと思います。

背景

下記の.tfファイルにあるexample_sgというセキュリティグループを手動で作り直してしまいました。そのため、「terraform apply」を実行すると、下記の動作になってしまいます。

.tfstateファイルに元のセキュリティグループ(作り直す前のセキュリティグループ)のIDが記載してありますが、手動で作り直したセキュリティグループのIDは存在していないので、新規セキュリティグループを作成するというアクションになっています。→Plan: 1 to add, 0 to change, 0 to destroy.

.tfファイル

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.27"
    }
  }
}

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

data "aws_caller_identity" "current" {}

output "account_id" {
  value = data.aws_caller_identity.current.account_id
}

<<<省略>>>

resource "aws_security_group" "example_sg" {
  name        = "example-sg"
  description = "example-sg"
  vpc_id      = "vpc-07d12d2"

 
  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  ingress {
    from_port   = 53
    to_port     = 53
    protocol    = "udp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  tags = {
    Name = "ExampleSG"
  }
}

既存のtfstate

terraform planの結果

  # aws_security_group.example_sg will be created
  + resource "aws_security_group" "example_sg" {
      + arn                    = (known after apply)
      + description            = "example-sg"
      + egress                 = [
          + {
              + cidr_blocks      = [
                  + "0.0.0.0/0",

<<<省略>>>

        ]
      + name                   = "example-sg"
      + name_prefix            = (known after apply)
      + owner_id               = (known after apply)
      + revoke_rules_on_delete = false
      + tags                   = {
          + "Name" = "ExampleSG"
        }
      + tags_all               = {
          + "Name" = "ExampleSG"
        }
      + vpc_id                 = "vpc-07d12d2"
    }

Plan: 1 to add, 0 to change, 0 to destroy.

大まかな作業の流れ

  • 手動で削除されたAWSリソースを.tfstateから削除する
  • 手動で作り直したAWSリソースをTerraformにインポートする
  • .tfstateを確認する

手順

すぐに下記のImportコマンドを実行するとエラーになりますので、まずは手動で削除されたAWSリソース(今回はセキュリティグループ)を既存の.tfstateから削除します。

terraform import aws_security_group.example_sg sg-XXX

エラー内容:

│ Error: Resource already managed by Terraform
│ 
│ Terraform is already managing a remote object for
│ aws_security_group.example_sg. To import to this address you must first
│ remove the existing object from the state.

手動で削除されたAWSリソースを.tfstateから削除する

terraform state rm aws_security_group.example_sg

手動で作り直したAWSリソースをTerraformにインポートする

terraform import aws_security_group.example_sg sg-XXX
aws_security_group.example_sg: Importing from ID "sg-XXX"...
aws_security_group.example_sg: Import prepared!
  Prepared aws_security_group for import
<<<省略>>>

Import successful!

.tfstateを確認する

.tfstateを確認すると、新規セキュリティグループ IDが記載してあります。

以上、御一読ありがとうございました。

本田 イーゴリ (記事一覧)

カスタマーサクセス部

・AWS SAP, DOP, SCS, DBS, SAA, DVA, CLF
・Azure AZ-900
・EC-Council CCSE

趣味:日本国内旅行(47都道府県制覇)・ドライブ・音楽