raymondbutcher / pretf

Generate Terraform code with Python
https://pretf.readthedocs.io/
MIT License
104 stars 14 forks source link

[question] How to specify the "ignore_changes" meta-argument? #59

Closed pepoluan closed 4 years ago

pepoluan commented 4 years ago

The Terraform documentation describes a way of ignoring specific changes; the second example given shows how to ignore changes to the "Name" key of the "tags" attribute by writing it (in HCL) as:

  lifecycle {
    ignore_changes = [
      tags["Name"],
    ]
  }

The tags["Name"] syntax caught me; at a glance, it seems to be referring a mapping. So I did it like this:

        # Block body, followed by:
        "lifecycle": {
            "ignore_changes": [
                {"tags": "Name"}
            ]
        }

but I ended up with the following error when doing pretf validate:

Error: Invalid expression

  on bastion.tf.json line 16, in [0].resource.aws_instance.bastion.lifecycle.ignore_changes:
  16:               {
  17:                 "tags": "Name"
  18:               }

A single static variable reference is required: only attribute access and
indexing with constant keys. No calculations, function calls, template
expressions, etc are allowed here.

So how do I do the same (that is, ignore only changes to the "Name" key in the "tags" attribute) in a pretf block?

raymondbutcher commented 4 years ago

Hi @pepoluan, sorry I didn't see this until recently.

This code passes terraform validate:

from pretf.blocks import resource

def pretf_blocks(var):
    yield resource.aws_lb.test(
        name="test",
        lifecycle={
            "ignore_changes": [
                'tags["Name"]'
            ]
        }
    )

or using the more verbose block function:

from pretf.api import block

def pretf_blocks(var):
    yield block("resource", "aws_lb", "test", {
        "name": "test",
        "lifecycle": {
            "ignore_changes": [
                'tags["Name"]'
            ]
        }
    })

Pretf is just converting the dictionary into JSON so we have to figure out (or guess) what Terraform is expecting the JSON to be, and then do that. I just guessed that it was meant to be provided as a simple string.