![[Terraform] セキュリティグループを検証済Moduleを使って作成してみた](https://devio2023-media.developers.io/wp-content/uploads/2019/05/terraform-eyecatch.png)
[Terraform] セキュリティグループを検証済Moduleを使って作成してみた
セキュリティグループを検証済Moduleを使って作成してみます。コード量が削減されいい感じです。
2020.03.31
この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
セキュリティグループをModuleを使って構築してみます。
terraform-aws-modules/security-group/aws | Terraform Registry
やってみた
例として80、443ポートを許可したALB用のセキュリティグループを想定とします。以下がModuleを使った場合です。標準の書き方と比べてすっきりしました。
module public_alb_sg {
  source = "terraform-aws-modules/security-group/aws"
  name   = "public-alb-sg"
  vpc_id = var.vpc_id
  ingress_cidr_blocks = ["0.0.0.0/0"]
  ingress_rules       = ["https-443-tcp", "http-80-tcp"]
  egress_rules        = ["all-all"]
}
下記が標準の書き方です。
resource aws_security_group public_alb_sg {
  name   = "public-alb-sg"
  vpc_id = var.vpc_id
  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
  
  ingress {
    from_port   = 443
    to_port     = 443
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
  
  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}
これくらいなら書いてもいいかな....
まとめ
Terraform Registry には便利なModuleが多数登録されているので、使ってみてはいかがでしょうか。






