Search code examples
terraformovh

Can't store OVH credentials in Terraform .tfvars file


I'm looking for some guidance and help. I'm new with terraform and trying new things. I added OVH provider for DNS records, but I'm having troubles with passing credentials.

I was trying to pass OVH credentials as variables and store them in credentials.tfvars or credentials.auto.tfvars files, but I'm getting 403 Forbidden errors when using terraform plan.

Next I placed all credentials in main.tf file and everything works ok. I'm looking for some guidance what can be wrong with my config.

Here are example files for my terraform.

main.tf

terraform { 
  required_providers { 
    docker = { 
      source  = "kreuzwerker/docker" 
      version = "3.0.2" 
    } 
    ovh = { 
      source = "ovh/ovh" 
      version = "0.32.0" 
    } 
  } 
} 

variable "ovhapplication_key" { 
  type = string 
} 

variable "ovhapplication_secret" { 
  type = string 
} 
    
variable "ovhconsumer_key" { 
  type = string 
} 

variable "MariaDBPass" { 
  type = string 
} 
    
variable "Pass" { 
  type = string 
} 

provider "docker" { 
  host = "unix:///var/run/docker.sock" 
} 
    
provider "ovh" { 
  endpoint           = "ovh-eu" 
  application_key    = "var.ovhapplication_key" 
  application_secret = "var.ovhapplication_secret" 
  consumer_key       = "var.ovhconsumer_key" 
} 

terraform.tfvars

ovhapplication_key="SomeCredentials" 
ovhapplication_secret="SomeCredentials"  
ovhconsumer_key="SomeCredentials" 
MariaDBPass="SomeCredentials" 
Pass="SomeCredentials"

Solution

  • There might be a couple of problems with the code you have posted. First problem seems to be the way you are trying to reference the variables. The way your code shows it, what happens is that variables are not substituted for values. You will get string literals. In other words, this:

    application_key    = "var.ovhapplication_key" 
    application_secret = "var.ovhapplication_secret" 
    consumer_key       = "var.ovhconsumer_key" 
    

    will tell terraform to read the application key the same way it is written, i.e., "var.ovhapplication_key". To fix this, you need to fix variable references to use the correct syntax:

    application_key    = var.ovhapplication_key
    application_secret = var.ovhapplication_secret
    consumer_key       = var.ovhconsumer_key
    

    The second problem could be the naming convention of your tfvars files. Terraform should pick up auotmagically all the *.auto.tfvars files. So you might have set the variable values in a properly named file, but the way you were referencing them in the provider block is still using string literals. The second option you mentioned for the file name, i.e., credentials.tfvars would mean you need to specify that file name when calling different terraform commands, e.g:

    terraform plan -var-file="credentials.tfvars"
    

    Note that terraform picks up any variables defined in terraform.tfvars automatically. Make sure you go through the documentation as well.