セキュリティグループの作成

ここでは、EC2インスタンスへの通信を制御するセキュリティグループを作成します。Terraformでは、resource "aws_security_group"を使ってセキュリティグループを定義し、ingressブロックで受信ルール、egressブロックで送信ルールを設定します。

1. コードの記載

ここでは、EC2インスタンスへの通信を制御するセキュリティグループを作成します。Terraformでは、resource "aws_security_group"を使ってセキュリティグループを定義し、ingressブロックで受信ルール、egressブロックで送信ルールを設定します。以下の内容をmain.tfに追記してください。

# ====================
# セキュリティグループ
# ====================
resource "aws_security_group" "ec2" {
  name        = "terraform-ec2-sg"
  description = "Security group for EC2 instance"
  vpc_id      = aws_vpc.main.id

  # SSH接続を許可
  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
    description = "Allow SSH"
  }

  # HTTP接続を許可
  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
    description = "Allow HTTP"
  }

  # アウトバウンド通信を全て許可
  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
    description = "Allow all outbound traffic"
  }

  tags = {
    Name = "terraform-ec2-sg"
  }
}

今回設定した内容について解説します。

設定項目 値 設定の基準
name(セキュリティグループ名) terraform-ec2-sg リソースを識別するため
description(セキュリティグループの説明) Security group for EC2 instance セキュリティグループの用途を明示するため
vpc_id(セキュリティグループを作成するVPC) aws_vpc.main.id 作成したVPCに所属させるため
ingress(SSH用インバウンドルール) ポート22, TCP, 0.0.0.0/0 SSH接続を許可するため
ingress(HTTP用インバウンドルール) ポート80, TCP, 0.0.0.0/0 HTTP接続を許可するため
egress(アウトバウンドルール) 全ポート, 全プロトコル, 0.0.0.0/0 すべてのアウトバウンド通信を許可するため
tags.Name(AWSコンソールに表示される名前タグ) terraform-ec2-sg リソースを識別するため
💡 ポイント
このセクションでは簡略化のため、SSH接続を0.0.0.0/0(全世界)から許可しています。本番環境では、自分のIPアドレスのみを許可するなど、適切なセキュリティ設定を行ってください。

2. 差分の確認

terraform planで差分を確認します。

terraform plan

以下のような実行結果が表示されます。

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with
the following symbols:
  + create

Terraform will perform the following actions:

  # aws_security_group.ec2 will be created
  + resource "aws_security_group" "ec2" {
      + arn                    = (known after apply)
      + description            = "Security group for EC2 instance"
      + egress                 = [
          + {
              + cidr_blocks      = [
                  + "0.0.0.0/0",
                ]
              + description      = "Allow all outbound traffic"
              + from_port        = 0
              + ipv6_cidr_blocks = []
              + prefix_list_ids  = []
              + protocol         = "-1"
              + security_groups  = []
              + self             = false
              + to_port          = 0
            },
        ]
      + id                     = (known after apply)
      + ingress                = [
          + {
              + cidr_blocks      = [
                  + "0.0.0.0/0",
                ]
              + description      = "Allow HTTP"
              + from_port        = 80
              + ipv6_cidr_blocks = []
              + prefix_list_ids  = []
              + protocol         = "tcp"
              + security_groups  = []
              + self             = false
              + to_port          = 80
            },
          + {
              + cidr_blocks      = [
                  + "0.0.0.0/0",
                ]
              + description      = "Allow SSH"
              + from_port        = 22
              + ipv6_cidr_blocks = []
              + prefix_list_ids  = []
              + protocol         = "tcp"
              + security_groups  = []
              + self             = false
              + to_port          = 22
            },
        ]
      + name                   = "terraform-ec2-sg"
      + name_prefix            = (known after apply)
      + owner_id               = (known after apply)
      + revoke_rules_on_delete = false
      + tags                   = {
          + "Name" = "terraform-ec2-sg"
        }
      + tags_all               = {
          + "Name" = "terraform-ec2-sg"
        }
      + vpc_id                 = "vpc-xxxxxxxxxxxxxxxxx"
    }

Plan: 1 to add, 0 to change, 0 to destroy.

セキュリティグループが1つ作成されることがわかります。ingressにSSH(ポート22)とHTTP(ポート80)の2つのインバウンドルール、egressにすべてのアウトバウンドトラフィックを許可するルールが設定されていることを確認してください。

3. AWSへの反映

問題なければterraform applyで反映します。

terraform apply

確認プロンプトが表示されたら、yesと入力して実行します。以下のように、セキュリティグループが1件作成されたことを確認します。

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

4. AWSコンソールでの確認

AWSマネジメントコンソールで EC2 > セキュリティグループ を開き、「terraform-ec2-sg」が作成されていることを確認してください。セキュリティグループを選択し、以下の項目がコードで設定した内容と一致していることも確認しましょう。

  • セキュリティグループ名 が「terraform-ec2-sg」であること
  • 説明 が「Security group for EC2 instance」であること
  • VPC が「terraform-vpc」であること
  • 「インバウンドルール」タブで、SSH(ポート22, TCP, ソース0.0.0.0/0)が許可されていること
  • 「インバウンドルール」タブで、HTTP(ポート80, TCP, ソース0.0.0.0/0)が許可されていること
  • 「アウトバウンドルール」タブで、すべてのトラフィック(全プロトコル, 送信先0.0.0.0/0)が許可されていること