Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use terraform with environment variables in .tf file

I am new to Terraform and I ran into some issue when trying to use environment variables with .tf file, I tried to use terraform.tfvars / variables.tf.

./terraform apply -var-file="terraform.tfvars"
Failed to load root config module: Error parsing variables.tf: At 54:17: illegal char

What am I missing here?

Terraform Version: Terraform v0.9.2

main.tf:

provider "aws" {
  access_key = "${var.aws_access_key}"
  secret_key = "${var.aws_secret_key}"
  region = "${var.aws_region}"
  allowed_account_ids = ["${var.aws_account_id}"]
}

resource "aws_instance" "db" {

  ami           = "ami-49c9295"
  instance_type = "t2.micro"

  tags {
    Name = "test"
  }

  connection {
   user = "ubuntu"
  }

  security_groups = ["sg-ccc943b0"]
  availability_zone = "${var.availability_zone}"
  subnet_id = "${var.subnet_id}"
}

terraform.tfvars:

aws_profile = "default"
aws_access_key = "xxxxxx"
aws_secret_key = "xxxxxx"
aws_account_id = "xxxxxx"
key_name = "keyname"
key_path = "/home/user/.ssh/user.pem"
aws_region = "us-east-1"
subnet_id = "subnet-51997e7a"
vpc_security_group_ids = "mysql"
instance_type = "t2.xlarge"
availability_zone = "us-east-1a"

variables.tf:

variable "key_name" {
    description = "Name of the SSH keypair to use in AWS."
    default     = "keypairname"
}

variable "key_path" {
    description = "Path to the private portion of the SSH key specified."
    default     = "/home/user/.ssh/mypem.pem"
}

variable "aws_region" {
    description = "AWS region to launch servers."
    default     = "us-east-1"
}

variable "aws_access_key" {
    decscription = "AWS Access Key"
    default     = "xxxxxx"
}

variable "aws_secret_key" {
    description = "AWS Secret Key"
    default     = "xxxxxx"
}

variable "aws_account_id" {
    description = "AWS Account ID"
    default     = "xxxxxx"
}

variable "subnet_id" {
    description = "Subnet ID to use in VPC"
    default     = "subnet-51997e7a"
}

variable "vpc_security_group_ids" {
    description = "vpc_security_group_ids"
    default     = "sec"
}

variable "instance_type" {
    description = "Instance type"
    default     = "t2.xlarge"
}

variable "instance_name" {
    description = "Instance Name"
    default     = "test"
}

variable "availability_zone" {
    description = "availability_zone"
    default     = "us-east-1a"
}

variable "aws_amis" {
    default = {
        "us-east-1": "ami-49c9295f",
        "eu-west-1": "ami-49c9295f",
        "us-west-1": "ami-49c9295f",
        "us-west-2": "ami-49c9295f"
    }
}

Update

After removing variable "aws_amis" section from variables.tf, I ran into another issue:

Failed to load root config module: Error loading variables.tf: 1 error(s) occurred:
* variable[aws_access_key]: invalid key: decscription
like image 981
Berlin Avatar asked Apr 13 '17 12:04

Berlin


People also ask

How do I use environment variables in Terraform?

Additionally, input variable values can also be set using Terraform environment variables. To do so, simply set the environment variable in the format TF_VAR_<variable name> . The variable name part of the format is the same as the variables declared in the variables.tf file.

How do I view environment variables in Terraform?

Terraform can directly access environment variables that are named using the pattern TF_VAR_ , for example TF_VAR_foo=bar will provide the value bar to the variable declared using variable "foo" {} .

How do you input variables in Terraform?

Terraform variables allow you to write configuration that is flexible and easier to re-use. Add a variable to define the instance name. Create a new file called variables.tf with a block defining a new instance_name variable. Note: Terraform loads all files in the current directory ending in .

What is var TF file in Terraform?

A "terraform. tfvars" file is an alternative to using the "-var" flag or environment variables. The file defines the variable values used by the script. If the file is named "terraform. tvars" it is loaded by default.


1 Answers

The aws_amis variable being used as a lookup map looks incorrectly formatted to me. Instead it should probably be of the format:

variable "aws_amis" {
    default = {
        us-east-1 = "ami-49c9295f"
        eu-west-1 = "ami-49c9295f"
        us-west-1 = "ami-49c9295f"
        us-west-2 = "ami-49c9295f"
    }
}

As an aside Terraform will look for a terraform.tfvars file by default so you can drop the -var-file="terraform.tfvars". You'll need to pass the -var-file option if you want to use a differently named file (such as prod.tfvars) but for this you can omit it.

like image 183
ydaetskcoR Avatar answered Oct 02 '22 15:10

ydaetskcoR