📌 概要
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"
}
[参考リポジトリ]
📌 実行
準備ができたらコマンド実行しましょう!
terraform init
terraform plan
terraform apply
Terraform関連のコマンドについては以下の記事を参考にしてください ☺️