Search code examples
amazon-web-servicesamazon-ec2terraformterraform-provider-awsterraform-template-file

Creating terraform aws rds first then create ec2 instance


I wanna create an rds & ec2 instance using terraform & I'm able to do that but the issue is that I wanna use the rds end-point inside ec2 bash script using template data source script right now what happening is my ec2 instance create first & then my rds was created due to that I'm not able to use rds endpoint because it was created at that time. so once it creates then my ec2 data source script should run.


# RDS 
resource "aws_db_parameter_group" "terraform_postgres_parameters" {
  name        = format("%s-%s-%s", var.PROJECT_NAME, var.RDS_IDENTIFIER , "parameters")
  family      = format("%s%s", var.ENGINE, var.ENGINE_VERSION)
  description = format("%s %s %s", var.PROJECT_NAME, var.ENGINE , "parameter group")

}

resource "aws_db_instance" "terraform_postgres" {
  allocated_storage       = 20               # min_storage
  max_allocated_storage   = 100              # max_storage
  engine                  = var.ENGINE
  engine_version          = var.ENGINE_VERSION
  instance_class          = "db.t2.micro"
  identifier              = var.RDS_IDENTIFIER
  name                    = var.RDS_NAME
  username                = var.RDS_USERNAME
  password                = var.RDS_PASSWORD
  parameter_group_name    = aws_db_parameter_group.terraform_postgres_parameters.name
  multi_az                = "false"
  storage_type            = "gp2"
  backup_retention_period = 30
  skip_final_snapshot     = true
  publicly_accessible     = true
  tags = {
    Name = "terraform_postgres_instance"
  }
}


# EC2 Instance
resource "aws_instance" "example" {
  ami           = "ami-0bdef2eb518663879"
  instance_type = "t2.micro"

  user_data = data.template_cloudinit_config.cloudinit-example.rendered
}

# Templete File
data "template_file" "shell-script" {
  template = file("script/cloud-init.sh")
  vars = {
    DB_HOST = aws_db_instance.terraform_postgres.endpoint   # Need to use rds endpoint here
    DB_USER = var.RDS_USERNAME
    DB_PASSWORD = var.RDS_PASSWORD
    DB_NAME = "kong"
  }
}

data "template_cloudinit_config" "cloudinit-example" {
  gzip          = false
  base64_encode = false

  part {
    content_type = "text/x-shellscript"
    content      = data.template_file.shell-script.rendered
  }

}



# script/cloud-init.sh file
...
...
...

if true; then
    psql --host ${DB_HOST} --port=5432 --username=${DB_USERNAME} <<EOF      # creating database for kong api-gateway
CREATE DATABASE kong OWNER ${DB_USER}; 
EOF
fi

...
...
...

# Setup Configuration file

...
...
...
...
...
...
...

sudo -u kong kong migrations bootstrap

# Start Kong
sudo /usr/local/bin/kong start
# end of cloud-init.sh file

Solution

  • You should be ale to do that with depends_on meta-argument in the aws_instance resource.

    Try to look at the official documentation for that https://www.terraform.io/docs/language/meta-arguments/depends_on.html