オーストラリアで勉強してきたMLデザイナーの口語自由詩

主に、データ分析・機械学習・ベイズ・統計について自由に書く。

Terraform Getting Startedをやってみた

Terraform Getting Startedをやってみた

Terraformの Getting Started をやったメモ。

What is Terraform?

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions.

The key features:

  • Infrastructure as Code: インフラをコードとして定義
  • Execution Plans: 実行計画の作成
  • Resource Graph: リソースグラフによる依存の管理。平行処理による効率的なインフラ構築が可能
  • Change Automation: (自動)変更管理。最小の手動制御で変更を設定することが可能

Use Cases

  • Heroku App Setup
    • Herokuのセットアップ
  • Multi-Tier Applications
    • マルチレイヤーのアーキテクチャのセットアップ(例: Database -> Routing -> Caching -> API
    • 依存性を確認して自動的に構築(例: Databaseを構築してからAPIを構築するなど)
  • Self-Service Clusters
    • 中央集権的にインフラを管理する
  • Software Demos
    • デモ
  • Disposable Environments
    • 複数の同一環境を構築・メンテ可能(例: テスト、検証、QA etc)
  • Software Defined Networking
    • Software Defined Networking (SDN) の構築
  • Resource Schedulers
    • リソースの動的配備(例: Docker, Hadoop, Spark etc)
  • Multi-Cloud Deployment

Getting Started

see: Installing Terraform | Terraform - HashiCorp Learn

Download Terraform

see: Download Terraform - Terraform by HashiCorp

$ cd /var/tmp
$ wget https://releases.hashicorp.com/terraform/0.12.2/terraform_0.12.2_darwin_amd64.zip
$ unzip terraform_0.12.2_darwin_amd64.zip
$ rm -f terraform_0.12.2_darwin_amd64.zip
$ ls -l terraform
-rwxrwxr-x  1 yuki  staff  54228088  6 13 05:12 terraform

Move terraform to PATH directory

$ cd /var/tmp
$ mv terraform /usr/local/bin
$ which terraform
/usr/local/bin/terraform
$ terraform version
Terraform v0.12.2

Initialization

see: Build Infrastructure | Terraform - HashiCorp Learn

provider "aws" {
  profile = "default"
  region  = "us-east-1"
}

resource "aws_instance" "example" {
  ami           = "ami-2757f631"
  instance_type = "t2.micro"
}
$ terraform init

Initializing the backend...

Initializing provider plugins...
- Checking for available provider plugins...
- Downloading plugin for provider "aws" (terraform-providers/aws) 2.15.0...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.aws: version = "~> 2.15"

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

Apply Changes

see: Build Infrastructure | Terraform - HashiCorp Learn

$ terraform apply

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_instance.example will be created
  + resource "aws_instance" "example" {
    (中略)
    }

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

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: [yesを入力]

インスタンスの現在のステートを確認

$ terraform show
# aws_instance.example:
resource "aws_instance" "example" {
    (中略)
}

Change Infrastructure

see: Change Infrastructure | Terraform - HashiCorp Learn

resource部分を以下に書き換える

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

変更を適用

$ terraform apply
aws_instance.example: Refreshing state... [id=i-xxxxxxxxxxxxxx]

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

Terraform will perform the following actions:

  # aws_instance.example must be replaced
-/+ resource "aws_instance" "example" {
      ~ ami                          = "ami-2757f631" -> "ami-b374d5a5" # forces 
    (中略)
    }

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

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: [yesを入力]

consoleを確認すると、最初に作成したインスタンスがterminatedされ、新しいインスタンスが起動されていることがわかる(※ ami の変更のため、変更というよりはインスタンスの削除・新規作成になっている)

f:id:yukinagae:20190729232439p:plain

Destroy Infrastructure

see: Destroy Infrastructure | Terraform - HashiCorp Learn

$ terraform destroy
aws_instance.example: Refreshing state... [id=i-xxxxxxxxx]

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

Terraform will perform the following actions:

  # aws_instance.example will be destroyed
  - resource "aws_instance" "example" {
      - ami                          = "ami-b374d5a5" -> null
    (中略)
    }

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

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: [yesを入力]

- ami = "ami-b374d5a5" -> null とあるように、ami をnullに設定するのと同義みたいです。

console上でみてもterminatedされていますね。

f:id:yukinagae:20190729232416p:plain

Resources