Terraform で既存サーバの情報をインポートしてみる

# Terraform で既存サーバの情報をインポートしてみる

今設定されてるものの情報を取りたいときってありますよね。
そんなときには `import` が使えます。
あまり使い勝手はよくないですが、ちょっとやってみましょう。

## terraform import

とりあえず [Command: import](https://www.terraform.io/docs/commands/import.html) を見ながらやってみます。

```aws.tf
provider "aws" {
  region     = "ap-northeast-1"
  access_key = "<access key>"
  secret_key = "<secret key>"
}
```

まずは、`init`　して・・・

```
> ./terraform init

Initializing provider plugins...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.aws: version = "~> 1.50"

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
```

では、`import` してみましょう。
インスタンスID：`i-0c209273bdd039827` でやっています。

```
> ./terraform import aws_instance.foo i-0c209273bdd039827
Error: resource address "aws_instance.foo" does not exist in the configuration.

Before importing this resource, please create its configuration in the root module. For example:

resource "aws_instance" "foo" {
  # (resource arguments)
}
```

おや、失敗ですね。
なんか、リソース用意しとけ的な感じですね。

```aws.tf
provider "aws" {
  region     = "ap-northeast-1"
  access_key = "<access key>"
  secret_key = "<secret key>"
}

resource "aws_instance" "foo" {
  # (resource arguments)
}
```

では、気を取り直して、行ってみましょう。

```
> ./terraform import aws_instance.foo i-0c209273bdd039827
aws_instance.foo: Importing from ID "i-0c209273bdd039827"...
aws_instance.foo: Import complete!
  Imported aws_instance (ID: i-0c209273bdd039827)
aws_instance.foo: Refreshing state... (ID: i-0c209273bdd039827)

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 show
aws_instance.foo:
  id = i-0c209273bdd039827
  ami = ami-0a2de1c3b415889d2
  arn = arn:aws:ec2:ap-northeast-1:682339321057:instance/i-0c209273bdd039827
  associate_public_ip_address = true
  availability_zone = ap-northeast-1a
  cpu_core_count = 1
  cpu_threads_per_core = 1
  credit_specification.# = 1
  credit_specification.0.cpu_credits = standard
  disable_api_termination = false
  ebs_block_device.# = 0
  ebs_optimized = false
  ephemeral_block_device.# = 0
  get_password_data = false
  iam_instance_profile =
  instance_state = running
  instance_type = t2.micro
  ipv6_addresses.# = 0
  key_name = test
  monitoring = false
  network_interface.# = 0
  network_interface_id = eni-0fd2273ec0cfe0545
  password_data =
  placement_group =
  primary_network_interface_id = eni-0fd2273ec0cfe0545
  private_dns = ip-172-31-37-39.ap-northeast-1.compute.internal
  private_ip = 172.31.37.39
  public_dns = ec2-18-179-4-96.ap-northeast-1.compute.amazonaws.com
  public_ip = 18.179.4.96
  root_block_device.# = 1
  root_block_device.0.delete_on_termination = true
  root_block_device.0.iops = 100
  root_block_device.0.volume_id = vol-013305f4e2095702f
  root_block_device.0.volume_size = 8
  root_block_device.0.volume_type = gp2
  security_groups.# = 1
  security_groups.2525401260 = launch-wizard-1
  source_dest_check = true
  subnet_id = subnet-8fcc99c6
  tags.% = 1
  tags.Name = test
  tenancy = default
  volume_tags.% = 0
  vpc_security_group_ids.# = 1
  vpc_security_group_ids.896935136 = sg-0ce00cefd989887ee
```

`terraform.tfstate` には無事取り込めたようですね。

## 終わりに

terraform についている `import` 機能はちょっと面倒ですが、一応既存データのインポートができます。
使い勝手が悪いのでそこまで使わないかもしれませんが、ちょっとした確認には使えるのではないでしょうか。