Terraform - DB and security group are in different VPCs

Viewed 1497

What am I trying to achive:

Create and RDS Aurora cluster and place it in the same VPC as EC2 instances that I start so they can comunicate. I'm trying to start an SG named "RDS_DB_SG" and make it part of the VPC i'm creating in the process. I also create an SG named "BE_SG" and make it part of the same VPC. I'm doing this so I can get access between the 2 (RDS and BE server).

What I did so far:

Created an .tf code and started everything up.

What I got:

It starts ok if I don't include the RDS cluster inside the RDS SG - The RDS creates it's own VPC. When I include the RDS in the SG I want for him, The RDS cluster can't start and get's an error.

Error I got:

"The DB instance and EC2 security group are in different VPCs. The DB instance is in vpc-5a***63c and the EC2 security group is in vpc-0e5391*****273b3d"

Workaround for now:

I started the infrastructure without specifing a VPC for the RDS. It created it's own default VPC. I then created manuall VPC-peering between the VPC that was created for the EC2's and the VPC that was created for the RDS. But I want them to be in the same VPC so I won't have to create the VPC-peering manuall.

My .tf code:

variable "vpc_cidr" {
  description = "CIDR for the VPC"
  default = "10.0.0.0/16"
}

resource "aws_vpc" "vpc" {
  cidr_block = "${var.vpc_cidr}"
  tags = {
    Name = "${var.env}_vpc"
  }
}

resource "aws_subnet" "vpc_subnet" {
  vpc_id = "${aws_vpc.vpc.id}"
  cidr_block = "${var.vpc_cidr}"
  availability_zone = "eu-west-1a"

  tags = {
    Name = "${var.env}_vpc"
  }
}

resource "aws_db_subnet_group" "subnet_group" {
  name        = "${var.env}-subnet-group"
  subnet_ids  = ["${aws_subnet.vpc_subnet.id}"]
}

resource "aws_security_group" "RDS_DB_SG" {
    name = "${var.env}-rds-sg"
    vpc_id = "${aws_vpc.vpc.id}"
    ingress {
        from_port = 3396
        to_port = 3396
        protocol = "tcp"
        security_groups = ["${aws_security_group.BE_SG.id}"]
    }
}

resource "aws_security_group" "BE_SG" {
    name = "${var.env}_BE_SG"
    vpc_id = "${aws_vpc.vpc.id}"

    ingress {
        from_port = 80
        to_port = 80
        protocol = "tcp"
        cidr_blocks = ["0.0.0.0/0"]
    }
}

resource "aws_instance" "BE" {
    ami = "ami-*********************"
    instance_type = "t2.large"
    associate_public_ip_address = true
    key_name = "**********"
    tags = {
        Name = "WEB-${var.env}"
        Porpuse = "Launched by Terraform"
        ENV = "${var.env}"
    }

    subnet_id = "${aws_subnet.vpc_subnet.id}"
    vpc_security_group_ids = ["${aws_security_group.BE_SG.id}", "${aws_security_group.ssh.id}"]
}

resource "aws_rds_cluster" "rds-cluster" {
    cluster_identifier = "${var.env}-cluster"
    database_name = "${var.env}-rds"
    master_username = "${var.env}"
    master_password = "PASSWORD"
    backup_retention_period = 5
    vpc_security_group_ids = ["${aws_security_group.RDS_DB_SG.id}"]
}

resource "aws_rds_cluster_instance" "rds-instance" {
    count = 1
    cluster_identifier = "${aws_rds_cluster.rds-cluster.id}"
    instance_class = "db.r4.large"
    engine_version = "5.7.12"
    engine = "aurora-mysql"
    preferred_backup_window = "04:00-22:00"
}

Any suggestions on how to achieve my first goal?

0 Answers
Related