LoginSignup
37
39

More than 5 years have passed since last update.

terraform使い方(超基礎編)

Last updated at Posted at 2015-05-01

これだけは覚えておこう

  • plan: 設定ファイルが正しいかチェックしてくれる
  • apply: チェックの通った設定ファイルを本番のAWS環境に適用する

plan / apply

aws.tfを作成する

aws.tf
provider "aws" {
  access_key = "ACCESS_KEY"
  secret_key = "SECRET_KEY"
  region = "REGION"
}

resource "aws_instance" "example" {
  ami = "ami-cbf90ecb"
  instance_type = "t2.micro"
}

providerには

  • heroku
  • cloudFlare
  • aws

などなどが入る。

terraform plan

これで先ほど作成したaws.tfが正しいかどうかをチェックしれくれます。
このチェックが通ればterraform.tfstateが生成され、

terraform apply

でAWS上に指定したインスタンスが生成される。

設定ファイルにおける変数について

tfファイル内にvariableで指定する場合

先ほどのaws.tf内でaccess_keysecret_keyinstance_typeを変数に持ちたいときは以下のようにします。

aws.tf

aws.tf

variable "access_key" {
  default     = "ACCESSKEY"
}

variable "secret_key" {
  default     = "SECRET_KEY"
}

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

variable "instance_type" {
  default = "t2.micro"
}

variable "ami" {
  default = "ami-cbf90ecb"
}

provider "aws" {
  access_key = ${var.aws_access_key}
  secret_key = ${var.secret_key}
  region = ${var.region}
}

resource "aws_instance" "example" {
  ami = ${var.ami}
  instance_type = ${var.instance_type}
}

こんな感じになります。

例えば、amiをregionに応じて指定したい場合は以下のようにします。

variable "amis" {
    default = {
        us-east-1 = "ami-aa7ab6c2"
        us-west-2 = "ami-23f78e13"
        ap-northeast-1 = "ami-cbf90ecb"
    }
}

そして、amiの指定の際はこのように書きます。

resource "aws_instance" "example" {
  ami = "${var.amis.ap-northeast-1}"
  instance_type = "${var.instance_type}"
}

ap-north-eastって記載しなくても立ち上げる際のregionを読んでくれよ!って時があると思います。
すなわち、variable "region"の値を読んでくれよ!って時はこのようにします。

resource "aws_instance" "example" {
  ami = "${lookup(var.amis, var.region)}"
  instance_type = "${var.instance_type}"
}

全体のファイルはこのようになります。

aws.tf
variable "access_key" {
  description = "AWS access key"
  default     = "ACCESSKEY"
}

variable "secret_key" {
  description = "AWS secret access key"
  default     = "SECRETKEY"
}

variable "region" {
  description = "AWS region to host your network"
  default     = "ap-northeast-1"
}

variable "instance_type" {
  default = "t2.micro"
}

variable "amis" {
  default = {
    "us-east-1" = "ami-aa7ab6c2"
    "us-west-2" = "ami-23f78e13"
    "ap-northeast-1" = "ami-cbf90ecb"
  }
}

provider "aws" {
  access_key = "${var.access_key}"
  secret_key = "${var.secret_key}"
  region = "${var.region}"
}

resource "aws_instance" "example" {
  ami = "${lookup(var.amis, var.region)}"
  instance_type = "${var.instance_type}"
}

これで

terraform plan

terraform apply

で反映されます。

設定ファイルの変数を外部に持つ場合

これでうまくいきます。
http://qiita.com/kohey18/items/38400d8c498baa0a0ed8#comment-6ac1fc69503bd4bc9151

--------- 以下、面倒なやり方です ---------

tfvarsファイルを生成します。

config.tfvars

access_key="ACCESS_KEY"
secret_key="SECRET_KEY"
region="ap-northeast-1"

これでaws.tfをこのようにします。

variable "access_key" {
  description = "AWS access key"
}

variable "secret_key" {
  description = "AWS secret access key"
}

variable "region" {
  description = "AWS region to host your network"
}

variable "instance_type" {
  default = "t2.micro"
}

variable "amis" {
  default = {
    "us-east-1" = "ami-aa7ab6c2"
    "us-west-2" = "ami-23f78e13"
    "ap-northeast-1" = "ami-cbf90ecb"
  }
}

provider "aws" {
  access_key = "${var.access_key}"
  secret_key = "${var.secret_key}"
  region = "${var.region}"
}

resource "aws_instance" "example" {
  ami = "${lookup(var.amis, var.region)}"
  instance_type = "${var.instance_type}"
}

そして、設定ファイルが正しいかどうかを通すplanに作成した変数定義ファイルを指定する場合はこうします。

terraform plan --var-file=./config.tfvars

でおっけいです。

ちなみにconfig.tfvarsで指定できるのは、variableブロックに指定したものだけです。

variable "region" {}とaws.tfで指定せずに、config.tfvarsregion="ap-northeast-1"と指定しても

'region'. define it with 'variable' blocks

というエラーを吐きます。

以上で超基礎編を終えます。
次はVPCの構築をterraformで行ってみようと思います。

37
39
2

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
37
39