LoginSignup
0
2

More than 3 years have passed since last update.

既に作成されてるAWSリソースをterraformのコードに落とし込む

Last updated at Posted at 2020-09-14

はじめに

awsのterraformテンプレートをを0から書いていくのはなんとなく面倒だったので、既に作成されているawsリソースから作成できないかと調べていたのですが、terraform import と言うコマンドで似たようなことができそうだったのでまとめます。

今回のゴール

今回は既存のVPCをterraformのコードに起こしていきます

ディレクトリ構成

.
├── config.tf
├── terraform.tfstate
├── vpc.tf
└── .gitignore

下準備

1, 初期設定ファイルを作成

awsの初期設定をするファイル

config.tf
variable "aws_access_key" {}
variable "aws_secret_key" {}

provider "aws" {
  version    = "~> 3.0"
  access_key = var.aws_access_key
  secret_key = var.aws_secret_key
  region     = "ap-northeast-1"
}

秘匿情報を代入するファイル

terraform.tfvars
aws_access_key = "各種取得したaccess_key"
aws_secret_key = "各自取得したsecret_key"

vpcの情報を記入するための空ファイル

resource "aws_vpc" "test-tf-vpc" {
  # (resource arguments)
}

ignoreファイル

.gitignore
#  Local .terraform directories
**/.terraform/*

# .tfstate files
*.tfstate
*.tfstate.*

# .tfvars files
*.tfvars

2, 既存のvpcをimport

terraform import リソース名.リソースname リソースID
と言う構文を用いることでデータをimportすることができます
スクリーンショット 2020-09-14 0.19.36.png

$ terraform state show aws_vpc.test-tf-vpc vpc-xxxxxxxx

3, importした内容を確認

terraform import リソース名.リソースname
と言う構文を用いることでimportしたデータを確認することができます

$ terraform state show aws_vpc.test-tf-vpc 
resource "aws_vpc" "test-tf-vpc" {
  arn                              = "xxxxxxxxxxxxx"
  assign_generated_ipv6_cidr_block = false
  cidr_block                       = "10.0.0.0/16"
  default_network_acl_id           = "acl-xxxxxxxxxxx"
  default_route_table_id           = "rtb-xxxxxxxxxxx"
  default_security_group_id        = "sg-xxxxxxxxxxx"
  dhcp_options_id                  = "dopt-xxxxxxxxxxx"
  enable_classiclink               = false
  enable_classiclink_dns_support   = false
  enable_dns_hostnames             = true
  enable_dns_support               = true
  id                               = "vpc-xxxxxxxxxxx"
  instance_tenancy                 = "default"
  main_route_table_id              = "rtb-xxxxxxxxxxx"
  owner_id                         = "xxxxxxxxxxxxx"
  tags = {
    "Name" = "test-tf-vpc"
  }
}

vpc.ftにハードコーディング

importを行ったら公式ドキュメントをみたり、 terraform plan を叩いたりしながらtfファイルを完成させていきます

そして最終的に

$ terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

aws_vpc.test-tf-vpc: Refreshing state... [id=vpc-xxxxxxxxxxxxx]

------------------------------------------------------------------------

No changes. Infrastructure is up-to-date.

This means that Terraform did not detect any differences between your
configuration and real physical resources that exist. As a result, no
actions need to be performed.

と言う文字列が出現すれば完了です

最後に

本当は1コマンドだけで全てのリソースをterraformのファイルに落としてきてもらう物があれば一番良かったのですが、それは難しそうだったので今回は割と頑張るタイプのものになってしまいました汗
ただ、1つ自分専用のterraformテンプレートを持っていれば何かと楽できる場面は多いと思うので1つは所持しておくのがいいのではないかと思います。

0
2
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
0
2