Security Group
The following module creates a typical security group allowing inbound SSH while granting full egress.
variable "name" {}
variable "description" {}
variable "vpc_id" {}
resource "aws_security_group" "main" {
name = var.name
description = var.description
vpc_id = var.vpc_id
ingress {
from_port = 22
to_port = 22
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"]
}
}
output "id" {
value = aws_security_group.main.id
}
The following module builds on the typical security group to support specifiying a custom port that is only made accessible to the VPC's internal network.
variable "name" {}
variable "description" {}
variable "vpc_id" {}
variable "port" {
default = 8080
}
data "aws_vpc" "main" {
id = var.vpc_id
}
resource "aws_security_group" "main" {
name = var.name
description = var.description
vpc_id = var.vpc_id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = var.port
to_port = var.port
protocol = "tcp"
cidr_blocks = [data.aws_vpc.main.cidr_block]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
output "id" {
value = aws_security_group.main.id
}