LoginSignup
4
4

More than 3 years have passed since last update.

ECRにpushしたコンテナをECSFargateで使うVPCエンドポイントTerraform例

Posted at

必要なエンドポイント

起動タイプに「Fargate」を利用している場合、ECRにあるコンテナを使うためには以下の3つのエンドポイントを作成する必要がある

  1. ecr.dkr
    dockerコマンドを実行するためのエンドポイント

  2. s3
    コンテナイメージをpullするためのエンドポイント

  3. logs
    CloudWatchLogsへログを送信するために必要なエンドポイント
    awslogs(CloudwatchLogs)を利用する場合にのみ必要

エンドポイントのタイプが"Interface"の場合、エンドポイントのセキュリティーグループを作成し、ECSセキュリティーグループから443ポートの通信を許可する必要がある

参考:
Systems Manager を使用したインターネットアクセスなしでのプライベート EC2 インスタンスの管理
エンドポイントを使用してプライベートサブネットでECSを使用する

Terraform例

/*
  VPC Endpoint for S3

  Endpoint for pulling container image.
 */
resource "aws_vpc_endpoint" "s3" {
  vpc_id            = aws_vpc.default.id
  service_name      = data.aws_vpc_endpoint_service.s3.service_name
  vpc_endpoint_type = "Gateway"
}

resource "aws_vpc_endpoint_route_table_association" "private_s3" {
  vpc_endpoint_id = aws_vpc_endpoint.s3.id
  route_table_id  = element(aws_route_table.private.*.id, count.index)
}

data "aws_vpc_endpoint_service" "s3" {
  service = "s3"
}

/*
  VPC Endpoint for ECR DKR

  Endpoint for executing docker command.
 */
resource "aws_vpc_endpoint" "dkr" {
  vpc_id            = aws_vpc.default.id
  service_name      = data.aws_vpc_endpoint_service.dkr.service_name
  vpc_endpoint_type = "Interface"

  security_group_ids  = var.ecr_dkr_endpoint_security_group_ids
  subnet_ids          = aws_subnet.private.*.id
  private_dns_enabled = true

  tags = var.tags
}

data "aws_vpc_endpoint_service" "dkr" {
  service = "ecr.dkr"
}

/*
  VPC Endpoint for logs

  Endpoint for sending logs to CloudWatch Logs
 */
resource "aws_vpc_endpoint" "logs" {
  vpc_id            = aws_vpc.default.id
  service_name      = data.aws_vpc_endpoint_service.logs.service_name
  vpc_endpoint_type = "Interface"

  security_group_ids  = var.logs_endpoint_security_group_ids
  subnet_ids          = aws_subnet.private.*.id
  private_dns_enabled = true

  tags = var.tags
}

data "aws_vpc_endpoint_service" "logs" {
  service = "logs"
}
4
4
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
4
4