0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

TerraformでNLB構築

Last updated at Posted at 2024-12-21

はじめに

本記事はTerraformを使ったNLBの構築方法を紹介します。
今回は、シンプルにNLBとセキュリティグループをTerraformで構築してみましたので構築例をご紹介いたします。

前提条件

今回は話を簡単にするため、VPC、IGW、サブネット、EC2は作成済み。
下記構成図におけるNLBとNLBにアタッチするセキュリティグループ(SG)をTerraformで構築します。

セキュリティグループ

まずはSecurity Groupを作成します。

・security_group.tf

#セキュリティグループ本体
resource "aws_security_group" "sg_nlb" {
  vpc_id = <VPCのID>"
  name   = "nlb-alpha-sg"
}

#インバウンドルール1
resource "aws_vpc_security_group_ingress_rule" "allow_http" {
  from_port         = 80
  to_port           = 80
  ip_protocol       = "tcp"
  cidr_ipv4         = "172.31.0.11/32" #適当に許可したいIPアドレス
  security_group_id = aws_security_group.sg_nlb.id
}

#インバウンドルール2
resource "aws_vpc_security_group_ingress_rule" "allow_https" {
  from_port         = 443
  to_port           = 443
  ip_protocol       = "tcp"
  cidr_ipv4         = "172.31.0.11/32" #適当に許可したいIPアドレス
  security_group_id = aws_security_group.sg_nlb.id
}

#アウトバウンドルール1
resource "aws_vpc_security_group_egress_rule" "allow_all" {
  ip_protocol       = "-1"
  cidr_ipv4         = "0.0.0.0/0"
  security_group_id = aws_security_group.sg_nlb.id
}

NLB

続いてNLBを作成します。

・NLB.tf

# NLB本体
resource "aws_lb" "nlb" {
  name               = "nlb-alpha"
  internal           = false
  load_balancer_type = "network"
  security_groups    = [aws_security_group.sg_nlb.id]
  subnets = ["<サブネットID>"]
  
}

# NLBのターゲットグループ(HTTP)
resource "aws_lb_target_group" "tg_nlb_80" {
  name        = "tg-nlb-80"
  port        = 80
  protocol    = "TCP"
  vpc_id      = "<VPCID>"
  target_type = "instance"
}

# NLBのターゲットグループへのターゲット紐づけ(HTTP)
resource "aws_lb_target_group_attachment" "tg_nlb_attachment_80" {
  target_group_arn = aws_lb_target_group.tg_nlb_80.arn
  target_id        = "<EC2インスタンスID>"
}

# NLBのリスナー設定(NLBとターゲットグループの紐づけ)(HTTP)
resource "aws_lb_listener" "listener_nlb_80" {
  load_balancer_arn = aws_lb.nlb.arn
  port              = 80
  protocol          = "TCP"
  default_action {
    type             = "forward"
    target_group_arn = aws_lb_target_group.tg_nlb_80.arn
  }
}

# NLBのターゲットグループ(HTTPS)
resource "aws_lb_target_group" "tg_nlb_443" {
  name        = "tg-nlb-443"
  port        = 443
  protocol    = "TCP"
  vpc_id      = "<VPCID>"
  target_type = "instance"
}

# NLBのターゲットグループへのターゲット紐づけ(HTTPS)
resource "aws_lb_target_group_attachment" "tg_nlb_attachment_443" {
  target_group_arn = aws_lb_target_group.tg_nlb_443.arn
  target_id        = "<EC2インスタンスID>"
}

# NLBのリスナー設定(NLBとターゲットグループの紐づけ)(HTTPS)
resource "aws_lb_listener" "listener_nlb_443" {
  load_balancer_arn = aws_lb.nlb.arn
  port              = 443
  protocol          = "TCP"
  default_action {
    type             = "forward"
    target_group_arn = aws_lb_target_group.tg_nlb_443.arn
  }
}

構築されたリソース

・セキュリティグループ

image-1.png

image-2.png

・NLB

image-3.png

おわりに

今回の基礎編ではterraformを使って簡単なNLB、SGの構築を紹介しました。
次回は大量の外部IPアドレスからのインバウンドルールをterraformで簡単に管理する方法を紹介予定です。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?