1
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?

More than 1 year has passed since last update.

Tarraformを使ってAWSにWebサーバを構築 #5 ~セキュリティグループ(ファイアウォール)を設定する~

Last updated at Posted at 2023-07-03

目的

EC2にNginxがインストールされた状態を作り出し、ブラウザからデフォルトページが確認できることをゴールとする。
インフラ構築はTerraformでコード化することで再現性、再利用性、速度などが手作業に比べると高い。
GitHubリンク

手順

  1. TerraformからAWSリソースを操作できるように設定する
  2. Terraformをインストールする
  3. TerraformでEC2サーバを立ち上げてみる
  4. ネットワークを整える(VPC, サブネット, ルーティングテーブル,インターネットゲートウェイ)
  5. セキュリティグループ(ファイアウォール)を設定する
  6. Webサーバ用EC2を立ち上げる
  7. Webサーバが立ち上がる際にWebサーバソフトをインストールするように設定する

今回の到達点

セキュリティグループ(ファイアウォール)を設定する

Webサーバを立てる準備

  • ネットワークを構築する
  • ネットワークに許可する通信を設定する ←今回はここ
  • EC2をプライベートネットワークに所属させて外部からアクセスできるようにする
  • EC2にWebサーバソフト(今回はNginx)をインストールする
Step1. security_group.tfの作成

セキュリティグループに関する設定をするファイルを作成する。
SSH, HTTP, HTTPSのインバウンド、HTTP,HTTPSのアウトバウンドを許可するような設定をする。
SSHインバウンドは管理のため。
HTTPとHTTPSのインバウンドはブラウザから閲覧するため。
HTTPとHTTPSのアウトバウンドはNginxをインストールするため。
C:\Users[ユーザ名]\Documents\MyProject\terraform\security_group.tf

security_group.tf
# Web server security group
resource "aws_security_group" "web_sg" {
  name        = "${var.project}-${var.environment}-web-sg"
  description = "web server nginx security group"
  vpc_id      = aws_vpc.vpc.id

  tags = {
    Name    = "${var.project}-${var.environment}-web-sg"
    Project = "${var.project}"
    Env     = "${var.environment}"
  }
}

resource "aws_security_group_rule" "web_in_http" {
  security_group_id = aws_security_group.web_sg.id
  type              = "ingress"
  protocol          = "tcp"
  from_port         = 80
  to_port           = 80
  cidr_blocks       = ["0.0.0.0/0"]
}

resource "aws_security_group_rule" "web_in_https" {
  security_group_id = aws_security_group.web_sg.id
  type              = "ingress"
  protocol          = "tcp"
  from_port         = 443
  to_port           = 443
  cidr_blocks       = ["0.0.0.0/0"]
}

resource "aws_security_group_rule" "web_out_http" {
  security_group_id = aws_security_group.web_sg.id
  type              = "egress"
  protocol          = "tcp"
  from_port         = 80
  to_port           = 80
  cidr_blocks       = ["0.0.0.0/0"]
}

resource "aws_security_group_rule" "web_out_https" {
  security_group_id = aws_security_group.web_sg.id
  type              = "egress"
  protocol          = "tcp"
  from_port         = 443
  to_port           = 443
  cidr_blocks       = ["0.0.0.0/0"]
}

resource "aws_security_group_rule" "web_in_ssh" {
  security_group_id = aws_security_group.web_sg.id
  type              = "ingress"
  protocol          = "tcp"
  from_port         = 22
  to_port           = 22
  cidr_blocks       = ["0.0.0.0/0"]
}

image.png

1
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
1
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?