Answers: <p>You can use the count meta-parameter to achieve the effects of an if-else statement.  It is talked about in detail in the link below and I also pulled one relevant example out.</p>
  
  <p>I'm not 100% sure if you can give both the data source and resource the same name without a conflict; but I assume it would work.  It may be dependent on the resource you're creating though, not sure.</p>
  
  <p><a href="https://blog.gruntwork.io/terraform-tips-tricks-loops-if-statements-and-gotchas-f739bbae55f9" rel="nofollow noreferrer">https://blog.gruntwork.io/terraform-tips-tricks-loops-if-statements-and-gotchas-f739bbae55f9</a></p>
  
  <h2>Excerpt:</h2>
  
  <pre><code># This is just pseudo code. It won't actually work in Terraform.
  if var.give_neo_cloudwatch_full_access {
    resource "aws_iam_user_policy_attachment" "neo_cloudwatch_full" {
      user       = aws_iam_user.example[0].name
      policy_arn = aws_iam_policy.cloudwatch_full_access.arn
    }
  } else {
    resource "aws_iam_user_policy_attachment" "neo_cloudwatch_read" {
      user       = aws_iam_user.example[0].name
      policy_arn = aws_iam_policy.cloudwatch_read_only.arn
    }
  }
  </code></pre>
  
  <p>To do this in Terraform, you can use the count parameter and a conditional expression on each of the resources:</p>
  
  <pre><code>resource "aws_iam_user_policy_attachment" "neo_cloudwatch_full" {
    count = var.give_neo_cloudwatch_full_access ? 1 : 0
    user       = aws_iam_user.example[0].name
    policy_arn = aws_iam_policy.cloudwatch_full_access.arn
  }
  resource "aws_iam_user_policy_attachment" "neo_cloudwatch_read" {
    count = var.give_neo_cloudwatch_full_access ? 0 : 1
    user       = aws_iam_user.example[0].name
    policy_arn = aws_iam_policy.cloudwatch_read_only.arn
  }
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2020-04-01T16:54:33.353Z
Answers: Thank you for this answer, but it's not quite what I'm looking for, because I want to use a boolean to decide if we're creating a resource or sourcing a data source. It may be the best answer, though - we'll see! :D
Answers: Jesse Adelman
Answers: CC BY-SA 4.0
Answers: 2020-07-27T03:21:56.407Z
Answers: It really seems what I was seeking is just not possible via the current version of HCL. But, I "accepted" this answer because this is likely the closest we'll get to what I wanted. Thanks!
Answers: Jesse Adelman
Answers: CC BY-SA 4.0
Answers: 2020-03-31T23:43:00.580Z
Answers: 2020-03-31T23:43:00.580Z
Answers: John Humphreys
Body: <p>Basically, what I'd like to do is create a module that does this pseudocode:</p>
  
  <pre><code>if &lt;boolean&gt; then
    resource "&lt;resource type&gt;" "name" {
      # ... set variables ...
    } else {
    data "&lt;data source&gt;" "name" {
      # grab data
    }
  
  output "resource_type.name" {
    if &lt;boolean&gt; then
      value = resource_type.name
    else
      value = data_source.name
  }
  </code></pre>
  
  <p>I hope I'm being clear. Happy to add more info to clarify what I'm hoping is possible.</p>
  
Comments: CC BY-SA 4.0
Comments: 2020-04-01T16:48:21.077Z
Comments: It's not exactly what you were asking, but there's some official documentation with some advice on how to handle this sort of thing in modules: https://www.terraform.io/docs/modules/composition.html#conditional-creation-of-objects
Comments: Martin Atkins
Tags: <terraform><infrastructure-as-code>
Title: Is it possible to create a module that either creates a resource or sources a data source, based on a boolean value?
UserDisplayName: Jesse Adelman
ThreadText: # Is it possible to create a module that either creates a resource or sources a data source, based on a boolean value?
  
  (Asked by: username_0 on 2020-03-31)
  
  Basically, what I'd like to do is create a module that does this pseudocode:
  
  ```
  if <boolean> then
    resource "<resource type>" "name" {
      # ... set variables ...
    } else {
    data "<data source>" "name" {
      # grab data
    }
  
  output "resource_type.name" {
    if <boolean> then
      value = resource_type.name
    else
      value = data_source.name
  }
  
  ```
  
  I hope I'm being clear. Happy to add more info to clarify what I'm hoping is possible.
  
  **Tags:** terraform, infrastructure-as-code
  
  > **Comments:**
  > **username_1**: It's not exactly what you were asking, but there's some official documentation with some advice on how to handle this sort of thing in modules: https://www.terraform.io/docs/modules/composition.html#conditional-creation-of-objects  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2020-03-31. Score: 2 | Accepted answer
  
  You can use the count meta-parameter to achieve the effects of an if-else statement. It is talked about in detail in the link below and I also pulled one relevant example out.
  
  I'm not 100% sure if you can give both the data source and resource the same name without a conflict; but I assume it would work. It may be dependent on the resource you're creating though, not sure.
  
  <https://blog.gruntwork.io/terraform-tips-tricks-loops-if-statements-and-gotchas-f739bbae55f9>
  
  ## Excerpt:
  
  ```
  # This is just pseudo code. It won't actually work in Terraform.
  if var.give_neo_cloudwatch_full_access {
    resource "aws_iam_user_policy_attachment" "neo_cloudwatch_full" {
      user       = aws_iam_user.example[0].name
      policy_arn = aws_iam_policy.cloudwatch_full_access.arn
    }
  } else {
    resource "aws_iam_user_policy_attachment" "neo_cloudwatch_read" {
      user       = aws_iam_user.example[0].name
      policy_arn = aws_iam_policy.cloudwatch_read_only.arn
    }
  }
  
  ```
  
  To do this in Terraform, you can use the count parameter and a conditional expression on each of the resources:
  
  ```
  resource "aws_iam_user_policy_attachment" "neo_cloudwatch_full" {
    count = var.give_neo_cloudwatch_full_access ? 1 : 0
    user       = aws_iam_user.example[0].name
    policy_arn = aws_iam_policy.cloudwatch_full_access.arn
  }
  resource "aws_iam_user_policy_attachment" "neo_cloudwatch_read" {
    count = var.give_neo_cloudwatch_full_access ? 0 : 1
    user       = aws_iam_user.example[0].name
    policy_arn = aws_iam_policy.cloudwatch_read_only.arn
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Thank you for this answer, but it's not quite what I'm looking for, because I want to use a boolean to decide if we're creating a resource or sourcing a data source. It may be the best answer, though - we'll see! :D  
  > **username_0**: It really seems what I was seeking is just not possible via the current version of HCL. But, I "accepted" this answer because this is likely the closest we'll get to what I wanted. Thanks!  
  
  ---
  
  