Terraformを使用してEC2インスタンスを作成

投稿者: | 2016年8月9日

インフラリソース管理するツールTerraformを試しに使用してみました。
今回は簡単な使い方として、導入からインスタンスの作成迄の手順を紹介します。
Terraformはバイナリを配置するだけ動作させることができます。
仕組みとしては内部でAWSCLIを実行しているそうです。
まずはTerraformをダウンロードして配置します。
下記公式サイトで最新バージョンを確認できます。
https://www.terraform.io/

$ wget https://releases.hashicorp.com/terraform/0.6.16/terraform_0.6.16_linux_amd64.zip
$ unzip terraform_0.6.16_linux_amd64.zip
$ sudo mv terraform /usr/local/bin/

パスを通します。

$ vi ~/.bash_profile

下記の行を追加します。

PATH=$PATH:/usr/local/bin/terraform

設定を読み直し、terraformコマンドが使えれば導入完了です。

$ source .bash_profile
$ terraform --version

続いてTerraformの定義ファイルを作成します。
ここからは作業ディレクトリを作成しその中で行います。

$ vi aws.tf

定義ファイル(拡張子.tf)を作成し、下記の記述を行います。
内容は既存VPCにsubnetを作成し、その中にインスタンスを立てるというものです。

provider "aws" {
    access_key = "自分のACCESS_KEY_をここに"
    secret_key = "自分のSECRET_KEY_をここに"
    region = "us-east-1"
}
resource "aws_subnet" "tTest" {
    vpc_id = "vpc-XXXXXXXX"
    cidr_block = "172.XXX.XXX.XXX/24"
    availability_zone = "ap-northeast-1a"
    tags {
        Name = "tTest"
    }
}
resource "aws_instance" "tTest" {
    ami = "ami-374db956"
    instance_type = "t2.medium"
    key_name = "key"
    vpc_security_group_ids = [
        "sg-xxxxxxxx",
    ]
    subnet_id = "${aws_subnet.tTest.id}"
    tags {
        Name = "tTest"
    }
}
resource "aws_eip" "tTest" {
  instance = "${aws_instance.tTest.id}"
  vpc      = true
}

続いてリソース作成内容の確認コマンドです。

$ terraform plan

コマンドを実行すると下記のように変更内容が出力されます。

Refreshing Terraform state prior to plan...
The Terraform execution plan has been generated and is shown below.
Resources are shown in alphabetical order for quick scanning. Green resources
will be created (or destroyed and then created if an existing resource
exists), yellow resources are being changed in-place, and red resources
will be destroyed.
Note: You didn't specify an "-out" parameter to save this plan, so when
"apply" is called, Terraform can't guarantee this is what will execute.
+ aws_eip.tTest
    allocation_id:     "" => "<computed>"
    association_id:    "" => "<computed>"
    domain:            "" => "<computed>"
    instance:          "" => "${aws_instance.tTest.id}"
    network_interface: "" => "<computed>"
    private_ip:        "" => "<computed>"
    public_ip:         "" => "<computed>"
    vpc:               "" => "true"
+ aws_instance.tTest
    ami:                               "" => "ami-374db956"
    availability_zone:                 "" => "<computed>"
    ebs_block_device.#:                "" => "<computed>"
    ephemeral_block_device.#:          "" => "<computed>"
    instance_state:                    "" => "<computed>"
    instance_type:                     "" => "t2.medium"
    key_name:                          "" => "key"
    placement_group:                   "" => "<computed>"
    private_dns:                       "" => "<computed>"
    private_ip:                        "" => "<computed>"
    public_dns:                        "" => "<computed>"
    public_ip:                         "" => "<computed>"
    root_block_device.#:               "" => "<computed>"
    security_groups.#:                 "" => "<computed>"
    source_dest_check:                 "" => "true"
    subnet_id:                         "" => "${aws_subnet.tTest.id}"
    tags.#:                            "" => "1"
    tags.Name:                         "" => "tTest"
    tenancy:                           "" => "<computed>"
    vpc_security_group_ids.#:          "" => "1"
    vpc_security_group_ids.2906222716: "" => "sg-xxxxxxxx"
+ aws_subnet.tTest
    availability_zone:       "" => "ap-northeast-1a"
    cidr_block:              "" => "172.XXX.XXX.XXX/24"
    map_public_ip_on_launch: "" => "false"
    tags.#:                  "" => "1"
    tags.Name:               "" => "tTest"
    vpc_id:                  "" => "vpc-XXXXXXXX"
Plan: 3 to add, 0 to change, 0 to destroy.

下記のコマンドで実際にリソースの作成を行います。

$ terraform apply

完了すると「terraform.tfstate」ファイルが作成されます。
この中に各リソースの状態、ID等が記述されています。
以上でインスタンス作成の完了です。
以前、似たような機能を持つCloudFormationを試しました。
実際に両方触ってみたところTerraformの方が使いやすい印象でした。
事前の差分確認が簡単だったり、リソース定義ファイルの記述しやすいのが良かったです。

コメントを残す

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

CAPTCHA