Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data source - return "null" if the source does not have the specified value #11782

Closed
w32-blaster opened this issue Feb 8, 2017 · 6 comments
Closed

Comments

@w32-blaster
Copy link
Contributor

I'm trying to solve a problem where a cloudformation stack output value is used.
I'm getting a key does not exist using the aws_cloudformation_stack data source which is expected because the specified key is not set in the CF template.

I'm wondering if it would make sense in general in all data sources to return a null value or empty string if the key does not exist in the source, so we can handle this situation using the function coalesce to return a default value.
That would allow us to set a default value like in a variable and use that instead. Or going back to my original problem, we would be able to return a default cloudformation output if the first key does not return anything.

@apparentlymart
Copy link
Contributor

Hi @w32-blaster! Thanks for this feature request.

I'd like to try to understand better what you're dealing with here. I think you are trying to retrieve the outputs of a cloudformation stack using the aws_cloudformation_stack data source, doing something like this:

data "aws_cloudformation_stack" "example" {
  name = "some-example-stack"
}

resource "something_else" "example" {
  some_attribute = "${data.aws_cloudformation_stack.example.outputs["foobarbaz"]}"
}

The above would fail if the map retrieved from the Cloudformation API did not include an output named "foobarbaz".

I think this issue could be avoided by using the lookup function, which is similar to the indexing syntax map[key] but allows a default to be provided to use in the event that the given key is not in the map:

resource "something_else" "example" {
  some_attribute = "${lookup(data.aws_cloudformation_stack.example.outputs, "foobarbaz", "default-foobarbaz")}"
}

Does that help you achieve the result you were looking for here, or is there something different going on?

@w32-blaster
Copy link
Contributor Author

OK, somehow I didn't get that the outputs is a map. It make sense to use lookup to set a fallback/default in this case. Thanks for the suggestion 👍

@apparentlymart
Copy link
Contributor

Great, @w32-blaster! I'm glad that works for you.

I'm going to close this issue, since it seems like this will be a workable approach for you.

@jobmiller
Copy link

I'm wondering how I can get around an empty datasource also. My issue seems slightly different.
The data source is empty initially, before the template starts running, so it fails:

${lookup(data.oci_core_subnets.sub.subnets[count.index],"id","dummy")}

  • oci_core_instance.TFInstance[0]: At column 43, line 1: list "data.oci_core_subnets.sub.subnets" does not have any elements so cannot determine type. in:

however, if the template just started running (or was run previously), this wouldn't be empty.

@ivystopia
Copy link

Is anyone using a workaround for this?

@ghost
Copy link

ghost commented Apr 3, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Apr 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants