Open7
TerraformでECSの環境を作成する
clusterのみ作成
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
required_version = ">= 1.2.0"
}
provider "aws" {
region = "ap-northeast-1"
}
resource "aws_ecs_cluster" "cluster1" {
name = "claster-1"
}
できるのはこんな感じ
# aws_ecs_cluster.cluster1 will be created
+ resource "aws_ecs_cluster" "cluster1" {
+ arn = (known after apply)
+ capacity_providers = (known after apply)
+ id = (known after apply)
+ name = "claster-1"
+ tags_all = (known after apply)
+ default_capacity_provider_strategy {
+ base = (known after apply)
+ capacity_provider = (known after apply)
+ weight = (known after apply)
}
+ setting {
+ name = (known after apply)
+ value = (known after apply)
}
}
こんな感じにするとキャパシティプロバイダーがfargateになる
locals {
name = "claster-1"
}
resource "aws_ecs_cluster" "cluster1" {
name = local.name
}
resource "aws_ecs_cluster_capacity_providers" "cluster1_capacity_providers" {
cluster_name = local.name
capacity_providers = ["FARGATE"]
}
デフォルトのキャパシティプロバイダーを設定していたほうが良さそうなので設定しなおす
resource "aws_ecs_cluster_capacity_providers" "cluster1_capacity_providers" {
cluster_name = local.name
capacity_providers = ["FARGATE"]
default_capacity_provider_strategy {
base = 1
weight = 100
capacity_provider = "FARGATE"
}
}
タスクを定義する。fargateの場合、ミニマムはこれになる気がする。
imageはとりあえずnginxとする
resource "aws_ecs_task_definition" "cluster-task1" {
family = "cluster-task"
requires_compatibilities = ["FARGATE"]
cpu = 256
memory = 512
network_mode = "awsvpc"
container_definitions = jsonencode([
{
name = "nginx"
image = "nginx"
cpu = 10
memory = 512
essential = true
portMappings = [
{
containerPort = 80
hostPort = 80
}
]
}
])
runtime_platform {
operating_system_family = "LINUX"
cpu_architecture = "X86_64"
}
}
serviceの定義
resource "aws_default_vpc" "default_vpc" {
}
data "aws_subnets" "default_subnets" {
filter {
name = "vpc-id"
values = [aws_default_vpc.default_vpc.id]
}
}
resource "aws_ecs_service" "service" {
name = "nginx-service"
cluster = local.name
task_definition = aws_ecs_task_definition.cluster-task1.arn
desired_count = 0
network_configuration {
subnets = toset(data.aws_subnets.default_subnets.ids)
}
}
サブネットとかを指定するのが面倒だったので、デフォルトを利用するように設定
ついでにdata周りの使い方がわかったので良かった。