2
2

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でVPCエンドポイントを一気に作る

Posted at

はじめに

  • Terraformの勉強記録です

  • 本稿ではVPCエンドポイントを一気に作ります

  • 今回は例としてSSMへのアクセスに用いる3つのVPCエンドポイントを作成します

  • 誤りの訂正・より良い方法のご助言、どしどしコメント頂ければ幸いです

Terraform 実行環境

  • AWS Cloud9
    • Amazon Linux2
    • t2.micro
  • Cloud9を使う理由
    • 標準でTerraformが備わっているため

アウトライン

  1. 変数の定義
  2. VPC・サブネット・セキュリティグループの作成
  3. VPCエンドポイントの一括作成

### 1. 変数の定義

  • vpc_endpoints作成したいVPCエンドポイント名を要素にもつリストを定義しています
variable.tf
locals {
  name = "terraform"
  region   = "ap-northeast-1"
}

# VPC
variable "vpc_cidr" {
  type    = string
  default = "10.1.0.0/16"
}

# Subnet
variable "subnets" {
  type = map(any)
  default = {
    private_subnets = {
      private-1a = {
        name = "private-1a",
        cidr = "10.1.10.0/24",
        az   = "ap-northeast-1a"
      },
      private-1c = {
        name = "private-1c",
        cidr = "10.1.11.0/24",
        az   = "ap-northeast-1c"
      },
      private-1d = {
        name = "private-1d",
        cidr = "10.1.12.0/24",
        az   = "ap-northeast-1d"
      },
    },
  }
}

# VPC Endpoint
variable "vpc_endpoints" {
  type    = list(any)
  default = ["ssm", "ssmmessages", "ec2messages"]
}

### 2. VPC・サブネット・セキュリティグループの作成

  • VPC
vpc.tf
resource "aws_vpc" "main" {
  cidr_block = var.vpc_cidr

  instance_tenancy     = "default"
  enable_dns_hostnames = true
  enable_dns_support   = true

  tags = {
    Name = "${local.name}-vpc"
  }
}
  • サブネット
subnet.tf
resource "aws_subnet" "private" {
  for_each = var.subnets.private_subnets

  vpc_id = aws_vpc.main.id

  cidr_block        = each.value.cidr
  availability_zone = each.value.az

  tags = {
    Name = "${local.name}-${each.value.name}"
  }
}
  • セキュリティグループ
security_group.tf
resource "aws_security_group" "vpc_endpoint" {
  name        = "vpc_endpoint-sg"
  description = "vpc_endpoint-sg"
  vpc_id      = aws_vpc.main.id

  ingress {
    description = "HTTPS from VPC"
    from_port   = 443
    to_port     = 443
    protocol    = "tcp"
    cidr_blocks = [aws_vpc.main.cidr_block]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  tags = {
    Name = "${local.name}-vpc_endpoint-sg"
  }
}

### 3. VPCエンドポイントの一括作成

  • var.vpc_endpointsの要素をfor_eachで回す
vpc_endpoints.tf
resource "aws_vpc_endpoint" "interface" {
  for_each = toset(var.vpc_endpoints)

  vpc_id              = aws_vpc.main.id
  service_name        = "com.amazonaws.${local.region}.${each.value}"
  vpc_endpoint_type   = "Interface"
  private_dns_enabled = true

  # privateサブネットのidをfor式で一括取得
  subnet_ids = [for sn in aws_subnet.private : sn.id]
  security_group_ids = [
    aws_security_group.vpc_endpoint.id,
  ]
  depends_on = [
    aws_vpc.main, aws_subnet.private
  ]
  tags = {
    Name = "${local.name}-${each.value}-endpoint"
  }
}
  • 完成

おわりに

  • 無事にスッキリしたコードでTerraformでVPCエンドポイントを一気に作ることができました
  • ネタがあればまた記事を書こうと思います
2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?