StackStorm-Exchange / stackstorm-terraform

Apache License 2.0
13 stars 16 forks source link

Passing dictionary of variables to the variable_dict doesn't work #33

Open k-willowhawk opened 3 years ago

k-willowhawk commented 3 years ago

I've been trying to pass values present in a workflow context to the terraform.apply action as input to the variable_dict. I can confirm it is a properly formed dictionary of variable=value pairs, but terraform doesn't seem to know their values on execution.

As a manual workaround I've needed to write out a variables file using the core.local action and pass the variable filename to terraform.apply, but this is cludgy.

jensenja commented 2 years ago

Not sure if you've solved this for yourself already, but what I do for this is to create the dictionary in a python-runner action, then on action completion I return the dictionary along with my success status:

from st2common.runners.base_action import Action

class MyAction(Action):
    def run(self, message):
        some_dict = {
            'apple': 'red',
            'banana': 'yellow',
        }
        # maybe do more stuff
        return (True, some_dict)

After that you just need to publish the result to a variable in the workflow context, and then reference it in your terraform plan/apply/whatever. If I had to guess - you might not be referencing the contents of the variable properly using Jinja/YAQL:

version: 1.0

input:
  - foo
  - bar

vars:
  - tform_variable_dict: null

tasks:
  get_tform_vars:
    action: my_pack.my_action
    next:
      - when: <% succeeded() %>
        publish:
          - tform_variable_dict: "{{ result().result }}"
        do: terraform_plan

  terraform_plan:
    action: terraform.plan
    input:
      plan_path: "/some/dir"
      variable_dict: "{{ ctx().tform_variable_dict }}"
[...]

EDIT: For sensitive/auth-y type stuff ie provider credentials or something, I actually do use the "write-to-file" pattern (although I'm using a python-runner with Jinja templates rather than core.local) because when you use the variable_dict parameter, the contents of the parameter will be shown in the web UI when you click on that particular action in the workflow step.