0

Terraform newbie here. I've a module which creates an instance in GCP. I'm using variables and terraform.tfvars to initialize them. I created one instance successfully - say instance-1. But when I modify the .tfvars file to create a second instance (in addition to the first), it says it has to destroy the first instance. How can I run the module to 'add' an instance, instead of 'replacing the instance'? I know the first instance which was created is in terraform.tfstate. But that doesn't explain the inability to 'add' an instance.

Maybe I'm wrong, but I'm looking at 'modules' (and its config files) as functions- such that I can call them anytime with different parameters. That does not appear to be the case.

1 Answer 1

1

Terraform will try to maintain the deployed resources matching your resources definition. If you want two instances at the same time, then you should describe them both in your .tf file.

Ex. same instances, add a count to your definition

resource "some_resource" "example" {
  count = 2
  name = "example-${count.index}"
}

Ex. two different resources with specific values

resource "some_resource" "example-1" {
  name = "example-1"
  size = "small"
}
resource "some_resource" "example-2" {
  name = "example-2"
  size = "big"
}

Better you can set the specific values in tfvars for each resource

resource "some_resource" "example" {
  count = 2
  name = "example-${count.index}"
  size = ${vars.mysize[count.index]}
}
variable mysize {}

with tfvars file:

mysize = ["small", "big"]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.