digitickets / terraform-aws-cli

Run the AWS CLI, with the ability to run under an assumed role, to access resources and properties missing from the Terraform AWS Provider.
28 stars 11 forks source link

Re-applying fails for non-existent output being parsed as JSON #3

Closed yaronya closed 2 years ago

yaronya commented 2 years ago

Getting this error (during plan) when applying an existing stack:

│ Error: Error in function call
│ 
│   on .terraform/modules/stack.enable_asg_metrics_collection/output.tf line 3, in output "result":
│    3:   value       = jsondecode(data.local_file.awscli_results_file.content)
│     ├────────────────
│     │ data.local_file.awscli_results_file.content is ""
│ 
│ Call to function "jsondecode" failed: EOF.

Deleting the .terraform/modules/stack.enable_asg_metrics_collection folder or re-running terraform init --upgrade (which removes the folder) is the workaround I'm current doing, but how can I overcome this in a better way? I would've expected the module to not jsondecode in case there is no output...

rquadling commented 2 years ago

Interesting. What is the AWS CLI call you're making and what output is generated?

To date, all our use cases have been to retrieve known data.

rquadling commented 2 years ago

And apologies for not responding earlier.

rquadling commented 2 years ago

OOI, is your local environment configured such that JSON is not the default output of the AWS CLI? (See https://github.com/digitickets/terraform-aws-cli/pull/2 which I'm about to release).

rquadling commented 2 years ago

I've have managed to replicate the issue in our own setup! So thank you for the report. Hopefully the fix I'm about to release will work for us. Hopefully it'll work for you also.

rquadling commented 2 years ago

Hi. I've just released https://github.com/digitickets/terraform-aws-cli/releases/tag/v5.0.2. I'd be very grateful if you could see if this release fixes the issue you're facing.

pavkar commented 2 years ago

I have the same problem. I use version 5.0.2. I'm trying to update service settings and command does not have a response body. As stated in documentation "If the action is successful, the service sends back an HTTP 200 response with an empty HTTP body.". Response is in JSON.

rquadling commented 2 years ago

Can you send me the module resource you're using please? Hopefully with that, I can replicate the issue and provide a solution.

pavkar commented 2 years ago

https://awscli.amazonaws.com/v2/documentation/api/latest/reference/guardduty/update-detector.html Example of the command: aws guardduty update-detector --finding-publishing-frequency ONE_HOUR

rquadling commented 2 years ago

So, is the issue that you've not supplied a --detector-id?

$ aws guardduty update-detector --finding-publishing-frequency ONE_HOUR

usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:

  aws help
  aws <command> help
  aws <command> <subcommand> help

aws: error: the following arguments are required: --detector-id

So tracking errors from the AWS CLI and reporting that back is what's needed.

pavkar commented 2 years ago

Sorry I didn't add it into the example I am using this syntax: aws guardduty --detector-id XXXXXXXXXXXXXXXXXX update-detector --finding-publishing-frequency ONE_HOUR

rquadling commented 2 years ago

Thank you for that. Replicated. It does seem that Terraform already supports this (https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/guardduty_detector#finding_publishing_frequency).

But having replicated the issue using your example, I think the fix is relatively simple.

I've documented a new test that demonstrates the fix.

Releasing v5.0.3 shortly.