LoginSignup
3
0

More than 3 years have passed since last update.

ケース別Terraformでリソース作成

Last updated at Posted at 2019-12-15

この記事はチームラボエンジニアリングアドベントカレンダー16日目の記事です。

はじめに

Terraformを使用する際のケース別の利用方法をまとめてみました

バージョン情報

Terraform v0.11.13

実行方法

  • 実行ディレクトリに移動
cd aws_terraform
  • 秘密鍵を配置 awsディレクトリ配下に秘密鍵配置 秘密鍵は何かと同じ秘密鍵でも良い
/aws_terraform/test-key.pem
  • AWSで発行したIAMユーザーのアクセスキーとシークレットアクセスキーをaws prifileに設定する
sudo aws configure --profile [IAMユーザー名]
  • providerに作成したIAMユーザーのユーザー名を入れる
variable "region" {}

provider "aws" {

  profile = "[IAMユーザー名]"
  region     = "${var.region}"
}
  • Terraform初期化
terraform init

.terraformディレクトリが作成される

Terraformを動かしてみる

awsディレクトリに移動、planをみる

terraform plan

実行する AWS上にリソースが作成される

terraform apply

Terraformで作成したリソースを削除する

terraform destroy

ケース別利用方法

tfvarsファイル

ケース:EC2の秘密鍵をdev環境と本番環境で別のものを利用したい

dev環境

terraform/aws_terraform/develop.tfvars


key_name   = "test-key"

terraform/aws_terraform/variables.tf


# 秘密鍵
variable "key_name" {}

terraform/aws_terraform/ec2.tf


resource "aws_instance" "tfvars_test" {
  ami                    = "ami-da9e2cbc"
  instance_type          = "t2.micro"
  key_name               = "${var.key_name}"
  subnet_id              = "subnet-05ea87c24c647c898"
  vpc_security_group_ids = ["${aws_security_group.test-terraform-ec2-security.id}"]

  tags {
    Name = "test-terraform-ec2"
  }
}

実行

terraform apply -target=aws_instance.tfvars_test -var-file=develop.tfvars

オプション
-target: 作成するリソースを指定する
-var-file: 環境ファイルを指定する

本番環境

terraform/aws_terraform/production.tfvars


key_name   = "prod-key"

terraform/aws_terraform/variables.tf


# 秘密鍵
variable "key_name" {}

terraform/aws_terraform/ec2.tf


# devと同じ

実行

terraform apply -target=aws_instance.tfvars_test -var-file=production.tfvars

dataリソース

ケース:Terraformで作成するEC2インスタンスで既に作成されているサブネットを利用したい

dev環境

terraform/aws_terraform/data.tf
->Nameタグと一致するものを検索している


data "aws_subnet" "public-a" {
  filter {
    name   = "tag:Name"
    values = ["${var.subnet_id_public_a_name}"]
  }
}

terraform/aws_terraform/ec2.tf


resource "aws_instance" "data_test" {
  ami                    = "ami-da9e2cbc"
  instance_type          = "t2.micro"
  key_name               = "${var.key_name}"
  subnet_id              = "${data.aws_subnet.public-a.id}" # データリソースを使用している
  vpc_security_group_ids = ["${aws_security_group.test-terraform-ec2-security.id}"]

  tags {
    Name = "test-terraform-ec2"
  }
}

terraform/aws_terraform/develop.tfvars


key_name   = "test-key"
subnet_id_public_a_name = "test-public-1a"

terraform/aws_terraform/variables.tf


# 秘密鍵
variable "key_name" {}

# EC2 サブネット名
variable "subnet_id_public_a_name" {}

dev環境 実行

terraform apply -target=aws_instance.data_test -var-file=develop.tfvars

本番環境

terraform/aws_terraform/production.tfvars


key_name   = "prod-key"
subnet_id_public_a_name = "prod-public-1a"

実行

terraform apply -target=aws_instance.data_test -var-file=production.tfvars

ワークスペース workspace

ケース:devとprodでそれぞれインスタンス立っているか分からなくなったので確認したい

ワークスペース作成

  • ワークスペース一覧 現在のワークスペースが何かも知れる
terraform workspace list
  • ワークスペース作成
terraform workspace new develop
  • ワークスペース選択
terraform workspace select develop
  • ワークスペース削除
terraform workspace delete develop

dev環境のワークスペースでec2作成

ケース:

terraform apply -target=aws_instance.workspace_test -var-file=develop.tfvars

今までdefautのワークススペースで作成してきたので
developワークスペースではvpcごと新規作成される

条件分岐&カウント

ケース:dev環境でEC2を1台たてたいけど、本番環境ではEC2をたてたくない

dev環境

terraform/aws_terraform/ec2.tf


# 条件分岐&カウントテスト
resource "aws_instance" "count_test" {
  count                  = "${var.env == "production" ? 1 : 0}"
  ami                    = "ami-da9e2cbc"
  instance_type          = "t2.micro"
  key_name               = "${var.key_name}"
  subnet_id              = "${data.aws_subnet.public-a.id}"
  vpc_security_group_ids = ["${aws_security_group.test-terraform-ec2-security.id}"]

  tags {
    Name = "test-terraform-ec2"
  }
}

terraform/aws_terraform/develop.tfvars


env = "develop"
key_name   = "test-key"
subnet_id_public_a = "subnet-05ea87c24c647c898"
subnet_id_public_a_name = "test-public-1a"

terraform/aws_terraform/variables.tf


# 環境名
variable "env" {}

# 秘密鍵
variable "key_name" {}

# EC2 サブネットID
variable "subnet_id_public_a" {}

# EC2 サブネット名
variable "subnet_id_public_a_name" {}

実行

terraform apply -target=aws_instance.count_test -var-file=develop.tfvars

リソース作成されない

本番環境

terraform/aws_terraform/production.tfvars


env = "production"
key_name   = "prod-key"
subnet_id_public_a = "subnet-0310cf72d3e2a73ad"
subnet_id_public_a_name = "prod-public-1a"

実行

terraform apply -target=aws_instance.count_test -var-file=production.tfvars

リソース作成される

さいごに

ワークスペースで分けるより、ディレクトリで分ける意見が多そうなので
環境ごとに分ける場合は、ディレクトリで分けようかと思います。
またモジュールについてもまとめていきたいです。

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