狙い、目的とか
第0回に書いたので、そちらを参照
https://qiita.com/michi_taka/items/55ec466f99d0d87e08b7
作りたいアプリの環境
第2回の目標
- セキュリティグループの作成
- インバウンド、アウトバウンドを作る
ざっくりと作ってもらう
ロードバランサとRDSとECSアプリで使うセキュリティグループを作って欲しい。
それぞれよしなにインバウンド、アウトバウンドを設定し、terraformファイルを書いてほしい。
なお、VPCは既に設定が終わっている為、変数化しておいてほしい。
出てきたコード
variable "vpc_id" {
type = string
description = "VPC ID"
}
resource "aws_security_group" "lb_sg" {
name = "lb-sg"
vpc_id = var.vpc_id
ingress {
from_port = 80
to_port = 80
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"]
}
}
resource "aws_security_group" "rds_sg" {
name = "rds-sg"
vpc_id = var.vpc_id
ingress {
from_port = 3306
to_port = 3306
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"]
}
}
resource "aws_security_group" "ecs_app_sg" {
name = "ecs-app-sg"
vpc_id = var.vpc_id
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
source_security_group_ids = [aws_security_group.lb_sg.id]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
いい具合に作ってくれているけど、CIDRブロックの設定がガバガバ。
ALBのインバウンドにはHTTPSのプロトコルも追加して。RDSのセキュリティグループは、ECSのアプリとの通信に制限して。
あと、WEBアプリのfrom_portとto_portは共通の変数化しておいてほしい。
出たきたコード
variable "vpc_id" {
type = string
description = "VPC ID"
}
variable "web_app_port" {
type = number
description = "WEBアプリのポート番号"
default = 80
}
resource "aws_security_group" "lb_sg" {
name = "lb-sg"
vpc_id = var.vpc_id
ingress {
from_port = var.web_app_port
to_port = var.web_app_port
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"]
}
}
resource "aws_security_group" "rds_sg" {
name = "rds-sg"
vpc_id = var.vpc_id
ingress {
from_port = 3306
to_port = 3306
protocol = "tcp"
source_security_group_ids = [aws_security_group.ecs_app_sg.id]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_security_group" "ecs_app_sg" {
name = "ecs-app-sg"
vpc_id = var.vpc_id
ingress {
from_port = var.web_app_port
to_port = var.web_app_port
protocol = "tcp"
source_security_group_ids = [aws_security_group.lb_sg.id]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
修正箇所
怪しい変数名source_security_group_ids
をsecurity_groups
に修正したら無事にplan/applyに成功。非常にシンプルでVPCのNATゲートウェイとかに比べると、一瞬で良いコードを出力してくれた。
まとめ
- 作るものが明確ならば、かなり精度高く作ってくれる
- 「何故、このように設定すべきか」というのが分からないと、セキュリティ面はガバガバになる
- RDSのingressが最初はフルアクセス可能だった
- 作りたいもののイメージをもって的確な指示や修正が必要
- やはり知識がなくてもAIがいい具合に作ってくれるわけではない
- 今後のAIの進化に期待
ここまでのgithub