ColossusMark1
ColossusMark1

Reputation: 1289

Terraform backend.tf via modules

I've modules in another directory. So I want to add the backend.tf and set the provider data from linux environment variable .

But terraform giving error .

My Structure is showing as below .

main.tf
└── vpc
├── backend.tf
├── export.sh
├── vars.tf
└── vpc.tf

## main.tf
module "my_vpc" {
      source = "../../vpc"
      instance_tenancy = "default"

}

## backend.tf
terraform {
backend "s3" {
        region = "${var.aws_region}"
        bucket = "${var.TERRAFORM_BUCKET}-vpc"
        profile = "${var.ORGANISATION}"
        key    = "${var.ORGANISATION}"

    }
 }

 provider aws {

    profile = "${var.ORGANISATION}"
    region = "${var.aws_region}"
 }

I've exported variables ORGANISATION,REGION and TERRAFORM_BUCKET variables from terminal , but it give this error :

   Error: module "my_vpc": missing required argument "aws_region"

   Error: module "my_vpc": missing required argument "TERRAFORM_BUCKET"

   Error: module "my_vpc": missing required argument "ORGANISATION"

How Can I solve this issue ?

Notice : call backend.tf from module via environment variables . (Dynamic and default variables )

Please Help !

Upvotes: 0

Views: 2612

Answers (2)

abiydv
abiydv

Reputation: 621

This is what the docs say about variables in backend config.

Only one backend may be specified and the configuration may not contain interpolations. Terraform will validate this

This might help - #17288

Upvotes: 0

rclement
rclement

Reputation: 1714

The value for variables in a Terraform script can be provided a couple of different ways.

Input Variable Configuration

Since you are trying to provide them via environment variables, you should following the naming patter required.

$ TF_VAR_terraform_bucket=bucket_name
$ TF_VAR_organisation=org_name

Then when you perform terraform plan or terraform apply terraform will load the variables.

If you don't have the aws_region variable defined as an environment variable, then you will need to put it in a .tfvars file and use terraform plan -var-file config.tfvars or pass it in via the command line using terraform plan -var us-east-1.

This is all assuming that in your vars.tf file you have the variables defined.

variable "organisation" {
   type = "string"
}
variable "terraform_bucket" {
   type = "string"
}
variable "aws_region" {
   type = "string"
}

*** Edit 1

Thinking through your question, if the variables are needed inside the module then you will need to update your call to the module to include those variables in the use of it.

I cannot tell by the formating of your structure if the backend.tf, vars.tf and vpc.tf are inside the vpc folder or not.

module "my_vpc" {
   source = "../../vpc"
   instance_tenancy = "default"
   bucket = "${var.TERRAFORM_BUCKET}-vpc"
   profile = "${var.ORGANISATION}"
   key    = "${var.ORGANISATION}"
}

Upvotes: 2

Related Questions