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 で AWS セキュリティグループ を作成するシンプルな手順

Last updated at Posted at 2025-02-09

📌 概要

Terraform を使って AWS のセキュリティグループ を作成する方法を解説します 📖

VPC内のリソースは

  • 外部と直接通信するリソース
  • 内部のみにアクセスを制限するリソース

に分けるのが一般的なため、パブリック用 と プライベート用 の2つのセキュリティーグループを作成していきます ⛑️

📌 事前に準備するもの

main.tf

main.tf の作成がまだの方は以下の記事を参考にファイルを作成してください。

VPC

VPCを作成していない場合は以下を参考に作成します。

📌 ディレクトリ構成

terraform-project/
│── main.tf
│── modules/
│   ├── vpc/
│   │   ├── main.tf
│   ├── security_group/
│   │   ├── main.tf

📌 コード

main.tf

AWS のプロバイダーを設定し、VPC とセキュリティグループのモジュールを読み込む。

# AWS プロバイダーの設定
provider "aws" {
  region = "ap-northeast-1" # 東京リージョン
}

# VPC モジュールの読み込み
module "vpc" {
  source = "./vpc"
}

# セキュリティグループのモジュールを読み込み

module "security_group" {
  source = "./security_group"
  vpc_id = module.vpc.vpc_id
}

vpc/main.tf

VPC(Virtual Private Cloud)を作成し、VPC の ID を出力します。

# VPC(Virtual Private Cloud)の作成
resource "aws_vpc" "main" {
  cidr_block = "192.168.250.0/24" # VPC 内で使用する IP アドレスの範囲

  tags = {
    # VPC の名前を sandbox-terraform に設定
    Name = "sandbox-terraform"
  }
}

# 作成した VPC の ID を出力
output "vpc_id" {
  value = aws_vpc.main.id
}

security_group/main.tf

パブリック(インターネット向け)とプライベート(内部向け)の 2 つのセキュリティーグループを作成します。

# VPC の ID を受け取る変数を定義
variable "vpc_id" {
  description = "Internet Gateway を関連付ける VPC の ID"
  type        = string
}

# -------------------------------- パブリックセキュリティグループ(インターネット向け)

# ----------- グループ

resource "aws_security_group" "public" {
  name        = "sandbox-terraform-public"
  description = "Security group for public EC2 instances"
  vpc_id      = var.vpc_id

  tags = {
    Name = "sandbox-terraform-public"
  }
}

# ----------- ルール

# プライベートSGからの全トラフィックを許可
resource "aws_security_group_rule" "public_private_sg" {
  type                     = "ingress"
  from_port                = 0
  to_port                  = 0
  protocol                 = "-1"
  security_group_id        = aws_security_group.public.id
  source_security_group_id = aws_security_group.private.id
  description              = "Allow all traffic from private security group"
}

# HTTPS (443) を許可
resource "aws_security_group_rule" "public_https" {
  type              = "ingress"
  from_port         = 443
  to_port           = 443
  protocol          = "tcp"
  security_group_id = aws_security_group.public.id
  cidr_blocks       = ["0.0.0.0/0"]
  description       = "Allow HTTPS from everywhere"
}

# HTTP (80) を許可
resource "aws_security_group_rule" "public_http" {
  type              = "ingress"
  from_port         = 80
  to_port           = 80
  protocol          = "tcp"
  security_group_id = aws_security_group.public.id
  cidr_blocks       = ["0.0.0.0/0"]
  description       = "Allow HTTP from everywhere"
}

# SSH (22) を許可
resource "aws_security_group_rule" "public_ssh" {
  type              = "ingress"
  from_port         = 22
  to_port           = 22
  protocol          = "tcp"
  security_group_id = aws_security_group.public.id
  cidr_blocks       = ["0.0.0.0/0"]
  description       = "Allow SSH from everywhere"
}


# --------------------------------プライベートセキュリティグループ(内部向け)

# ----------- グループ

resource "aws_security_group" "private" {
  name        = "sandbox-terraform-private"
  description = "Security group for private EC2 instances"
  vpc_id      = var.vpc_id

  tags = {
    Name = "sandbox-terraform-private"
  }
}

# ----------- ルール

# プライベートSG内の全トラフィックを許可
resource "aws_security_group_rule" "private_private_sg" {
  type                     = "ingress"
  from_port                = 0
  to_port                  = 0
  protocol                 = "-1"
  security_group_id        = aws_security_group.private.id
  source_security_group_id = aws_security_group.private.id
  description              = "Allow all traffic from private security group"
}

# パブリックSGからの全トラフィックを許可
resource "aws_security_group_rule" "private_public_sg" {
  type                     = "ingress"
  from_port                = 0
  to_port                  = 0
  protocol                 = "-1"
  security_group_id        = aws_security_group.private.id
  source_security_group_id = aws_security_group.public.id
  description              = "Allow all traffic from public security group"
}

[参考リポジトリ]

📌 実行

準備ができたらコマンド実行しましょう!

  1. terraform init
  2. terraform plan
  3. terraform apply

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?