LoginSignup
1
1

More than 5 years have passed since last update.

TerraformでEC2インスタンスを構築(実践編)

Last updated at Posted at 2019-04-14

はじめに

AWSを使ってて、毎回コンソールから環境を構築するのは面倒です。
Terraformというツールを使えば、環境をドカッと作れて、ゴソッと消せるらしいので触ってみました。

まずは、お試し感覚でAWSのEC2を構築します。

この実践編ではインストールが終わったあとの構築方法を説明しています。
インストールや初期設定についてはインストール編で説明しています。

目標

Terraformを用いたインフラの構築は以下のような流れで行うことを目標にします。

① .tfファイルにインフラのコードを書く
② terraform planコマンドで現状との差分を比較する
③ terraform applyコマンドでインフラを構成を適用する
④ terraform showコマンドで状態を確認する

今回は環境を構築したあとに、削除します。
削除は以下の流れで行います。

⑤ terraform destroyコマンドで削除
⑥ terraform showコマンドで削除されたことを確認

実践

環境構築

① .tfファイル作成

Terraformはインフラの構成情報をHCL(HaashCorp Configuration Language)というDSLで.tfファイルに記述します。

今回は、VPCとサブネットとインスタンスをresourceとして定義します。

パラメータは公式ドキュメントを見ながらやればOK。

Provider: AWS - Terraform by HashiCorp

aws_ec2_config.tf
resource "aws_instance" "linux" {
    ami = "ami-00a5245b4816c38e6"
    instance_type = "t2.micro"
    subnet_id = "${aws_subnet.public_subnet.id}"
}

resource "aws_vpc" "vpc" {
    cidr_block = "10.0.0.0/16"
    instance_tenancy = "default"
}

resource "aws_subnet" "public_subnet" {
vpc_id = "${aws_vpc.vpc.id}"
cidr_block = "10.0.10.0/24"
availability_zone = "ap-northeast-1a"
}

② terraform plan

terraform planコマンドで現在の環境と.tfファイルの差分を確認します。

今回は、VPCとサブネットとインスタンスが新規作成されるので「+」で表示されます。

$ ./terraform plan
Terraform will perform the following actions:

  + aws_instance.linux
      id:                               <computed>
      ami:                              "ami-00a5245b4816c38e6"
      arn:                              <computed>
      ・
      ・
      ・
  + aws_subnet.public_subnet
      id:                               <computed>
      arn:                              <computed>
      assign_ipv6_address_on_creation:  "false"
      ・
      ・
      ・

  + aws_vpc.vpc
      id:                               <computed>
      arn:                              <computed>
      ・
      ・
      ・

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

③ terraform apply

構成情報の確認ができたら、terraform applyコマンドで構成情報を適用します。
途中でアクションを続行するか聞かれるので「yes」を入力します。

$ ./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 complete! Resources: 3 added, 0 changed, 0 destroyed.

④ terraform show

terraform showコマンドで構成情報を確認できます。
また、AWSコンソールなどからも確認することができます。

$ ./terraform show
aws_instance.linux:
  id = i-*****************
  ami = ami-00a5245b4816c38e6
  arn = arn:aws:ec2:ap-northeast-1:************:instance/i-*****************
      ・
      ・
      ・

aws_subnet.public_subnet:
  id = subnet-*****************
  arn = arn:aws:ec2:ap-northeast-1:************:subnet/subnet-*****************
      ・
      ・
      ・

aws_vpc.vpc:
  id = vpc-*****************
  arn = arn:aws:ec2:ap-northeast-1:************:vpc/vpc-*****************
      ・
      ・
      ・

環境の削除

⑤ terraform destroy

terraform destroyコマンドで定義したリソースを削除することができます。

削除のときも、途中でアクションを続行するか聞かれるので「yes」を入力します。

$ ./terraform 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

以下のように表示されれば、処理が完了です。

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

⑥ terraform show

最後にもう一度、terraform showコマンドで現在の構成を確認します。
正常に削除できている場合は、以下のように何も表示されません。

$ ./terraform show

おわり

今回までで、TerraformのインストールとEC2インスタンスの構築までが一旦終わりました。

応用すれば、もっと複雑なインフラも構築できるはず!

また、時間があればTerraformのstateやBackendという概念についても深掘っていきたいと思います。

1
1
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
1
1