What is the proper way to use the backend_config map with RubyTerraform::Commands::Init

Viewed 39

What is the proper way to use the backend_config map with the RubyTerraform::Commands::Init command?

If I use the variable name, backend_config as it is defined in the documentation, I get the runtime error, uninitialized constant RubyTerraform::Options::Values::Complex::JSON (NameError). See below for the actual error message.

I noticed that the RubyTerraform library converted the Terraform command line option var to a plural vars. So, I tried using backend_configs. See below for the actual code.

My results was that that the code that uses backend_configs will execute without any runtime errors, but when I check the S3 bucket, there is no terraform state file. Per suggestion from @Allisoin, I found that if I add the line require 'json', it resolves the error message, and neither form of the option generate an error message.

But, they also don't produce a state file. I am guessing that Terraform won't produce a state file, for an empty project, and I need to actually add some actual infrastructure code to get it to save the state.

Example code, with backend_configs.

require 'ruby-terraform'
require 'json' # makes the error message for 'backend_config' go away

puts "Testing creating parameters"
RubyTerraform.init(
    chdir: 'terraform',
    backend_configs: {
        bucket: '<myAWSAccountNumber>-terraform-state-files',
        dynamodb_table: 'terraform_locks',
        key: "deleteME/terraform.tfstate",
        encrypt: true,
        region: "us-east-1"
    }
)

Result message that I get when running with backend_configs.

Testing creating parameters
Initializing the backend...
Initializing provider plugins...
Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

Process exited with code: 0

Note: There is no Terraform state file in the s3 bucket.

Here is the error that I get when using backend_config.

Note: The error message below provides the version numbers for both Ruby and the Ruby_Terraform library that I am using.

/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/ruby-terraform-1.3.1/lib/ruby_terraform/options/values/complex.rb:14:in `render': uninitialized constant RubyTerraform::Options::Values::Complex::JSON (NameError)
0 Answers
Related