diff --git a/Makefile b/Makefile index cf044da2..84b88c06 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ build: hugo --buildDrafts --buildFuture --gc --minify --enableGitInfo serve: - hugo serve --buildDrafts --buildFuture --gc --enableGitInfo --ignoreCache --debug --disableFastRender + hugo serve --buildDrafts --buildFuture --gc --enableGitInfo --ignoreCache --disableFastRender clean: rm -rf public/ diff --git a/content/til/arch/custom-repository.md b/content/til/arch/custom-repository.md index 94fe0dd0..198cd953 100644 --- a/content/til/arch/custom-repository.md +++ b/content/til/arch/custom-repository.md @@ -1,5 +1,5 @@ --- -title: "Custom Repository" +title: "Hosting Custom Repository in S3" date: 2023-07-07 lastmod: 2023-07-07 draft: false diff --git a/content/til/terraform/_index.md b/content/til/terraform/_index.md new file mode 100644 index 00000000..3cb51fbc --- /dev/null +++ b/content/til/terraform/_index.md @@ -0,0 +1,3 @@ +--- +title: "Terraform" +--- diff --git a/content/til/terraform/for_each.md b/content/til/terraform/for_each.md new file mode 100644 index 00000000..6a2ce5ae --- /dev/null +++ b/content/til/terraform/for_each.md @@ -0,0 +1,81 @@ +--- +title: "for_each Patterns" +date: 2024-12-08 +lastmod: 2024-12-08 +draft: true +toc: true +--- + +## List of strings + +```hcl +locals { + servers = [ + "vm-1", + "vm-2", + "vm-3" + ] +} + +resource "example" "this" { + for_each = toset(local.servers) + + name = each.key +} +``` + +## List of objects + +```hcl +locals { + servers = [ + { + name = "vm-1" + ip = "10.0.0.5" + } + ] +} + +resource "example" "this" { + for_each = { + for i, vm in local.servers : vm.name => vm + } + + name = each.value.name + ip_address = each.value.ip +} +``` + +## Cartesian product of two lists + +```hcl +locals { + domains = [ + "https://google.com", + "https://example.com" + ] + paths = [ + "/foo", + "/bar", + "/bax" + ] +} + +resource "example" "this" { + urls = [for url in setproduct(locals.domains, locals.paths) : join("", url)] +} +``` + +## Conditional + +```hcl +resource "example" "this" { + for_each = var.create_example ? [] : [1] + name = ... + ip_address = ... +} +``` + +## References +- [How to for_each through list of objects in + Terraform](https://stackoverflow.com/questions/58594506/how-to-for-each-through-a-listobjects-in-terraform-0-12)