As the title says, if I'm using terraform/aws/layers/bastion/main.tf to create an EC2 instance, I know I can also create a security group within this same main.tf file for the bastion instance to use, but what if I wanted to create a security group that can be used in a different file?
For example, if terraform/aws/layers/worker/main.tf needed to use the same security group as bastion/main.tf how would I go about this?
bastion/main.tf
provider "aws" {
region = var.region
}
resource "aws_instance" "bastion" {
name = "bastion"
ami = var.image_id
instance_type = var.instance_type
vpc_security_group_ids = [aws_security_group.bastion.id]
subnet_id = var.subnet
iam_instance_profile = "aws-example-ec2-role"
tags = {
Layer = "Bastion"
}
}
resource "aws_security_group" "bastion_from_ssh" {
name = "Bastion"
description = "Bastion example group"
vpc_id = "vpc-12345"
}
resource "aws_security_group_rule" "allow_ssh" {
from_port = ##
to_port = ##
protocol = "##"
description = "Bastion SSH"
cidr_blocks = ["1.2.3.4/5"]
}
resource "aws_security_group_rule" "bastion_to_db" {
from_port = ##
to_port = ##
protocol = "##"
description = "Access to default server security group"
source_security_group_id = "sg-12345"
}