5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

TerraformでECSにEFSマウント

Posted at

以下をベースに

チュートリアル: Amazon ECS での Amazon EFS ファイルシステムの使用 - Amazon Elastic Container Service
https://docs.aws.amazon.com/ja_jp/AmazonECS/latest/developerguide/using_efs.html

1. クラスタ構築

1-1. EFSつくる

resource "aws_efs_file_system" "app" {
  tags = {
    Name = "app"
  }
}

resource "aws_efs_mount_target" "app" {
  count = length(module.vpc.public_subnets)
  file_system_id  = "${aws_efs_file_system.app.id}"
  subnet_id       = element(module.vpc.public_subnets, count.index)
  security_groups = ["${aws_security_group.default.id}"]
}

例。変数は適宜設定。

1-2. ECSクラスタつくる

resource "aws_ecs_cluster" "app" {
  name = "myapp"
}

1-2. ユーザーデータ

cloudinit-ecs.tpl
#!/bin/bash
# Set any ECS agent configuration options
echo "ECS_CLUSTER=${ecs_cluster_name}" >> /etc/ecs/ecs.config

↑クラスタ名設定用テンプレート

cloudinit-ecs-efs.tpl
# Install amazon-efs-utils
cloud-init-per once yum_update yum update -y
cloud-init-per once install_amazon-efs-utils yum install -y amazon-efs-utils

# Create /efs folder
cloud-init-per once mkdir_efs mkdir /efs

# Mount /efs
cloud-init-per once mount_efs echo -e '${efs_id}:/ /efs efs defaults,_netdev 0 0' >> /etc/fstab
mount -a

↑EFSマウント用テンプレート

data "template_file" "container_instance_base_cloud_config" {
  template = "${file("cloudinit-ecs.tpl")}"

  vars = {
    ecs_cluster_name = "${aws_ecs_cluster.app.name}"
  }
}

data "template_file" "container_instance_efs" {
  template = "${file("cloudinit-ecs-efs.tpl")}"

  vars = {
    efs_id = "${aws_efs_file_system.app.id}"
  }
}

data "template_cloudinit_config" "container_instance_cloud_config" {
  gzip          = true
  base64_encode = true

  part {
    content_type = "text/x-shellscript"
    content      = "${data.template_file.container_instance_base_cloud_config.rendered}"
  }

  part {
    content_type = "text/cloud-boothook"
    content      = "${data.template_file.container_instance_efs.rendered}"
  }
}

上記によりmultipartで設定される。

1-3. Launch templateにユーザーデータ設定

resource "aws_launch_template" "container_instance" {
  # ...
  user_data = "${data.template_cloudinit_config.container_instance_cloud_config.rendered}"
}

1-4. AutoScalingでEC2たてる

省略

2. ECSのTask Definitionでvolumeマウント

チュートリアルのまま。
以下チュートリアル内の例。

{
  "containerDefinitions": [
    {
      "memory": 128,
      "portMappings": [
        {
          "hostPort": 80,
          "containerPort": 80,
          "protocol": "tcp"
        }
      ],
      "essential": true,
      "mountPoints": [
        {
          "containerPath": "/usr/share/nginx/html",
          "sourceVolume": "efs-html"
        }
      ],
      "name": "nginx",
      "image": "nginx"
    }
  ],
  "volumes": [
    {
      "host": {
        "sourcePath": "/efs/html"
      },
      "name": "efs-html"
    }
  ],
  "family": "nginx-efs"
}
5
3
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
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?