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データベースを宣言するリソースファイルは、以下
// AWSのEC2インスタンスリソースを定義し、その名前をwebとする resource "aws_instance" "web" { // dataブロックで取得したAMI(Amazon Machine Image)のIDを指定 ami = "${data.aws_ami.ubuntu.id}" // varブロックで定義されたserver_instance_type変数から取得したインスタンスタイプを指定 instance_type = "${var.server_instance_type}" // インスタンスにタグを付け tags = { Name = "${var.server_tag_name}" } } // AWSのAMIデータソースを定義し、その名前をubuntuと指定 data "aws_ami" "ubuntu" { // 最新のAMIを取得することを指定 most_recent = true // AMIのフィルタリング条件を指定 filter { // フィルタ名称指定 name = "name" // フィルタの値として、Ubuntu 14.04のAMIを指定 values = ["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"] } // AMIの所有者IDを指定します。このIDはCanonical(Ubuntuの開発元)のもの owners = ["099720109477"] } // AWS上にRDS(Relational Database Service)インスタンスを作成するための設定 resource "aws_db_instance" "default" { // AWSのRDSインスタンスリソースを定義し、名前を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 init コマンド
構成ファイルの検証を行う
> 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.
- terraform plan コマンド
最後に、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
これを触ってみると、学びになる
チュートリアル