0
votes

I am attempting to use an environment variable in my terraform main.tf file. I ran the following command in my development environment:

TF_VAR_source_ami=ami-048e57a7474e7284d
echo $TF_VAR_source_ami
ami-048e57a7474e7284d

Here are the contents for main.tf:

variable "source_ami" {
  type        = "string"
  description = "AMI to copy this account"
}

data "aws_ami" "ami_to_be_shared" {
  owners      = ["self"]
  most_recent = true

  filter {
    name   = "image-id"
    values = ["${var.source_ami}"]
  }
}

output "ami_creation_date" {
  value = "${data.aws_ami.ami_to_be_shared.tags.AMICoreEngDate}"
}

When I run terraform plan, it prompts me for var.source_ami however, when I run terraform apply -var="source_ami=ami-048 e57a7474e7284d" it works.

I have also tried declaring the variable without a type and description.

Additional information:

Thank you for any help!

1

1 Answers

0
votes

you need export the variable.

export TF_VAR_source_ami=ami-048e57a7474e7284d

or run as one line

TF_VAR_source_ami=ami-048e57a7474e7284d terraform plan

Take a further read on this Defining a variable with or without export