terraformのimport機能を試してみた

はじめに

試しただけなので読む人は注意
使い方はこちらの公式ドキュメントを参照

使用環境・対象

AWS
Terraform 0.7.0

ターゲット

EC2 *1
RDS *1

やってみた

1 とりあえずEC2を対象に実行
# terraform import aws_instance.instance-1  i-xxxxxxxx
provider.aws.region

aws_instance.instance-1: Importing from ID "i-xxxxxxxx"...
aws_instance.instance-1: Import complete!
  Imported aws_instance (ID: i-xxxxxxxx)
aws_instance.instance-1: Refreshing state... (ID: i-xxxxxxx)

Import success! The resources imported are shown above. These are
now in your Terraform state. Import does not currently generate
configuration, so you must do this next. If you do not create configuration
for the above resources, then the next `terraform plan` will mark
them for destruction.

# ls
terraform.tfstate

# cat terraform.tfstate
{
    "version": 3,
    "terraform_version": "0.7.0",
    "serial": 0,
    "lineage": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "modules": [
        {
            "path": [
                "root"
            ],
            "outputs": {},
            "resources": {
                "aws_instance.instance-1": {
                    "type": "aws_instance",
                    "primary": {
                        "id": "i-xxxxxxxx",
                        "attributes": {
                            "ami": "ami-xxxxxxxx",
                            "availability_zone": "ap-northeast-1a",
                            "disable_api_termination": "false",
                            "ebs_block_device.#": "0",
                            "ebs_optimized": "false",
                            "ephemeral_block_device.#": "0",
                            "iam_instance_profile": "InstanceProfile-test",
                            "id": "i-xxxxxxxx",
                            "instance_state": "running",
                            "instance_type": "t2.micro",
                            "key_name": "key-name",
                            "monitoring": "false",
                            "network_interface_id": "eni-xxxxxxxx",
                            "private_dns": "ip-10-0-0-10.ap-northeast-1.compute.internal",
                            "private_ip": "10.0.0.10",
                            "public_dns": "",
                            "public_ip": "xxx.xxx.xxx.xxx",
                            "root_block_device.#": "1",
                            "root_block_device.0.delete_on_termination": "true",
                            "root_block_device.0.iops": "100",
                            "root_block_device.0.volume_size": "8",
                            "root_block_device.0.volume_type": "gp2",
                            "security_groups.#": "0",
                            "source_dest_check": "true",
                            "subnet_id": "subnet-xxxxxx",
                            "tags.%": "2",
                            "tags.Name": "test-tag",
                            "tags.inspector": "before",
                            "tenancy": "default",
                            "vpc_security_group_ids.#": "1",
                            "vpc_security_group_ids.00000000": "sg-xxxxxxxx"
                        },
                        "meta": {
                            "schema_version": "1"
                        }
                    },
                    "provider": "aws"
                }
            }
        }
    ]
}

tfstateファイルが作成された
中にちゃんと設定は記述されている

2 RDSも試してみる
# terraform import aws_db_instance.rds-1 sample-db
aws_db_instance.rds-1: Importing from ID "sample-db"...
aws_db_instance.rds-1: Import complete!
  Imported aws_db_instance (ID: sample-db)
aws_db_instance.rds-1: Refreshing state... (ID: sample-db)

Import success! The resources imported are shown above. These are
now in your Terraform state. Import does not currently generate
configuration, so you must do this next. If you do not create configuration
for the above resources, then the next `terraform plan` will mark
them for destruction.

# ls 
terraform.tfstate               terraform.tfstate.backup

# cat terraform.tfstate
{
    "version": 3,
    "terraform_version": "0.7.0",
    "serial": 0,
    "lineage": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "modules": [
        {
            "path": [
                "root"
            ],
            "outputs": {},
            "resources": {
                "aws_db_instance.rds-1": {
                    "type": "aws_db_instance",
                    "primary": {
                        "id": "sample-db",
                        "attributes": {
                            "address": "sample-db.xxxxxxxx.ap-northeast-1.rds.amazonaws.com",
                            "allocated_storage": "20",
                            "arn": "arn:aws:rds:ap-northeast-1:xxxxxxxxxxxxxx:db:sample-db",
                            "auto_minor_version_upgrade": "true",
                            "availability_zone": "ap-northeast-1c",
                            "backup_retention_period": "7",
                            "backup_window": "15:03-15:33",
                            "copy_tags_to_snapshot": "false",
                            "db_subnet_group_name": "sample-network-dbsubnetgroup-xxxxxx",
                            "endpoint": "sample-db.xxxxxxxxxx.ap-northeast-1.rds.amazonaws.com:3306",
                            "engine": "mysql",
                            "engine_version": "5.6.23",
                            "id": "sample-db",
                            "identifier": "sample-db",
                            "instance_class": "db.t2.micro",
                            "iops": "0",
                            "kms_key_id": "",
                            "license_model": "general-public-license",
                            "maintenance_window": "wed:20:00-wed:20:30",
                            "monitoring_interval": "0",
                            "multi_az": "false",
                            "name": "sample",
                            "option_group_name": "default:mysql-5-6",
                            "parameter_group_name": "sampe-mysql56",
                            "port": "3306",
                            "publicly_accessible": "false",
                            "replicas.#": "0",
                            "replicate_source_db": "",
                            "security_group_names.#": "0",
                            "skip_final_snapshot": "true",
                            "status": "available",
                            "storage_encrypted": "false",
                            "storage_type": "standard",
                            "tags.%": "1",
                            "tags.workload-type": "other",
                            "username": "hogehoge",
                            "vpc_security_group_ids.#": "1",
                            "vpc_security_group_ids.xxxxxxxxxx": "sg-xxxxxxxx"
                        }
                    },
                    "provider": "aws"
                },
                "aws_instance.instance-1": {
                    "type": "aws_instance",
                    "primary": {
                        "id": "i-xxxxxxxx",
                        "attributes": {
                            "ami": "ami-xxxxxxxx",
                            "availability_zone": "ap-northeast-1a",
                            "disable_api_termination": "false",
                            "ebs_block_device.#": "0",
                            "ebs_optimized": "false",
                            "ephemeral_block_device.#": "0",
                            "iam_instance_profile": "InstanceProfile-test",
                            "id": "i-xxxxxxxx",
                            "instance_state": "running",
                            "instance_type": "t2.micro",
                            "key_name": "key-name",
                            "monitoring": "false",
                            "network_interface_id": "eni-xxxxxxxx",
                            "private_dns": "ip-10-0-0-10.ap-northeast-1.compute.internal",
                            "private_ip": "10.0.0.10",
                            "public_dns": "",
                            "public_ip": "xxx.xxx.xxx.xxx",
                            "root_block_device.#": "1",
                            "root_block_device.0.delete_on_termination": "true",
                            "root_block_device.0.iops": "100",
                            "root_block_device.0.volume_size": "8",
                            "root_block_device.0.volume_type": "gp2",
                            "security_groups.#": "0",
                            "source_dest_check": "true",
                            "subnet_id": "subnet-xxxxxx",
                            "tags.%": "2",
                            "tags.Name": "test-tag",
                            "tags.inspector": "before",
                            "tenancy": "default",
                            "vpc_security_group_ids.#": "1",
                            "vpc_security_group_ids.00000000": "sg-xxxxxxxx"
                        },
                        "meta": {
                            "schema_version": "1"
                        }
                    },
                    "provider": "aws"
                }
            }
        }
    ]
}

カレントディレクトリにあるterraform.tfstateに追記されている。
古いものは.backupとなった

元記事はこちら

terraform importを試してみた