Carpe Diem

備忘録

TerraformでAMIをセットアップ

概要

Terraformを使ってインスタンスを作成する方法です。

環境

  • Ubuntu 14.04
  • Terraform 0.3.7

インストール

TerraformでHerokuをセットアップを参考にしてください。

.tfファイルの作成

.tfファイルを作成します。今回はaws.tfとします。
access_keysecret_keyは自分のAWSの鍵情報を使います。

provider "aws" {
    access_key = "xxxxxxxxxxxxxxxxxxx"
    secret_key = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
    region = "ap-northeast-1"
}

resource "aws_instance" "test_instance01" {
    ami = "ami-18b6aa19"
    instance_type = "t1.micro"
}

インスタンスを作成するためには最低限以下の情報を設定します。

名称 説明
ami インスタンスIDを指定する。
インスタンスVagrantのBoxやDockerのImage
それを指定して構築するためのID
instance_type インスタンスのマシンタイプ、スペックを指定する。
無料枠はt1.micro, t2.microしか選択できない

初回のamiのIDをどこから取得するのが正しいのかはわからないのですが、とりあえずAWSのWeb上のインスタンス作成画面で見れるのでそちらから使いました。

f:id:quoll00:20150327102801p:plain

自分固有のAMIを作成した場合はそのインスタンスIDを使えます。

計画

terraform planをします。

$ 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_instance.test_instance01
    ami:                 "" => "ami-18b6aa19"
    availability_zone:   "" => "<computed>"
    block_device.#:      "" => "<computed>"
    instance_type:       "" => "t1.micro"
    key_name:            "" => "<computed>"
    private_dns:         "" => "<computed>"
    private_ip:          "" => "<computed>"
    public_dns:          "" => "<computed>"
    public_ip:           "" => "<computed>"
    root_block_device.#: "" => "<computed>"
    security_groups.#:   "" => "<computed>"
    subnet_id:           "" => "<computed>"
    tenancy:             "" => "<computed>"

<computed>は指定していないので自動で設定される項目です。

実行

terraform applyを実行します。

$ terraform apply
aws_instance.test_instance01: Creating...
  ami:                 "" => "ami-18b6aa19"
  availability_zone:   "" => "<computed>"
  block_device.#:      "" => "<computed>"
  instance_type:       "" => "t1.micro"
  key_name:            "" => "<computed>"
  private_dns:         "" => "<computed>"
  private_ip:          "" => "<computed>"
  public_dns:          "" => "<computed>"
  public_ip:           "" => "<computed>"
  root_block_device.#: "" => "<computed>"
  security_groups.#:   "" => "<computed>"
  subnet_id:           "" => "<computed>"
  tenancy:             "" => "<computed>"
aws_instance.test_instance01: Creation complete

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

The state of your infrastructure has been saved to the path
below. This state is required to modify and destroy your
infrastructure, so keep it safe. To inspect the complete state
use the `terraform show` command.

State path: terraform.tfstate

作成できました。
amiinstance_typeの組み合わせがおかしいとterraform planで成功してもterraform applyでコケますので注意。

確認

まずはコンソールで。

$ terraform show
aws_instance.test_instance01:
  id = i-02c401f7
  ami = ami-18b6aa19
  availability_zone = ap-northeast-1c
  block_device.# = 0
  instance_type = t1.micro
  key_name = 
  private_dns = ip-172-31-17-159.ap-northeast-1.compute.internal
  private_ip = 172.31.17.159
  public_dns = ec2-xx-xxx-xxx-xxx.ap-northeast-1.compute.amazonaws.com
  public_ip = xx.xxx.xxx.xxx
  root_block_device.# = 1
  root_block_device.0.delete_on_termination = true
  root_block_device.0.device_name = /dev/sda1
  root_block_device.0.volume_size = 8
  root_block_device.0.volume_type = gp2
  security_groups.# = 1
  security_groups.3814588639 = default
  subnet_id = subnet-0049b459
  tenancy = default

Webの管理画面でも新しく作られているのがわかります。 f:id:quoll00:20150327104001p:plain

ソース