Terraform環境をDockerで構築する方法

最終的なコードはこちらです。

Terraformコンテナを使用してS3に静的サイトをホスティングする所まで行います。

環境

  • Docker version20.10.10
  • Windows10
  • Terraform version 1.2.4
  • provider hashicorp/aws 4.21.0

構築方法

docker-compose.ymlを作成

docker-compose.yml

version: '3'
services:
  terraform:
    image: hashicorp/terraform:1.2.4
    env_file:
      - .env
    volumes:
      - ./terraform:/terraform
    working_dir: /terraform
    entrypoint: ash
    tty: true

環境変数を設定

.env
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=

.gitignoreを作成

AWSアクセスキー等をプッシュしてしまわないようにgitignoreを作成します。

.gitignore
.env

terraformフォルダに公式のterraform.gitignoreを配置します。

./terraform/.gitignore
# Local .terraform directories
**/.terraform/*

# .tfstate files
*.tfstate
*.tfstate.*

# Crash log files
crash.log
crash.*.log

# Exclude all .tfvars files, which are likely to contain sensitive data, such as
# password, private keys, and other secrets. These should not be part of version 
# control as they are data points which are potentially sensitive and subject 
# to change depending on the environment.
*.tfvars
*.tfvars.json

# Ignore override files as they are usually used to override resources locally and so
# are not checked in
override.tf
override.tf.json
*_override.tf
*_override.tf.json

# Include override files you do wish to add to version control using negated pattern
# !example_override.tf

# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*

# Ignore CLI configuration files
.terraformrc
terraform.rc

S3に静的ウェブサイトをホスティングしてみる

動作確認としてS3に静的ウェブサイトをホスティングします。

Terraformのバージョン、プロバイダーを指定

./terraform/provider.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "4.21.0"
    }
  }

  required_version = "1.2.4"
}

provider "aws" {
  region = "ap-northeast-1"
}

作成するS3バケット&オブジェクトを定義

./terraform/main.tf
resource "aws_s3_bucket" "website" {
  bucket_prefix = "sample"
}

resource "aws_s3_bucket_policy" "website" {
  bucket = aws_s3_bucket.website.id
  policy = <<POLICY
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": [
                "s3:GetObject"
            ],
            "Resource": [
                "arn:aws:s3:::${aws_s3_bucket.website.id}/*"
            ]
        }
    ]
}
POLICY
}

resource "aws_s3_bucket_website_configuration" "website" {
  bucket = aws_s3_bucket.website.bucket
  index_document {
    suffix = "index.html"
  }
}

resource "aws_s3_bucket_object" "index" {
  key          = "index.html"
  bucket       = aws_s3_bucket.website.id
  source       = "index.html"
  content_type = "text/html"
}

ホスティングするページを作成

./terraform/index.html
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>My Website Home Page</title>
</head>
<body>
  <h1>Welcome to my website</h1>
  <p>Now hosted on Amazon S3!</p>
</body>
</html>

Terraformを実行してホスティング

Terminal
docker-compose up -d
docker-compose exec terraform ash
terraform init
terraform apply

サイトを確認

コマンドでサイトのアドレスを確認します。

Terminal
docker-compose exec terraform ash
terraform state show aws_s3_bucket_website_configuration.website

サイトのエンドポイントが出力されるのでブラウザからアクセスします。

上記ページが表示されたら成功です。

クリーンアップ

AWSリソース及びコンテナをクリーンアップします。

Terminal
docker-compose exec terraform ash
terraform destroy
exit
docker-compose down

参考文献

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です