Showing posts with label terraform. Show all posts
Showing posts with label terraform. Show all posts

Saturday, October 31, 2020

Terraform: For and If

Reference: https://www.concurrency.com/blog/july-2019/conditionals-and-for-in-terraform

In below article, if purpose of statement or <condition>?<true>:<false> is to filter, rather than flow control

For

In the newer versions of Terraform >= 0.12, Terraform now supports for expressions. This is mostly used for parsing preexisting lists and maps rather than generating ones. For example, we are able to convert all elements in a list of strings to upper case using this expression.

[for el in var.list : upper(el)]

The For iterates over each element of the list and returns the value of upper(el) for each element in form of a list. We can also use this expression to generate maps.

{for el in var.list : el => upper(el)}

In this case, the original element from list now correspond to their uppercase version.

Lastly, we can include an if statement as a filter in for expressions. Unfortunately, we are not able to use if in logical operations like the ternary operators we used before. The following state will try to return a list of all non-empty elements in their uppercase state.

[for el in var.list : upper(el) if el != ""]

All in all, the use of for expressions are much more limited compared to imperative programming languages. The two expressions introduced in this section, for and if are exclusively used for lists rather than flow control of the program. That is to be expected since Terraform follows the declarative programming paradigm by describing the logic of the computation rather than the control flow- describing what the program must accomplish rather than how that is accomplished.

Thursday, October 29, 2020

Terraform

 

External Data Source

https://registry.terraform.io/providers/hashicorp/external/latest/docs/data-sources/data_source

https://www.terraform.io/docs/configuration/syntax-json.html

https://stackoverflow.com/questions/63296646/iterate-through-list-of-dictionaries-to-create-terraform-resources-with-differen

variable query_dict {
    default = [
        {
            name = "query1"
            workgroup = "bar"
            query = "SELECT * FROM foo"   
        },
        {
            name = "query2"
            workgroup = "bar"
            query = "SELECT * FROM baz"   
        }
    ]
}
resource "aws_athena_named_query" "olap" {

  for_each = {for idx, query in var.query_dict: idx => query}
  
  name = each.value.name
  query = each.value.query
  database = "test"
  workgroup = each.value.workgroup
} 

In the above you create map with idx as a key:

{
  "0" = {
    "name" = "query1"
    "query" = "SELECT * FROM foo"
    "workgroup" = "bar"
  }
  "1" = {
    "name" = "query2"
    "query" = "SELECT * FROM baz"
    "workgroup" = "bar"
  }
}
https://github.com/hashicorp/terraform-provider-external/issues?q=is%3Aissue+is%3Aopen+list

https://technology.amis.nl/2020/02/24/set-terraform-resource-properties-from-an-element-in-a-list-retrieved-by-a-data-source-using-a-local-value/

https://stackoverflow.com/questions/51946982/dependency-on-local-file-creation

https://registry.terraform.io/providers/hashicorp/external/latest/docs/data-sources/data_source

https://paulbrice.com/terraform/python/2017/12/18/external-provider-terraform.html
Loop and if
https://blog.gruntwork.io/terraform-tips-tricks-loops-if-statements-and-gotchas-f739bbae55f9