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 · 5 comments

Comments

Projects
None yet
4 participants
@w32-blaster
Copy link
Contributor

w32-blaster commented Feb 8, 2017

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

This comment has been minimized.

Copy link
Contributor

apparentlymart commented Feb 8, 2017

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

This comment has been minimized.

Copy link
Contributor Author

w32-blaster commented Feb 8, 2017

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

This comment has been minimized.

Copy link
Contributor

apparentlymart commented Feb 8, 2017

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

This comment has been minimized.

Copy link

jobmiller commented Dec 31, 2017

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.

@RoryHolland

This comment has been minimized.

Copy link

RoryHolland commented Apr 18, 2018

Is anyone using a workaround for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.