dflook / terraform-github-actions

GitHub actions for terraform
785 stars 156 forks source link

Back-end config file in json throws an error #233

Open rsealsovtech opened 1 year ago

rsealsovtech commented 1 year ago

Problem description

Previously I was using a json formatted back-end file. On Friday suddenly terraform plan started throwing errors about not expecting a Left brace. I updated the name of the file to exclude .json and changed the syntax to HCL and it worked.

I did test a plan and apply locally with the same json files and received no syntax errors.

Terraform version

1.3.6

Backend

s3

Workflow YAML

- name: Plan Terraform
        if: ${{ github.event_name == 'pull_request' }}
        uses: dflook/terraform-plan@v1
        with:
          path: infrastructure
          backend_config_file: infrastructure/.s3.tfbackend.json
          var_file: infrastructure/terraform.tfvars.json

Workflow log

Plan: 0 to add, 6 to change, 0 to destroy.
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/dist-packages/lark/parsers/lalr_parser.py", line 59, in get_action
    return states[state][token.type]
KeyError: 'LBRACE'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/dist-packages/terraform/hcl.py", line 16, in try_load
    return hcl2.load(f)
  File "/usr/local/lib/python3.9/dist-packages/hcl2/api.py", line 9, in load
    return loads(file.read())
  File "/usr/local/lib/python3.9/dist-packages/hcl2/api.py", line 18, in loads
    return hcl2.parse(text + "\n")
  File "/usr/local/lib/python3.9/dist-packages/lark/lark.py", line 464, in parse
    return self.parser.parse(text, start=start)
  File "/usr/local/lib/python3.9/dist-packages/lark/parser_frontends.py", line 115, in parse
    return self._parse(token_stream, start)
  File "/usr/local/lib/python3.9/dist-packages/lark/parser_frontends.py", line 63, in _parse
    return self.parser.parse(input, start, *args)
  File "/usr/local/lib/python3.9/dist-packages/lark/parsers/lalr_parser.py", line 35, in parse
    return self.parser.parse(*args)
  File "/usr/local/lib/python3.9/dist-packages/lark/parsers/lalr_parser.py", line 88, in parse
    action, arg = get_action(token)
  File "/usr/local/lib/python3.9/dist-packages/lark/parsers/lalr_parser.py", line 66, in get_action
    raise UnexpectedToken(token, expected, state=state, puppet=puppet)
lark.exceptions.UnexpectedToken: Unexpected token Token('LBRACE', '***') at line 1, column 1.
Expected one of: 
    * __ANON_3
    * __ANON_0
    * __ANON_1
    * __ANON_2
    * $END

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.9/dist-packages/terraform/hcl.py", line 61, in <module>
    try_load(Path(sys.argv[1]))
  File "/usr/local/lib/python3.9/dist-packages/terraform/hcl.py", line 18, in try_load
    debug(e)
  File "/usr/local/lib/python3.9/dist-packages/github_actions/debug.py", line 9, in debug
    for line in msg.splitlines():
AttributeError: 'UnexpectedToken' object has no attribute 'splitlines'
Failed to load backend config file infrastructure/.s3.tfbackend.json
dflook commented 1 year ago

Thanks @rsealsovtech, I didn't realise that backend config files could be in json format. Can you share what such a file looks like?

rdhar commented 1 year ago

Going by the docs, Terraform supports JSON syntax with .json suffix, e.g., .tf.json and .tfvars.json. This is useful for feeding in programmatically-generated JSON output prepared by other software.

For our "backend_config_file" example, that can look like:

# backend.tfbackend
bucket         = "terraform-aws-rdhar"
dynamodb_table = "terraform-aws-rdhar"
key            = "terraform.tfstate"
region         = "us-east-1"

# backend.tfbackend.json
{
  "bucket": "terraform-aws-rdhar",
  "dynamodb_table": "terraform-aws-rdhar",
  "key": "terraform.tfstate",
  "region": "us-east-1"
}
sjmiller609 commented 1 year ago

I thought this was my problem, but actually my solution was to just not specify the backend config file. That works in the case of .tf.json files, where a backend file would start with

{
   "terraform": {
   "backend": {
   ...