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

Feature Request: If Exists Use Else Create #23178

Closed
frugecn opened this issue Oct 24, 2019 · 2 comments
Closed

Feature Request: If Exists Use Else Create #23178

frugecn opened this issue Oct 24, 2019 · 2 comments

Comments

@frugecn
Copy link

frugecn commented Oct 24, 2019

Feature Request

Need the ability to check to see if an object already exists and then be able to use it else create a new object.

Current Terraform Version

0.12.12

...

Use-cases

Example, in AWS, if a security group name xyz_sg already exists get the id of that security group for use in other parts of the terraform scripts. If the group doesn't exist, create the xyz_sg group to be used.

Attempted Solutions

Data aws_security_group sg {
name = "xyz_sg"
}

resource aws_security_group sg {
name = xyz_sg
count = data.aws_security_group.sg.id == "" ? 1 : 0
}

Unfortunately the data.aws_security_group throws an error

Proposal

References

@teamterraform
Copy link
Collaborator

Hi @frugecn! Thanks for sharing this use-case.

This pattern is intentionally not supported in Terraform because it would make it unclear to future readers of the configuration whether a particular object is managed by Terraform or not. Instead, you should decide for each situation whether the object is expected to exist already or whether it needs to be created and state that decision explicitly in your configuration.

There's some information on alternative approaches in the Terraform documentation page Module Composition, and in particular in the subsection Conditional Creation of Objects.

Since the capability you are describing is intentionally not supported, we're going to close this. Thanks for taking the time to share it, though! If you have any questions about the module composition patterns, please feel free to start a new topic on the community forum. Thanks!

@ghost
Copy link

ghost commented Mar 29, 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.

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

No branches or pull requests

3 participants