16
12

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 5 years have passed since last update.

Terraform import機能を使って既存のVPCとサブネットをTerraformで管理できるようにする

Last updated at Posted at 2019-02-10

#はじめに
本記事では、Terraformのimport機能を使って、手動で構築された既存のVPCとサブネットをTerraformで管理できるようにする手順を記載しています。

#前提条件
Terraformはインストールされていることとする。

$ terraform --version
Terraform v0.11.8

下記の構成図のVPCとサブネットをサンプルとする。

#手順
作業ディレクトリに、各設定ファイルを準備する。
variables.tf 内の各変数の値とNameTagの有無は、適宜、既存の環境に合わせて下さい。)

$ tree terraform/
terraform/
├── aws.tf
├── terraform.tfvars
├── variables.tf
└── vpc.tf
terraform.tfvars
aws_access_key = "アクセスキー"
aws_secret_key = "シークレットキー"
aws_region = "ap-northeast-1"
aws.tf
provider "aws" {
  access_key = "${var.aws_access_key}"
  secret_key = "${var.aws_secret_key}"
  region     = "${var.aws_region}"
  version    = "~> 1.0"
}
variables.tf
## AWS
variable "aws_access_key" {}

variable "aws_secret_key" {}

variable "aws_region" {
  default = "ap-northeast-1"
}

data "aws_caller_identity" "self" {}

## VPC CIDR
variable "vpc_cidr" {
  default = "10.1.0.0/16"
}

## VPC Name Tag
variable "vpc_name_tag" {
  default = "terraform-import-vpc"
}

## Subnet
variable "subnet_cidr" {
  type = "map"

  default = {
    public-a  = "10.1.10.0/24"
    public-c  = "10.1.20.0/24"
    private-a = "10.1.100.0/24"
    private-c = "10.1.200.0/24"
  }
}

## Subnet Name Tag
variable "subnet_name_tag" {
  type = "map"

  default = {
    public-a  = "terraform-import-public-subnet-a"
    public-c  = "terraform-import-public-subnet-c"
    private-a = "terraform-import-private-subnet-a"
    private-c = "terraform-import-private-subnet-c"
  }
}
vpc.tf
# VPC
resource "aws_vpc" "vpc" {
  cidr_block = "${var.vpc_cidr}"

  tags {
    Name = "${var.vpc_name_tag}"
  }
}

# Subnet
resource "aws_subnet" "public-a" {
  vpc_id            = "${aws_vpc.vpc.id}"
  cidr_block        = "${var.subnet_cidr["public-a"]}"
  availability_zone = "ap-northeast-1a"

  tags {
    Name = "${var.subnet_name_tag["public-a"]}"
  }
}

resource "aws_subnet" "public-c" {
  vpc_id            = "${aws_vpc.vpc.id}"
  cidr_block        = "${var.subnet_cidr["public-c"]}"
  availability_zone = "ap-northeast-1c"

  tags {
    Name = "${var.subnet_name_tag["public-c"]}"
  }
}

resource "aws_subnet" "private-a" {
  vpc_id            = "${aws_vpc.vpc.id}"
  cidr_block        = "${var.subnet_cidr["private-a"]}"
  availability_zone = "ap-northeast-1a"

  tags {
    Name = "${var.subnet_name_tag["private-a"]}"
  }
}

resource "aws_subnet" "private-c" {
  vpc_id            = "${aws_vpc.vpc.id}"
  cidr_block        = "${var.subnet_cidr["private-c"]}"
  availability_zone = "ap-northeast-1c"

  tags {
    Name = "${var.subnet_name_tag["private-c"]}"
  }
}

下記コマンドで、Terraformの作業ディレクトリを初期化します。

$ terraform init

Initializing provider plugins...
- Checking for available provider plugins on https://releases.hashicorp.com...
- Downloading plugin for provider "aws" (1.58.0)...

Terraform has been successfully initialized!

下記コマンドで、既存のVPCをimportします。

terraform import aws_vpc.vpc [VPCのID]

下記コマンドで、既存のサブネットをimportします。

terraform import aws_subnet.public-a [Public Subnet A の ID]
terraform import aws_subnet.public-c [Public Subnet C の ID]
terraform import aws_subnet.private-a [Private Subnet A の ID]
terraform import aws_subnet.private-c [Private Subnet C の ID]

terraform.tfstateファイルが作成されます。

$ tree terraform/
terraform/
├── aws.tf
├── terraform.tfstate
├── terraform.tfstate.backup
├── terraform.tfvars
├── variables.tf
└── vpc.tf

下記コマンドで、差分がないか確認し、下記メッセージが表示されれば、完了です。

$ terraform plan

No changes. Infrastructure is up-to-date.
16
12
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
16
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?