Running multiple infrastructures

Hey folks!

I am new to Terraform and some things I just can’t figure out.

I have a pipeline where I use terraform to build our infrastructure (AWS) based on a file say ‘main.tf’ . I in fact use python-terraform wrapper to automate the task. Say, I initialize an ec2 instance where my processing takes place. While this is running, I want to start another infrastructure (terraform init, plan, apply) that can run parallelly.

As of now I was able to achieve this by creating different folders for different tfstate files (2 in our case) and disabling locks. But the problem occurs when I destroy the infrastructure. How do I specify which infrastructure to destroy?

Not sure how you wrote your main.tf, but if it’s anything like below:

variable "ec2_instance_names" {
  type    = list(string)
  default = []
}
module "ec2_instance" {
  for_each = toset(var.ec2_instance_names)

  source  = "terraform-aws-modules/ec2-instance/aws"
  version = "~> 3.0"

  name = each.key

  ami                    = "ami-ebd02392"
  instance_type          = "t2.micro"
  key_name               = "user1"
  monitoring             = true
  vpc_security_group_ids = ["sg-12345678"]
  subnet_id              = "subnet-eddcdzz4"

  tags = {
    Terraform   = "true"
    Environment = "dev"
  }
}

You can run terraform plan and apply on the above by supplying a list of ec2 instances that you want to create.

terraform apply -var 'ec2_instance_names=["one", "two", "three"]'

# This will create three instances.

Using your python code you should be able to change the input array and either create or delete a new instance.

terraform apply -var 'ec2_instance_names=["one", "two", "four"]'

# This will create instance three and create instance four.

And you can delete all of your instances

terraform apply -var 'ec2_instance_names=[]'

# This will delete all the instances that you created.

Hope this helps.