Terraspace AWS Backend undefined method

I’m getting error when use dynamodb_table and kms_key_id attributes in aws S3 backend.

backend.rb

backend("s3",
    bucket:                      ENV['TF_S3_BUCKET'],
    key:                         ":ENV/:REGION/customers/:INSTANCE/:MOD_NAME/terraform.tfstate",
    region:                      ":REGION",
    encrypt:                     true,
    dynamodb_table:              ENV['TF_LOCK_TABLE'],
    acl:                         "private",
    kms_key_id:                  ENV['TF_S3_KMS_KEY_ID'],
    skip_credentials_validation: true,
    skip_region_validation:      true,
    skip_metadata_api_check:     true
)

Error:

Error evaluating /app/config/terraform/backend.rb:
undefined method `aws' for #<TerraspacePluginAws::Interfaces::Expander:0x00005610ce2d9530>
Here's the line in /app/config/terraform/backend.rb with the error:

 1 backend("s3",
 2     bucket:                      ENV['TF_S3_BUCKET'],
 3     key:                         ":ENV/:REGION/customers/:INSTANCE/:MOD_NAME/terraform.tfstate",
 4     region:                      ":REGION",
 5     encrypt:                     true,
 6     dynamodb_table:              ENV['TF_LOCK_TABLE'],

Full error:
bundler: failed to load command: terraspace (/usr/local/bundle/bin/terraspace)
NoMethodError: undefined method `aws' for #<TerraspacePluginAws::Interfaces::Expander:0x00005610ce2d9530>
  /usr/local/bundle/gems/terraspace-0.6.19/lib/terraspace/plugin/expander/interface.rb:72:in `var_value'
  /usr/local/bundle/gems/terraspace-0.6.19/lib/terraspace/plugin/expander/interface.rb:41:in `block in expansion'
  /usr/local/bundle/gems/terraspace-0.6.19/lib/terraspace/plugin/expander/interface.rb:40:in `each'
  /usr/local/bundle/gems/terraspace-0.6.19/lib/terraspace/plugin/expander/interface.rb:40:in `expansion'
  /usr/local/bundle/gems/terraspace-0.6.19/lib/terraspace/plugin/expander/interface.rb:22:in `block in expand'
  /usr/local/bundle/gems/terraspace-0.6.19/lib/terraspace/plugin/expander/interface.rb:21:in `each'
  /usr/local/bundle/gems/terraspace-0.6.19/lib/terraspace/plugin/expander/interface.rb:21:in `expand'
  /usr/local/bundle/gems/terraspace-0.6.19/lib/terraspace/compiler/expander.rb:3:in `expand'
  /usr/local/bundle/gems/terraspace-0.6.19/lib/terraspace/compiler/dsl/syntax/mod/backend.rb:11:in `expansion_all!'
  /usr/local/bundle/gems/terraspace-0.6.19/lib/terraspace/compiler/dsl/syntax/mod/backend.rb:6:in `backend'
  /app/config/terraform/backend.rb:1:in `evaluate_file'
  /usr/local/bundle/gems/dsl_evaluator-0.1.3/lib/dsl_evaluator.rb:11:in `instance_eval'
  /usr/local/bundle/gems/dsl_evaluator-0.1.3/lib/dsl_evaluator.rb:11:in `evaluate_file'
  /usr/local/bundle/gems/terraspace-0.6.19/lib/terraspace/compiler/dsl/mod.rb:13:in `evaluate'
  /usr/local/bundle/gems/terraspace-0.6.19/lib/terraspace/compiler/dsl/mod.rb:8:in `build'
  /usr/local/bundle/gems/terraspace-0.6.19/lib/terraspace/compiler/strategy/mod/rb.rb:4:in `run'
  /usr/local/bundle/gems/terraspace-0.6.19/lib/terraspace/compiler/strategy/mod.rb:8:in `run'
  /usr/local/bundle/gems/terraspace-0.6.19/lib/terraspace/compiler/cleaner/backend_change.rb:40:in `fresh_backend'
  /usr/local/bundle/gems/terraspace-0.6.19/lib/terraspace/compiler/cleaner/backend_change.rb:21:in `purge?'
  /usr/local/bundle/gems/terraspace-0.6.19/lib/terraspace/compiler/cleaner/backend_change.rb:10:in `purge'
  /usr/local/bundle/gems/terraspace-0.6.19/lib/terraspace/compiler/cleaner.rb:16:in `backend_change_purge'
  /usr/local/bundle/gems/terraspace-0.6.19/lib/terraspace/compiler/cleaner.rb:9:in `clean'
  /usr/local/bundle/gems/terraspace-0.6.19/lib/terraspace/builder.rb:77:in `clean'
  /usr/local/bundle/gems/terraspace-0.6.19/lib/terraspace/builder.rb:13:in `run'
  /usr/local/bundle/gems/terraspace-0.6.19/lib/terraspace/cli/up.rb:20:in `build'
  /usr/local/bundle/gems/terraspace-0.6.19/lib/terraspace/cli/up.rb:8:in `run'
  /usr/local/bundle/gems/terraspace-0.6.19/lib/terraspace/cli.rb:222:in `up'
  /usr/local/bundle/gems/thor-1.1.0/lib/thor/command.rb:27:in `run'
  /usr/local/bundle/gems/thor-1.1.0/lib/thor/invocation.rb:127:in `invoke_command'
  /usr/local/bundle/gems/thor-1.1.0/lib/thor.rb:392:in `dispatch'
  /usr/local/bundle/gems/terraspace-0.6.19/lib/terraspace/command.rb:61:in `dispatch'
  /usr/local/bundle/gems/thor-1.1.0/lib/thor/base.rb:485:in `start'
  /usr/local/bundle/gems/terraspace-0.6.19/exe/terraspace:14:in `<top (required)>'
  /usr/local/bundle/bin/terraspace:23:in `load'
  /usr/local/bundle/bin/terraspace:23:in `<top (required)>'
/app # 

if i command the dynamodb table and kms key id, runs well.

Could you help?

Thanks.

Regards,
Fábio Santos

:face_with_monocle: Looked at the stack trace:

With the backend.rb ruby DSL, Terraspace calls expansion on all values. So it’s running into the kms_key_id which has a value something like this:

kms_key_id=arn:aws:kms:us-west-2:112233445566:key/937e938d-02f7-458c-8f9b-30846afd3bd3

The :aws is being interpreted as a value that it needs to expand and the terraspace aws plugin doesn’t define that method, so it bombs. Bummer. Will consider PRs for this. No sweat either way of course.

Unsure exactly how to handle yet. Maybe whitelist the keys where the expansion should run when using the ruby backend.rb dsl.


For something more immediate to solve this issue, you can switch to a backend.tf instead of a backend.rb This way you can ensure that the expansion method won’t get called on the kms_key_id assignment and bomb.

Sadly, the ERB in backend.tf won’t be as pretty but it should work for now.

Figured it out. Added an expand_string? interface method for plugin expanders. Details in these PRs:

Update your terraspace and terraspace_plugin_aws gems with:

cd terraspace-project-folder
bundle update
bundle info terraspace_plugin_aws
bundle info terraspace

You should see something like this:

$ bundle info terraspace_plugin_aws
  * terraspace_plugin_aws (0.3.3)
        Summary: Terraspace AWS Plugin
        Homepage: https://github.com/boltops-tools/terraspace_plugin_aws
        Path: /home/ec2-user/.rvm/gems/ruby-3.0.3/gems/terraspace_plugin_aws-0.3.3
$ bundle info terraspace
  * terraspace (0.6.20)
        Summary: Terraspace: The Terraspace Framework
        Homepage: https://github.com/boltops-tools/terraspace
        Path: /home/ec2-user/.rvm/gems/ruby-3.0.3/gems/terraspace-0.6.20
$ 

Then the kms_key_id value wont get expanded anymore even with the backend.rb DSL.

Many thanks @tung. It’s working.

Many thanks for this project, i have been really enjoying this framework.

Regards,
Fábio Santos