Terraformに入門したのでAWSのVPCを作成してEC2を起動する

AWSの環境を構築する場合に利用するツールとしてはAWS公式のCloudFormationがある。
だが、CloudFormation以外ではTerraformが有力な選択肢の1つになると思う。

若干必要に迫られてTerraformを触ることになったので、自分で試してみたことや調べたことをまとめておく。

Terraformとは

Hashcorp社が開発しているオープンソースのソフトウェアで、ドキュメントでは「Terraformは、インフラストラクチャを安全かつ効率的に構築、変更、およびバージョン管理するためのツールです。」と記載されている。

開発はGitHub上で行われている。

Amazon Web Services(以下、AWS)だけでなく、GCPやAzure、Heroku、OpenStackなどの非常に多くのインフラに対応している。
プロバイダというのが、Terraform上でどの環境を利用するか定義するもの。

CloudFormationとどちらが良いのかは環境によりけりだと思うが、Terraformの公式ドキュメントには、複数の環境を組み合わせる場合や、事前の実行計画を確認できるところが利点であることが挙げられている。

Terraformのセットアップ

Terraformを使うには、まずバイナリファイルをダウンロードしてそれを解凍し、できたファイルをPATHの通った場所に配置するだけ。

$ wget -O /tmp/terraform_0.11.11_linux_amd64.zip https://releases.hashicorp.com/terraform/0.11.11/terraform_0.11.11_linux_amd64.zip
$ unzip /tmp/terraform_0.11.11_linux_amd64.zip -d /usr/local/bin
$ terraform -v
Terraform v0.11.11

これでTerraformが使えるようになった。

TerraformでAWSのVPCとEC2作成

早速AWSの環境を構築してみる。

作業ディレクトリの作成

まずTerraformを実行するためのテンプレートファイルを管理するディレクトリを作成する。

$ mkdir terraform-example

テンプレートはHCL(HashiCorp Configuration Language)というHashiCorp製の独自のDSLで記述する。

上記のドキュメントにもあるが、JSON互換となっている。

テンプレートファイルの作成

テンプレートファイルを作成する。拡張子は.tfとなり、Terraformは自動でこの拡張子のものを読み込んでくれる。
なので、名前はなんでも良いがここではmain.tfとしている。

$ vim main.tf

別途変数の切り出しとかもできるが、ここでは実施せず、main.tfファイル1つで管理する。

プロバイダーの設定

最初にproviderという指定をする必要がある。

複数の環境に対応しているため、「どのプロバイダーを使うのか?」を宣言する。
main.tfに以下のように記述する。

access_key/secret_keyは$HOME/.aws/credentialsに記載しておいても良い。
shared_credentials_filecredentialsファイルの場所を指定することもできる

provider "aws" {
  access_key = "anaccesskey"
  secret_key = "asecretkey"
  #shared_credentials_file = "~/.aws/credentials"
  region = "ap-northeast-1"
  profile = "default"
}
}
リソースの設定

Terraformでのリソースとはインフラの構成要素を指す。
利用するリソースをresourceブロックで設定する。resource "リソースタイプ(最初のパラメータ)" "リソース名(2番目のパラメータ)" {}という構文となる。
タイプと名前の組み合わせは一意である必要がある。

リソースタイプは、プロバイダーがAWSの場合はaws_*という名前でTerraformで予め定義されている。VPCであればaws_vpc、EC2であればaws_instanceとなる。
その他のAWSで利用可能なリソースはドキュメントを参照のこと。

リソース名は任意の名前を設定可能。わかりやすい名前を付ければ良い。

VPCの作成

main.tfに以下を追記する。
リソースタイプをaws_vpcとする。リソース名はterraform_example_vpcとしている。
ブロックの中にcidr_blockやTagなどのパラメータを記述する。

# VPC
resource "aws_vpc" "terraform_example_vpc" {
  cidr_block = "10.1.0.0/16"
  instance_tenancy = "default"
  enable_dns_support = "true"
  enable_dns_hostnames = "true"
  tags {
    Name = "tf-example-vpc"
  }
}
VPCに関連するネットワークリソースの作成

main.tfに以下を追記する。
InternetGateway, RouteTable, Subnet, およびSubnetへのRouteTableの割り当てを行う。
他のリソースタイプの値を参照したい場合は、vpc_id = "${aws_vpc.example_vpc.id}"のように、<リソースタイプ>.<リソース名>.<属性名>と指定する。

# InternetGateway
resource "aws_internet_gateway" "terraform_example_igw" {
  vpc_id = "${aws_vpc.terraform_example_vpc.id}"
}

# RouteTable
resource "aws_route_table" "terraform_example_public_rt" {
  vpc_id = "${aws_vpc.terraform_example_vpc.id}"
  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = "${aws_internet_gateway.terraform_example_igw.id}"
  }
  tags {
    Name = "public"
  }
}

# Subnet
resource "aws_subnet" "terraform_example_subnet_public-a" {
  vpc_id = "${aws_vpc.terraform_example_vpc.id}"
  cidr_block = "10.1.1.0/24"
  availability_zone = "ap-northeast-1a"
  tags {
    Name = "public-a"
  }
}

resource "aws_subnet" "terraform_example_subnet_public-c" {
  vpc_id = "${aws_vpc.terraform_example_vpc.id}"
  cidr_block = "10.1.2.0/24"
  availability_zone = "ap-northeast-1c"
  tags {
    Name = "public-c"
  }
}

resource "aws_subnet" "terraform_example_subnet_public-d" {
  vpc_id = "${aws_vpc.terraform_example_vpc.id}"
  cidr_block = "10.1.3.0/24"
  availability_zone = "ap-northeast-1d"
  tags {
    Name = "public-d"
  }
}

# SubnetRouteTableAssociation
resource "aws_route_table_association" "terraform_example_rt_public-a" {
    subnet_id = "${aws_subnet.terraform_example_subnet_public-a.id}"
    route_table_id = "${aws_route_table.terraform_example_public_rt.id}"
}

resource "aws_route_table_association" "terraform_example_rt_public-c" {
    subnet_id = "${aws_subnet.terraform_example_subnet_public-c.id}"
    route_table_id = "${aws_route_table.terraform_example_public_rt.id}"
}
EC2の作成

main.tfに以下を追記する。
aws_security_group, aws_instanceのリソースタイプを使ってセキュリティグループ、EC2を作成する。
必要なネットワーク情報は上記のリソースタイプを参照するように記述する。

# Security Group
resource "aws_security_group" "terraform_example_sg" {
    name = "APP_SG"
    vpc_id = "${aws_vpc.terraform_example_vpc.id}"
    ingress {
        from_port = 22
        to_port = 22
        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"]
    }
    description = "tf-example-sg"
}

# EC2
resource "aws_instance" "terraform_example_ec2" {
    ami = "ami-2a69be4c"
    instance_type = "t2.micro"
  disable_api_termination = false
  key_name                = "aws-key-pair"
  vpc_security_group_ids  = ["${aws_security_group.terraform_example_sg.id}"]
  subnet_id               = "${aws_subnet.terraform_example_subnet_public-a.id}"

  tags {
    Name = "tf-example-ec2"
  }
}

# ElasticIP
resource "aws_eip" "terraform_example_eip" {
    instance = "${aws_instance.terraform_example_ec2.id}"
    vpc = true
}

実行

初期化

terraform initで作業ディレクトリを初期化する。初期化といっても作成した.tfが消えることはない。利用するterraformプラグイン(ここではAWS)の初期化したり設定内容とバージョンの互換性をチェックしてくれる模様。

$ 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.57"

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.
実行計画の作成

terraform plan実行計画を作成する。
際のリソースや状態を変更することなく、一連の変更の実行計画がユーザーの予想と一致するかどうかを確認できる。

$ 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.


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

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + aws_eip.terraform_example_eip
      id:                                          
      allocation_id:                               
      association_id:                              
      domain:                                      
      instance:                                    "${aws_instance.terraform_example_ec2.id}"
      network_interface:                           
      private_ip:                                  
      public_ip:                                   
      public_ipv4_pool:                            
      vpc:                                         "true"

  + aws_instance.terraform_example_ec2
      id:                                          
      ami:                                         "ami-2a69be4c"
      arn:                                         
      associate_public_ip_address:                 
      availability_zone:                           
      cpu_core_count:                              
      cpu_threads_per_core:                        
      disable_api_termination:                     "false"
      ebs_block_device.#:                          
      ephemeral_block_device.#:                    
      get_password_data:                           "false"
      host_id:                                     
      instance_state:                              
      instance_type:                               "t2.micro"
      ipv6_address_count:                          
      ipv6_addresses.#:                            
      key_name:                                    "aws-key-pair"
      network_interface.#:                         
      network_interface_id:                        
      password_data:                               
      placement_group:                             
      primary_network_interface_id:                
      private_dns:                                 
      private_ip:                                  
      public_dns:                                  
      public_ip:                                   
      root_block_device.#:                         
      security_groups.#:                           
      source_dest_check:                           "true"
      subnet_id:                                   "${aws_subnet.terraform_example_subnet_public-a.id}"
      tags.%:                                      "1"
      tags.Name:                                   "tf-example-ec2"
      tenancy:                                     
      volume_tags.%:                               
      vpc_security_group_ids.#:                    

  + aws_internet_gateway.terraform_example_igw
      id:                                          
      owner_id:                                    
      vpc_id:                                      "${aws_vpc.terraform_example_vpc.id}"

  + aws_route_table.terraform_example_public_rt
      id:                                          
      owner_id:                                    
      propagating_vgws.#:                          
      route.#:                                     "1"
      route.~3477870916.cidr_block:                "0.0.0.0/0"
      route.~3477870916.egress_only_gateway_id:    ""
      route.~3477870916.gateway_id:                "${aws_internet_gateway.terraform_example_igw.id}"
      route.~3477870916.instance_id:               ""
      route.~3477870916.ipv6_cidr_block:           ""
      route.~3477870916.nat_gateway_id:            ""
      route.~3477870916.network_interface_id:      ""
      route.~3477870916.transit_gateway_id:        ""
      route.~3477870916.vpc_peering_connection_id: ""
      tags.%:                                      "1"
      tags.Name:                                   "public"
      vpc_id:                                      "${aws_vpc.terraform_example_vpc.id}"

  + aws_route_table_association.terraform_example_rt_public-a
      id:                                          
      route_table_id:                              "${aws_route_table.terraform_example_public_rt.id}"
      subnet_id:                                   "${aws_subnet.terraform_example_subnet_public-a.id}"

  + aws_route_table_association.terraform_example_rt_public-c
      id:                                          
      route_table_id:                              "${aws_route_table.terraform_example_public_rt.id}"
      subnet_id:                                   "${aws_subnet.terraform_example_subnet_public-c.id}"

  + aws_security_group.terraform_example_sg
      id:                                          
      arn:                                         
      description:                                 "tf-example-sg"
      egress.#:                                    "1"
      egress.482069346.cidr_blocks.#:              "1"
      egress.482069346.cidr_blocks.0:              "0.0.0.0/0"
      egress.482069346.description:                ""
      egress.482069346.from_port:                  "0"
      egress.482069346.ipv6_cidr_blocks.#:         "0"
      egress.482069346.prefix_list_ids.#:          "0"
      egress.482069346.protocol:                   "-1"
      egress.482069346.security_groups.#:          "0"
      egress.482069346.self:                       "false"
      egress.482069346.to_port:                    "0"
      ingress.#:                                   "1"
      ingress.2541437006.cidr_blocks.#:            "1"
      ingress.2541437006.cidr_blocks.0:            "0.0.0.0/0"
      ingress.2541437006.description:              ""
      ingress.2541437006.from_port:                "22"
      ingress.2541437006.ipv6_cidr_blocks.#:       "0"
      ingress.2541437006.prefix_list_ids.#:        "0"
      ingress.2541437006.protocol:                 "tcp"
      ingress.2541437006.security_groups.#:        "0"
      ingress.2541437006.self:                     "false"
      ingress.2541437006.to_port:                  "22"
      name:                                        "APP_SG"
      owner_id:                                    
      revoke_rules_on_delete:                      "false"
      vpc_id:                                      "${aws_vpc.terraform_example_vpc.id}"

  + aws_subnet.terraform_example_subnet_public-a
      id:                                          
      arn:                                         
      assign_ipv6_address_on_creation:             "false"
      availability_zone:                           "ap-northeast-1a"
      availability_zone_id:                        
      cidr_block:                                  "10.1.1.0/24"
      ipv6_cidr_block:                             
      ipv6_cidr_block_association_id:              
      map_public_ip_on_launch:                     "false"
      owner_id:                                    
      tags.%:                                      "1"
      tags.Name:                                   "public-a"
      vpc_id:                                      "${aws_vpc.terraform_example_vpc.id}"

  + aws_subnet.terraform_example_subnet_public-c
      id:                                          
      arn:                                         
      assign_ipv6_address_on_creation:             "false"
      availability_zone:                           "ap-northeast-1c"
      availability_zone_id:                        
      cidr_block:                                  "10.1.2.0/24"
      ipv6_cidr_block:                             
      ipv6_cidr_block_association_id:              
      map_public_ip_on_launch:                     "false"
      owner_id:                                    
      tags.%:                                      "1"
      tags.Name:                                   "public-c"
      vpc_id:                                      "${aws_vpc.terraform_example_vpc.id}"

  + aws_subnet.terraform_example_subnet_public-d
      id:                                          
      arn:                                         
      assign_ipv6_address_on_creation:             "false"
      availability_zone:                           "ap-northeast-1d"
      availability_zone_id:                        
      cidr_block:                                  "10.1.3.0/24"
      ipv6_cidr_block:                             
      ipv6_cidr_block_association_id:              
      map_public_ip_on_launch:                     "false"
      owner_id:                                    
      tags.%:                                      "1"
      tags.Name:                                   "public-d"
      vpc_id:                                      "${aws_vpc.terraform_example_vpc.id}"

  + aws_vpc.terraform_example_vpc
      id:                                          
      arn:                                         
      assign_generated_ipv6_cidr_block:            "false"
      cidr_block:                                  "10.1.0.0/16"
      default_network_acl_id:                      
      default_route_table_id:                      
      default_security_group_id:                   
      dhcp_options_id:                             
      enable_classiclink:                          
      enable_classiclink_dns_support:              
      enable_dns_hostnames:                        "true"
      enable_dns_support:                          "true"
      instance_tenancy:                            "default"
      ipv6_association_id:                         
      ipv6_cidr_block:                             
      main_route_table_id:                         
      owner_id:                                    
      tags.%:                                      "1"
      tags.Name:                                   "tf-example-vpc"


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

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

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.
実行

terraform applyでテンプレートを実行してリソースを生成する。

$ terraform apply

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create

Terraform will perform the following actions:
+ aws_eip.terraform_example_eip
id:                                          
allocation_id:                               
association_id:                              
domain:                                      
instance:                                    "${aws_instance.terraform_example_ec2.id}"
network_interface:                           
private_ip:                                  
public_ip:                                   
public_ipv4_pool:                            
vpc:                                         "true"

+ aws_instance.terraform_example_ec2
id:                                          
ami:                                         "ami-2a69be4c"
arn:                                         
associate_public_ip_address:                 
availability_zone:                           
cpu_core_count:                              
cpu_threads_per_core:                        
disable_api_termination:                     "false"
ebs_block_device.#:                          
ephemeral_block_device.#:                    
get_password_data:                           "false"
host_id:                                     
instance_state:                              
instance_type:                               "t2.micro"
ipv6_address_count:                          
ipv6_addresses.#:                            
key_name:                                    "aws-key-pair"
network_interface.#:                         
network_interface_id:                        
password_data:                               
placement_group:                             
primary_network_interface_id:                
private_dns:                                 
private_ip:                                  
public_dns:                                  
public_ip:                                   
root_block_device.#:                         
security_groups.#:                           
source_dest_check:                           "true"
subnet_id:                                   "${aws_subnet.terraform_example_subnet_public-a.id}"
tags.%:                                      "1"
tags.Name:                                   "tf-example-ec2"
tenancy:                                     
volume_tags.%:                               
vpc_security_group_ids.#:                    

+ aws_internet_gateway.terraform_example_igw
id:                                          
owner_id:                                    
vpc_id:                                      "${aws_vpc.terraform_example_vpc.id}"

+ aws_route_table.terraform_example_public_rt
id:                                          
owner_id:                                    
propagating_vgws.#:                          
route.#:                                     "1"
route.~3477870916.cidr_block:                "0.0.0.0/0"
route.~3477870916.egress_only_gateway_id:    ""
route.~3477870916.gateway_id:                "${aws_internet_gateway.terraform_example_igw.id}"
route.~3477870916.instance_id:               ""
route.~3477870916.ipv6_cidr_block:           ""
route.~3477870916.nat_gateway_id:            ""
route.~3477870916.network_interface_id:      ""
route.~3477870916.transit_gateway_id:        ""
route.~3477870916.vpc_peering_connection_id: ""
tags.%:                                      "1"
tags.Name:                                   "public"
vpc_id:                                      "${aws_vpc.terraform_example_vpc.id}"

+ aws_route_table_association.terraform_example_rt_public-a
id:                                          
route_table_id:                              "${aws_route_table.terraform_example_public_rt.id}"
subnet_id:                                   "${aws_subnet.terraform_example_subnet_public-a.id}"

+ aws_route_table_association.terraform_example_rt_public-c
id:                                          
route_table_id:                              "${aws_route_table.terraform_example_public_rt.id}"
subnet_id:                                   "${aws_subnet.terraform_example_subnet_public-c.id}"

+ aws_security_group.terraform_example_sg
id:                                          
arn:                                         
description:                                 "tf-example-sg"
egress.#:                                    "1"
egress.482069346.cidr_blocks.#:              "1"
egress.482069346.cidr_blocks.0:              "0.0.0.0/0"
egress.482069346.description:                ""
egress.482069346.from_port:                  "0"
egress.482069346.ipv6_cidr_blocks.#:         "0"
egress.482069346.prefix_list_ids.#:          "0"
egress.482069346.protocol:                   "-1"
egress.482069346.security_groups.#:          "0"
egress.482069346.self:                       "false"
egress.482069346.to_port:                    "0"
ingress.#:                                   "1"
ingress.2541437006.cidr_blocks.#:            "1"
ingress.2541437006.cidr_blocks.0:            "0.0.0.0/0"
ingress.2541437006.description:              ""
ingress.2541437006.from_port:                "22"
ingress.2541437006.ipv6_cidr_blocks.#:       "0"
ingress.2541437006.prefix_list_ids.#:        "0"
ingress.2541437006.protocol:                 "tcp"
ingress.2541437006.security_groups.#:        "0"
ingress.2541437006.self:                     "false"
ingress.2541437006.to_port:                  "22"
name:                                        "APP_SG"
owner_id:                                    
revoke_rules_on_delete:                      "false"
vpc_id:                                      "${aws_vpc.terraform_example_vpc.id}"

+ aws_subnet.terraform_example_subnet_public-a
id:                                          
arn:                                         
assign_ipv6_address_on_creation:             "false"
availability_zone:                           "ap-northeast-1a"
availability_zone_id:                        
cidr_block:                                  "10.1.1.0/24"
ipv6_cidr_block:                             
ipv6_cidr_block_association_id:              
map_public_ip_on_launch:                     "false"
owner_id:                                    
tags.%:                                      "1"
tags.Name:                                   "public-a"
vpc_id:                                      "${aws_vpc.terraform_example_vpc.id}"

+ aws_subnet.terraform_example_subnet_public-c
id:                                          
arn:                                         
assign_ipv6_address_on_creation:             "false"
availability_zone:                           "ap-northeast-1c"
availability_zone_id:                        
cidr_block:                                  "10.1.2.0/24"
ipv6_cidr_block:                             
ipv6_cidr_block_association_id:              
map_public_ip_on_launch:                     "false"
owner_id:                                    
tags.%:                                      "1"
tags.Name:                                   "public-c"
vpc_id:                                      "${aws_vpc.terraform_example_vpc.id}"

+ aws_subnet.terraform_example_subnet_public-d
id:                                          
arn:                                         
assign_ipv6_address_on_creation:             "false"
availability_zone:                           "ap-northeast-1d"
availability_zone_id:                        
cidr_block:                                  "10.1.3.0/24"
ipv6_cidr_block:                             
ipv6_cidr_block_association_id:              
map_public_ip_on_launch:                     "false"
owner_id:                                    
tags.%:                                      "1"
tags.Name:                                   "public-d"
vpc_id:                                      "${aws_vpc.terraform_example_vpc.id}"

+ aws_vpc.terraform_example_vpc
id:                                          
arn:                                         
assign_generated_ipv6_cidr_block:            "false"
cidr_block:                                  "10.1.0.0/16"
default_network_acl_id:                      
default_route_table_id:                      
default_security_group_id:                   
dhcp_options_id:                             
enable_classiclink:                          
enable_classiclink_dns_support:              
enable_dns_hostnames:                        "true"
enable_dns_support:                          "true"
instance_tenancy:                            "default"
ipv6_association_id:                         
ipv6_cidr_block:                             
main_route_table_id:                         
owner_id:                                    
tags.%:                                      "1"
tags.Name:                                   "tf-example-vpc"

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

Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.

Enter a value: yes

aws_vpc.terraform_example_vpc: Creating...
arn:                              "" => ""
assign_generated_ipv6_cidr_block: "" => "false"
cidr_block:                       "" => "10.1.0.0/16"
default_network_acl_id:           "" => ""
default_route_table_id:           "" => ""
default_security_group_id:        "" => ""
dhcp_options_id:                  "" => ""
enable_classiclink:               "" => ""
enable_classiclink_dns_support:   "" => ""
enable_dns_hostnames:             "" => "true"
enable_dns_support:               "" => "true"
instance_tenancy:                 "" => "default"
ipv6_association_id:              "" => ""
ipv6_cidr_block:                  "" => ""
main_route_table_id:              "" => ""
owner_id:                         "" => ""
tags.%:                           "" => "1"
tags.Name:                        "" => "tf-example-vpc"
aws_vpc.terraform_example_vpc: Creation complete after 4s (ID: vpc-084056faf393b50f1)
aws_subnet.terraform_example_subnet_public-a: Creating...
arn:                             "" => ""
assign_ipv6_address_on_creation: "" => "false"
availability_zone:               "" => "ap-northeast-1a"
availability_zone_id:            "" => ""
cidr_block:                      "" => "10.1.1.0/24"
ipv6_cidr_block:                 "" => ""
ipv6_cidr_block_association_id:  "" => ""
map_public_ip_on_launch:         "" => "false"
owner_id:                        "" => ""
tags.%:                          "" => "1"
tags.Name:                       "" => "public-a"
vpc_id:                          "" => "vpc-084056faf393b50f1"
aws_subnet.terraform_example_subnet_public-d: Creating...
arn:                             "" => ""
assign_ipv6_address_on_creation: "" => "false"
availability_zone:               "" => "ap-northeast-1d"
availability_zone_id:            "" => ""
cidr_block:                      "" => "10.1.3.0/24"
ipv6_cidr_block:                 "" => ""
ipv6_cidr_block_association_id:  "" => ""
map_public_ip_on_launch:         "" => "false"
owner_id:                        "" => ""
tags.%:                          "" => "1"
tags.Name:                       "" => "public-d"
vpc_id:                          "" => "vpc-084056faf393b50f1"
aws_security_group.terraform_example_sg: Creating...
arn:                                   "" => ""
description:                           "" => "tf-example-sg"
egress.#:                              "" => "1"
egress.482069346.cidr_blocks.#:        "" => "1"
egress.482069346.cidr_blocks.0:        "" => "0.0.0.0/0"
egress.482069346.description:          "" => ""
egress.482069346.from_port:            "" => "0"
egress.482069346.ipv6_cidr_blocks.#:   "" => "0"
egress.482069346.prefix_list_ids.#:    "" => "0"
egress.482069346.protocol:             "" => "-1"
egress.482069346.security_groups.#:    "" => "0"
egress.482069346.self:                 "" => "false"
egress.482069346.to_port:              "" => "0"
ingress.#:                             "" => "1"
ingress.2541437006.cidr_blocks.#:      "" => "1"
ingress.2541437006.cidr_blocks.0:      "" => "0.0.0.0/0"
ingress.2541437006.description:        "" => ""
ingress.2541437006.from_port:          "" => "22"
ingress.2541437006.ipv6_cidr_blocks.#: "" => "0"
ingress.2541437006.prefix_list_ids.#:  "" => "0"
ingress.2541437006.protocol:           "" => "tcp"
ingress.2541437006.security_groups.#:  "" => "0"
ingress.2541437006.self:               "" => "false"
ingress.2541437006.to_port:            "" => "22"
name:                                  "" => "APP_SG"
owner_id:                              "" => ""
revoke_rules_on_delete:                "" => "false"
vpc_id:                                "" => "vpc-084056faf393b50f1"
aws_subnet.terraform_example_subnet_public-c: Creating...
arn:                             "" => ""
assign_ipv6_address_on_creation: "" => "false"
availability_zone:               "" => "ap-northeast-1c"
availability_zone_id:            "" => ""
cidr_block:                      "" => "10.1.2.0/24"
ipv6_cidr_block:                 "" => ""
ipv6_cidr_block_association_id:  "" => ""
map_public_ip_on_launch:         "" => "false"
owner_id:                        "" => ""
tags.%:                          "" => "1"
tags.Name:                       "" => "public-c"
vpc_id:                          "" => "vpc-084056faf393b50f1"
aws_internet_gateway.terraform_example_igw: Creating...
owner_id: "" => ""
vpc_id:   "" => "vpc-084056faf393b50f1"
aws_internet_gateway.terraform_example_igw: Creation complete after 1s (ID: igw-031a1b3bb9b08caaf)
aws_route_table.terraform_example_public_rt: Creating...
owner_id:                                   "" => ""
propagating_vgws.#:                         "" => ""
route.#:                                    "" => "1"
route.4202819103.cidr_block:                "" => "0.0.0.0/0"
route.4202819103.egress_only_gateway_id:    "" => ""
route.4202819103.gateway_id:                "" => "igw-031a1b3bb9b08caaf"
route.4202819103.instance_id:               "" => ""
route.4202819103.ipv6_cidr_block:           "" => ""
route.4202819103.nat_gateway_id:            "" => ""
route.4202819103.network_interface_id:      "" => ""
route.4202819103.transit_gateway_id:        "" => ""
route.4202819103.vpc_peering_connection_id: "" => ""
tags.%:                                     "" => "1"
tags.Name:                                  "" => "public"
vpc_id:                                     "" => "vpc-084056faf393b50f1"
aws_subnet.terraform_example_subnet_public-d: Creation complete after 1s (ID: subnet-08a80915ac04d7b90)
aws_subnet.terraform_example_subnet_public-c: Creation complete after 1s (ID: subnet-04568b62453decefa)
aws_subnet.terraform_example_subnet_public-a: Creation complete after 1s (ID: subnet-0d1c7eed2ead4e307)
aws_route_table.terraform_example_public_rt: Creation complete after 1s (ID: rtb-0f6bf85f04c90ecef)
aws_route_table_association.terraform_example_rt_public-a: Creating...
route_table_id: "" => "rtb-0f6bf85f04c90ecef"
subnet_id:      "" => "subnet-0d1c7eed2ead4e307"
aws_route_table_association.terraform_example_rt_public-c: Creating...
route_table_id: "" => "rtb-0f6bf85f04c90ecef"
subnet_id:      "" => "subnet-04568b62453decefa"
aws_security_group.terraform_example_sg: Creation complete after 2s (ID: sg-0e1dbb4daad93fc80)
aws_instance.terraform_example_ec2: Creating...
ami:                              "" => "ami-2a69be4c"
arn:                              "" => ""
associate_public_ip_address:      "" => ""
availability_zone:                "" => ""
cpu_core_count:                   "" => ""
cpu_threads_per_core:             "" => ""
disable_api_termination:          "" => "false"
ebs_block_device.#:               "" => ""
ephemeral_block_device.#:         "" => ""
get_password_data:                "" => "false"
host_id:                          "" => ""
instance_state:                   "" => ""
instance_type:                    "" => "t2.micro"
ipv6_address_count:               "" => ""
ipv6_addresses.#:                 "" => ""
key_name:                         "" => "aws-key-pair"
network_interface.#:              "" => ""
network_interface_id:             "" => ""
password_data:                    "" => ""
placement_group:                  "" => ""
primary_network_interface_id:     "" => ""
private_dns:                      "" => ""
private_ip:                       "" => ""
public_dns:                       "" => ""
public_ip:                        "" => ""
root_block_device.#:              "" => ""
security_groups.#:                "" => ""
source_dest_check:                "" => "true"
subnet_id:                        "" => "subnet-0d1c7eed2ead4e307"
tags.%:                           "" => "1"
tags.Name:                        "" => "tf-example-ec2"
tenancy:                          "" => ""
volume_tags.%:                    "" => ""
vpc_security_group_ids.#:         "" => "1"
vpc_security_group_ids.412218158: "" => "sg-0e1dbb4daad93fc80"
aws_route_table_association.terraform_example_rt_public-a: Creation complete after 0s (ID: rtbassoc-0738393cba67fe3bc)
aws_route_table_association.terraform_example_rt_public-c: Creation complete after 0s (ID: rtbassoc-062a44d929c881bcb)
aws_instance.terraform_example_ec2: Still creating... (10s elapsed)
aws_instance.terraform_example_ec2: Still creating... (20s elapsed)
aws_instance.terraform_example_ec2: Still creating... (30s elapsed)
aws_instance.terraform_example_ec2: Creation complete after 33s (ID: i-052a7a262f0fec3e1)
aws_eip.terraform_example_eip: Creating...
allocation_id:     "" => ""
association_id:    "" => ""
domain:            "" => ""
instance:          "" => "i-052a7a262f0fec3e1"
network_interface: "" => ""
private_ip:        "" => ""
public_ip:         "" => ""
public_ipv4_pool:  "" => ""
vpc:               "" => "true"
aws_eip.terraform_example_eip: Creation complete after 1s (ID: eipalloc-0912a5f0e5dc2ec34)

Apply complete! Resources: 11 added, 0 changed, 0 destroyed.
削除

terraform applyでテンプレートで生成されたリソースを削除する。

$ terraform destroy
aws_vpc.terraform_example_vpc: Refreshing state... (ID: vpc-084056faf393b50f1)
aws_subnet.terraform_example_subnet_public-c: Refreshing state... (ID: subnet-04568b62453decefa)
aws_security_group.terraform_example_sg: Refreshing state... (ID: sg-0e1dbb4daad93fc80)
aws_subnet.terraform_example_subnet_public-a: Refreshing state... (ID: subnet-0d1c7eed2ead4e307)
aws_subnet.terraform_example_subnet_public-d: Refreshing state... (ID: subnet-08a80915ac04d7b90)
aws_internet_gateway.terraform_example_igw: Refreshing state... (ID: igw-031a1b3bb9b08caaf)
aws_route_table.terraform_example_public_rt: Refreshing state... (ID: rtb-0f6bf85f04c90ecef)
aws_instance.terraform_example_ec2: Refreshing state... (ID: i-052a7a262f0fec3e1)
aws_route_table_association.terraform_example_rt_public-a: Refreshing state... (ID: rtbassoc-0738393cba67fe3bc)
aws_route_table_association.terraform_example_rt_public-c: Refreshing state... (ID: rtbassoc-062a44d929c881bcb)
aws_eip.terraform_example_eip: Refreshing state... (ID: eipalloc-0912a5f0e5dc2ec34)

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
- destroy

Terraform will perform the following actions:

- aws_eip.terraform_example_eip

- aws_instance.terraform_example_ec2

- aws_internet_gateway.terraform_example_igw

- aws_route_table.terraform_example_public_rt

- aws_route_table_association.terraform_example_rt_public-a

- aws_route_table_association.terraform_example_rt_public-c

- aws_security_group.terraform_example_sg

- aws_subnet.terraform_example_subnet_public-a

- aws_subnet.terraform_example_subnet_public-c

- aws_subnet.terraform_example_subnet_public-d

- aws_vpc.terraform_example_vpc

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

Do you really want to destroy all resources?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.

Enter a value: yes

aws_eip.terraform_example_eip: Destroying... (ID: eipalloc-0912a5f0e5dc2ec34)
aws_subnet.terraform_example_subnet_public-d: Destroying... (ID: subnet-08a80915ac04d7b90)
aws_route_table_association.terraform_example_rt_public-c: Destroying... (ID: rtbassoc-062a44d929c881bcb)
aws_route_table_association.terraform_example_rt_public-a: Destroying... (ID: rtbassoc-0738393cba67fe3bc)
aws_route_table_association.terraform_example_rt_public-a: Destruction complete after 0s
aws_route_table_association.terraform_example_rt_public-c: Destruction complete after 0s
aws_subnet.terraform_example_subnet_public-c: Destroying... (ID: subnet-04568b62453decefa)
aws_route_table.terraform_example_public_rt: Destroying... (ID: rtb-0f6bf85f04c90ecef)
aws_subnet.terraform_example_subnet_public-d: Destruction complete after 1s
aws_route_table.terraform_example_public_rt: Destruction complete after 1s
aws_internet_gateway.terraform_example_igw: Destroying... (ID: igw-031a1b3bb9b08caaf)
aws_subnet.terraform_example_subnet_public-c: Destruction complete after 1s
aws_eip.terraform_example_eip: Destruction complete after 1s
aws_instance.terraform_example_ec2: Destroying... (ID: i-052a7a262f0fec3e1)
aws_internet_gateway.terraform_example_igw: Still destroying... (ID: igw-031a1b3bb9b08caaf, 10s elapsed)
aws_instance.terraform_example_ec2: Still destroying... (ID: i-052a7a262f0fec3e1, 10s elapsed)
aws_internet_gateway.terraform_example_igw: Destruction complete after 10s
aws_instance.terraform_example_ec2: Still destroying... (ID: i-052a7a262f0fec3e1, 20s elapsed)
aws_instance.terraform_example_ec2: Still destroying... (ID: i-052a7a262f0fec3e1, 30s elapsed)
aws_instance.terraform_example_ec2: Still destroying... (ID: i-052a7a262f0fec3e1, 40s elapsed)
aws_instance.terraform_example_ec2: Still destroying... (ID: i-052a7a262f0fec3e1, 50s elapsed)
aws_instance.terraform_example_ec2: Still destroying... (ID: i-052a7a262f0fec3e1, 1m0s elapsed)
aws_instance.terraform_example_ec2: Destruction complete after 1m1s
aws_subnet.terraform_example_subnet_public-a: Destroying... (ID: subnet-0d1c7eed2ead4e307)
aws_security_group.terraform_example_sg: Destroying... (ID: sg-0e1dbb4daad93fc80)
aws_subnet.terraform_example_subnet_public-a: Destruction complete after 1s
aws_security_group.terraform_example_sg: Destruction complete after 1s
aws_vpc.terraform_example_vpc: Destroying... (ID: vpc-084056faf393b50f1)
aws_vpc.terraform_example_vpc: Destruction complete after 0s

Destroy complete! Resources: 11 destroyed.

まとめ

TerraformのセットアップとAWSのVPC/EC2作成用テンプレートを作成して削除するまでをやってみた。
慣れればCloudFormationより使いやすいかもしれない。

サンプルコードをGithubに挙げておいた。

参考

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です