気軽に楽しくプログラムと遊ぶ

自分が興味があってためになるかもって思う情報を提供しています。

Terraformとは? 概要と参考URL

Terraformとは?

  • IaC(Infrastructure as Code)を実現するためのツール
  • オープンソースで、HashCorpにより、Go言語で開発された
  • インフラ構成を構造化された構成ファイルによって、自動管理可能
  • コードを見るだけで構成内容が理解でき、コードはGit管理されるため、コードレビューが可能

ソース概要

Structure

  • Resources (main.tf...)
  • Input (variables.tf)
  • Output (outputs.tf)

main.tf

AWSを利用すること、及びバージョンとリージョンを指定します。
AWSにアクセスするにはクレデンシャル情報を指定する。

# definition provider
provider "aws" {
  version = "~> 2.0"
  region = "${var.provider_region}"
  access_key = "${var.secret_access_key}"
  secret_key = "${var.secret_key}"
}

resource.tf

UbuntuとPostgresデータベースを宣言するリソースファイルは、以下

resource "aws_instance" "web" {
  ami = "${data.aws_ami.ubuntu.id}"
  instance_type = "${var.server_instance_type}"

  tags = {
    Name = "${var.server_tag_name}"
  }
}

data "aws_ami" "ubuntu" {
  most_recent = true

  filter {
    name = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"]
  }

  owners = ["099720109477"]
}

resource "aws_db_instance" "default" {
  allocated_storage = "${var.db_allocated_storage}"
  storage_type = "${var.db_storage_type}"
  engine = "${var.db_engine}"
  engine_version = "${var.db_engine_version}"
  instance_class = "${var.db_instance_class}"
  name = "${var.db_database_name}"
  username = "${var.db_database_username}"
  password = "${var.secret_db_database_password}"
  final_snapshot_identifier = "${var.db_snapshot_identifier}"
}

var.tf

定義内に出てくる変数の設定

variable "provider_region" {description = "Provider region"default = "us-east-1"
}

variable "secret_access_key" {description = "Provider access key"default = "YOUR-ACCESS-KEY"
}

variable "secret_key" {description = "Provider secret key"default = "YOUR-SECRET-KEY"
}

# instance web variables
variable "server_instance_type" {
  description = "Server instance type"
  default = "t2.micro"
}

variable "server_tag_name" {
  description = "Server tag name"
  default = "JeSuisUnDev"
}

# instance base de données RDS postgres variables
variable "db_allocated_storage" {
  description = "Allocated storage"
  default = 20
}
variable "db_storage_type" {
  description = "Storage type"
  default = "gp2"
}

variable "db_engine" {
  description = "Storage engine"
  default = "postgres"
}

variable "db_engine_version" {
  description = "Storage engine version"
  default = "11.5"
}

variable "db_instance_class" {
  description = "Storage instance class"
  default = "db.t2.micro"
}

variable "db_database_name" {
  description = "Storage database name"
  default = "postgres"
}

variable "db_database_username" {
  description = "Storage database name"
  default = "postgres"
}

variable "secret_db_database_password" {
  description = "Storage database secret password"
  default = "postgres"
}

variable "db_snapshot_identifier" {
  description = "Storage database snapshot identifier"
  default = "postgres"
}

コマンドを実行して変更を反映

Terraformの初期化 → ファイル検証 → アクションの計画(plan) → 変更の適用(apply)

> terraform init

Initializing the backend...

Initializing provider plugins...
- Checking for available provider plugins...
- Downloading plugin for provider "aws" (hashicorp/aws) 2.48.0...

Terraform has been successfully initialized!

構成ファイルの検証を行う

> terraform validate

Success! The configuration is valid.

Planを行う。インフラ構築の作成のみを行う

> terraform plan

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_db_instance.default will be created
  # aws_instance.web will be created

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

最後に、planを適用(apply)して、実際にインフラをクラウドに構築する

> terraform apply

aws_instance.web: Creating...
aws_db_instance.default: Creating...
aws_instance.web: Still creating... [10s elapsed]
aws_instance.web: Still creating... [30s elapsed]
aws_instance.web: Creation complete after 43s [id=i-0f285230749e69a67]
aws_db_instance.default: Still creating... [50s elapsed]
aws_db_instance.default: Still creating... [1m50s elapsed]
aws_db_instance.default: Still creating... [2m50s elapsed]
aws_db_instance.default: Still creating... [3m50s elapsed]
aws_db_instance.default: Still creating... [4m0s elapsed]
aws_db_instance.default: Creation complete after 4m8s [id=terraform-20200208064624729700000001]

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

構築したインフラを破棄する

> terraform destroy

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

aws_instance.web: Destroying... [id=i-0f285230749e69a67]
aws_db_instance.default: Destroying... [id=terraform-20200208064624729700000001]
aws_instance.web: Destruction complete after 30s
aws_db_instance.default: Destruction complete after 50s

Destroy complete! Resources: 0 added, 0 changed, 2 destroyed.

Terraform 参考資料

概要理解するには、以下を読む
5分で分かるTerraform(Infrastructure as Code)

公式サイト

公式

リソースの引数の参考に
公式ドキュメント AWS Provider

これを触ってみると、学びになる
チュートリアル

参考URL