NAT Gatewayを使わずにFargateを動かす
やりたいこと
Private SubnetにおいたFargateからECRやS3へ NAT Gatewayを使わずに アクセスする方法。
構成図
今回は以下のような構成にしています。
手順
Fargateを用意する
今回はフロントにALBを置いています。
VPC周りのリソースやALBについては図の通りなので省略します。
VPCエンドポイントを作成する
VPCエンドポイントを経由することで、ECR, CloudWatch, S3などのAWSリソースはインターネットに出ることなくFargateからアクセス可能です。
もちろん、Docker Hubなどのサービスはインターネットに出る必要があるのでNAT GatewayなりNATのインスタンスを立てるなりする必要があります。
コード例
Terraformでの例を載せておきます。
resource "aws_vpc_endpoint" "s3" { vpc_id = aws_vpc.this.id service_name = "com.amazonaws.${var.region}.s3" vpc_endpoint_type = "Gateway" route_table_ids = [ aws_route_table.private_1a.id ] } resource "aws_vpc_endpoint_route_table_association" "private_s3_1a" { vpc_endpoint_id = aws_vpc_endpoint.s3.id route_table_id = aws_route_table.private_1a.id } resource "aws_vpc_endpoint" "ecr_dkr_1a" { vpc_id = aws_vpc.this.id service_name = "com.amazonaws.${var.region}.ecr.dkr" vpc_endpoint_type = "Interface" subnet_ids = [aws_subnet.private_1a.id] security_group_ids = [aws_security_group.vpc_endpoint.id] private_dns_enabled = true } resource "aws_vpc_endpoint" "ecr_api_1a" { vpc_id = aws_vpc.this.id service_name = "com.amazonaws.${var.region}.ecr.api" vpc_endpoint_type = "Interface" subnet_ids = [aws_subnet.private_1a.id] security_group_ids = [aws_security_group.vpc_endpoint.id] private_dns_enabled = true } resource "aws_vpc_endpoint" "logs_1a" { vpc_id = aws_vpc.this.id service_name = "com.amazonaws.${var.region}.logs" vpc_endpoint_type = "Interface" subnet_ids = [aws_subnet.private_1a.id] security_group_ids = [aws_security_group.vpc_endpoint.id] private_dns_enabled = true } resource "aws_vpc_endpoint" "ssm_1a" { vpc_id = aws_vpc.this.id service_name = "com.amazonaws.${var.region}.ssm" vpc_endpoint_type = "Interface" subnet_ids = [aws_subnet.private_1a.id] security_group_ids = [aws_security_group.vpc_endpoint.id] private_dns_enabled = true }