LoginSignup
0
1

More than 1 year has passed since last update.

Terraformを使ってコンテナ経由でAWSのインフラを構築しよう!

Last updated at Posted at 2023-01-03

前提

  • AWSを使用
  • AWSのアカウントを作成済み
  • AWS Vaultをインストールおよび設定済み

AWS Valutをまだインストールおよび設定できてない方は以下の公式ドキュメントと記事を参考にしてください

なぜコンテナを使うのか?

複数プロジェクトで開発することが想定されるのでプロジェクトによってTerraformのバージョンが違うかと思います
そのため、ローカル上ではなく、プロジェクトごとにterraformのコンテナを作成し、コンテナ経由でTerraformを使う方がいいと考えています
今回はチュートリアルに従ってt2.microのEC2インスタンスを作成します

ディレクトリ構成

tree
.
├── .gitignore
└── infra
    ├── docker-compose.yml
    └── main.tf

.gitignore

下記公式サイトからterraformの.gitignoreの内容をコピーします

infra/docker-compose.yml

以下のようにdocker-compose.ymlを記載します

使用するimageはhashicorp/terraform:1.3.6です

docker-compose.yml
version: '3.9'

services:
  terraform:
    container_name: terraform
    image: hashicorp/terraform:1.3.6
    # M1チップでも動くように
    platform: linux/x86_64
    volumes:
      - .:/infra
    working_dir: /infra
    environment:
      # AWS_ACCESS_KEY_IDとAWS_SECRET_ACCESS_KEYを環境変数として使用
      - AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
      - AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
      # MFAを使うため、AWS_SESSION_TOKENも環境変数として使用
      - AWS_SESSION_TOKEN=${AWS_SESSION_TOKEN}
# 永続Volumeを作成
volumes:
  infra:

infra/main.tf

今回は公式ドキュメントのチュートリアルに記載していた内容をほぼそのまま使用します

infra/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-0bba69335379e17f8"
  instance_type = "t2.micro"

  tags = {
    Name = "ExampleAppServerInstance"
  }
}

Terraformを使ってインフラを構築してみよう!

アクセスキーの期限を調整

まだアクセスキーを調整してない方は--durationを実行しましょう

aws-vault exec shun198 --duration=12h

Terraformの初期設定

以下のコマンドを実行してTerraformの初期設定を行います

docker-compose -f infra/docker-compose.yml run --rm terraform init

以下のログが出たら成功です

Initializing the backend...

Initializing provider plugins...
- Finding hashicorp/aws versions matching "~> 4.16"...
- Installing hashicorp/aws v4.48.0...
- Installed hashicorp/aws v4.48.0 (signed by HashiCorp)

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

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.

気になる方はterraform fmtコマンドを実行してmain.tfのフォーマットを修正しましょう

docker-compose -f infra/docker-compose.yml run --rm terraform fmt
main.tf

main.tfが有効かどうかvalidateコマンドで確認します

docker-compose -f infra/docker-compose.yml run --rm terraform validate
Success! The configuration is valid.

AWSに適用される変更をplanコマンドで確認します

docker-compose -f infra/docker-compose.yml run --rm 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:

EC2インスタンスを作成

AWSにmain.tfの設定を適用します
今回は-auto-approveを実行してyesを自動的に入力します

docker-compose -f infra/docker-compose.yml run --rm terraform apply -auto-approve

以下のログが出たら成功です

Plan: 1 to add, 0 to change, 0 to destroy.
aws_instance.app_server: Creating...
aws_instance.app_server: Still creating... [10s elapsed]
aws_instance.app_server: Still creating... [20s elapsed]
aws_instance.app_server: Still creating... [30s elapsed]
aws_instance.app_server: Creation complete after 32s [id=i-01d6eeab4d9a96cb4]

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

コンソール画面からEC2インスタンスが作成されたことが確認できました
スクリーンショット 2023-01-03 11.41.33.png

作成したインフラを削除しよう

EC2インスタンスを削除します

docker-compose -f infra/docker-compose.yml run --rm terraform destroy

yesを入力します

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

以下のログが出たら成功です

aws_instance.app_server: Destroying... [id=i-01d6eeab4d9a96cb4]
aws_instance.app_server: Still destroying... [id=i-01d6eeab4d9a96cb4, 10s elapsed]
aws_instance.app_server: Still destroying... [id=i-01d6eeab4d9a96cb4, 20s elapsed]
aws_instance.app_server: Destruction complete after 30s

Destroy complete! Resources: 1 destroyed.

コンソール画面からEC2インスタンスが削除されたことが確認できました
スクリーンショット 2023-01-03 11.44.45.png

Makefileを使ってコマンドを簡潔にしよう

コマンドが長いのでMakefileを作成します
例えばフォーマットを整えたいときは

make fmt

と打ったたけで

docker-compose -f infra/docker-compose.yml run --rm terraform fmt

を打ったことになるのでとても楽になります

RUN_TERRAFORM = docker-compose -f infra/docker-compose.yml run --rm terraform
IAM_USER = shun198
DURATION = 12h

vault:
	aws-vault exec $(IAM_USER) --duration=$(DURATION)

init:
	$(RUN_TERRAFORM) init

fmt:
	$(RUN_TERRAFORM) fmt

validate:
	$(RUN_TERRAFORM) validate

show:
	$(RUN_TERRAFORM) show

apply:
	$(RUN_TERRAFORM) apply -auto-approve

graph:
	$(RUN_TERRAFORM) graph | dot -Tsvg > graph.svg

destroy:
	$(RUN_TERRAFORM) destroy

記事の紹介

以下の記事も書きましたので興味があれば読んでいただけると幸いです

参考

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