TerraformでAWSのインフラを作成してみる
概要
この記事では、AWSでEC2インスタンスをプロビジョニングする方法を説明します。
この記事は、公式ドキュメントを参考にしています。
これは、初心者を対象としています。以下に関する基本的な知識を持っていることを前提としています
- AWSの基本的な概念(EC2インスタンスの作成をします)
始める前に {#始める前に}
- Terraform CLIをインストールしていること
- AWS CLIをインストールしていること
- リソースを作成できるAWSアカウントとクレデンシャルを持っていること
準備をする {#準備をする}
TerraformがAWSにアクセスできるようにAWS CLIの名前付きプロファイルを設定します。
$ export AWS_PROFILE=your-profile-name
適当なディレクトリを作成し、その中にmain.tfという名前のファイルを作成します。
$ mkdir terraform-aws
$ cd terraform-aws
$ touch main.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
required_version = ">= 1.2.0"
}
provider "aws" {
region = "ap-northeast-1"
}
resource "aws_instance" "app_server" {
ami = "ami-0a290015b99140cd1"
instance_type = "t2.micro"
tags = {
Name = "ExampleAppServerInstance"
}
}
初期化
新しくインフラストラクチャを作成する場合、Terraformは初期化が必要です。初期化は、TerraformがAWSプロバイダーを使用するために必要なプラグインをインストールします。
$ terraform init
フォーマット、バリデーションを行う
Terraformは、ファイルのフォーマットとバリデーションを行うためのコマンドを提供しています。
$ terraform fmt
$ terraform validate
作成されるインフラストラクチャを確認する
plan
コマンドを使用して、Terraformが実行する変更を確認します。
$ terraform plan
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# aws_instance.app_server will be created
+ resource "aws_instance" "app_server" {
+ ami = "ami-0a6fd4c92fc6ed7d5"
...
Plan: 1 to add, 0 to change, 0 to destroy.
Plan: 1 to add, 0 to change, 0 to destroy.
というメッセージは、Terraformが実行する変更を示しています。addはリソースを追加することを意味し、changeはリソースを変更することを意味します。destroyはリソースを削除することを意味します。
今回は、aws_instance.app_serverリソースを追加するだけなので、addの数字が1になっており、他は0です。
インフラストラクチャを作成する
apply
コマンドを使用して、Terraformが定義したリソースを作成します。
$ terraform apply
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
ステートファイル
applyを実行すると、Terraformはステートファイルを作成します。ステートファイルは、Terraformが管理しているインフラストラクチャを追跡する唯一の方法です。
Terraformが管理しているリソースを確認するには、terraform state list
コマンドを使用します。show
コマンドを使用して、リソースの詳細を表示することもできます。
$ terraform state list
aws_instance.app_server
先ほど、作成した aws_instance.app_server
リソースが確認できます。
インフラストラクチャに変更を加える
EC2インスタンスのAMIを変更するには、main.tfファイルを編集して、新しいAMI IDを指定します。
<compare>
<code-block lang="hcl">
ami = "ami-0a6fd4c92fc6ed7d5"
</code-block>
<code-block lang="hcl">
ami = "ami-0a290015b99140cd1"
</code-block>
</compare>
apply
コマンドを使用して、Terraformが実行する変更を確認します。
terraform apply
aws_instance.app_server: Refreshing state... [id=i-08a51965f80ee7d23]
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
-/+ destroy and then create replacement
Terraform will perform the following actions:
# aws_instance.app_server must be replaced
-/+ resource "aws_instance" "app_server" {
~ ami = "ami-0a6fd4c92fc6ed7d5" -> "ami-0a290015b99140cd1" # forces replacement
~ arn = "arn:aws:ec2:ap-northeast-1:123456789101:instance/i-08a51965f80ee7d23" -> (known after apply)
~ associate_public_ip_address = true -> (known after apply)
~ availability_zone = "ap-northeast-1c" -> (known after apply)
~ cpu_core_count = 1 -> (known after apply)
~ cpu_threads_per_core = 1 -> (known after apply)
~ disable_api_stop = false -> (known after apply)
~ disable_api_termination = false -> (known after apply)
~ ebs_optimized = false -> (known after apply)
- hibernation = false -> null
...
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
Plan: 1 to add, 0 to change, 1 to destroy.
となりました。EC2が一度削除されてから再作成されることを意味します。AWS上では一度リソースを作成すると、値を変更できない場合があります。そのため、Terraformはリソースを削除してから再作成する必要があります。
yes
を入力して、変更を適用します。
無事に変更が適用されたら、ブラウザでEC2インスタンスにアクセスして、新しいAMIが適用されていることを確認します。
インフラストラクチャを削除する
destroy
コマンドを使用して、作成したインフラストラクチャを削除します。
$ terraform destroy
...
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
Next Steps
これで、AWSでインフラストラクチャを構築する方法を学びました。 次のステップとして、Terraformの基本的な構文を学び、より複雑なインフラストラクチャを構築する方法を学びましょう。
Discussion