Skip to content

Instantly share code, notes, and snippets.

View khaliqgant's full-sized avatar
💯
👨🏿‍💻

Khaliq khaliqgant

💯
👨🏿‍💻
View GitHub Profile
@khaliqgant
khaliqgant / ternary.hcl
Created March 29, 2022 11:45
[Terraform Multi Line Ternary] Ternary condition that spans multiple lines #terraform #infrastructure
locals {
iam_access_creds = (
var.create ?
var.create_livewire_accesser ?
[module.iam[0].access_key_id, module.iam[0].access_key_secret, module.iam-restriction[0].access_key_id, module.iam-restriction[0].access_key_secret] :
[module.iam[0].access_key_id, module.iam[0].access_key_secret] :
[]
)
}
@khaliqgant
khaliqgant / tmpl.hcl
Last active March 29, 2022 11:47
[Terraform Template File] Template file with variables #terraform #infrastructure
data "template_file" "codebuild" {
template = file("${path.module}/policies/role-policy.json")
vars = {
aws_s3_bucket_arn = module.aws_s3_bucket.arn[0]
}
}
role-policy.json
@khaliqgant
khaliqgant / tuple.hcl
Created March 29, 2022 11:46
[Terraform Tuple->Set] Convert a tuple to a set #terraform #infrastructure
resource "aws_network_interface" "subnets" {
for_each = { for subnet in module.vpc.public_subnets : subnet.id => subnet }
subnet_id = each.value.key
}
// reference: https://selleo.com/til/posts/cnfrqv1ipl-foreach-over-tuples-in-terraform
@khaliqgant
khaliqgant / for_each.hcl
Created March 29, 2022 11:47
[Terraform Output] Output from for_each #terraform #infrastructure
# object
output "repo_ids" {
value = {
for idx,repo in aws_ecr_repository.repo : idx => repo.registry_id
}
}
# list
output elastic_ips {
value = [for eip in aws_eip.eip : eip.id]
@khaliqgant
khaliqgant / debug.hcl
Created March 29, 2022 11:48
[Terraform Debug] Debug Flag #terraform #infrastructure
TF_LOG=TRACE terraform foo
https://www.terraform.io/internals/debugging
@khaliqgant
khaliqgant / tags.hcl
Created March 29, 2022 11:49
[Terraform Merge] Merge Tags #terraform #infrastructure
tags = merge(
{
"Name" = var.instance_count > 1 || var.use_num_suffix ? format("%s${var.num_suffix_format}", var.name, count.index + 1) : var.name
},
var.tags,
)
@khaliqgant
khaliqgant / conditional.hcl
Created March 29, 2022 11:49
[Terraform Conditional Module] Conditional Module #terraform #infrastructure
Version 11
count = "${var.create_bucket == true ? 1 : 0}"
> version 12
count = var.db_username != "" ? 1 : 0
Version 13
count = var.create_bucket ? 1 : 0
@khaliqgant
khaliqgant / import.hcl
Created March 29, 2022 11:51
[Terraform Import] Import Module #terraform #infrastructure
terraform import -var-file="../prod.tfvars" 'module.ip-address.azurerm_public_ip.static_ip' /subscriptions/6ba1-4e5d-a23e-0e4d3a59208d/resourceGroups/MC_h-network-resources_h-cluster_northeurope/providers/Microsoft.Network/publicIPAddresses/h-ip
terraform import -var-file="../prod.tfvars" 'module.key-vault.azurerm_key_vault.main' /subscriptions/6ba1-4e5d-a23e-0e4d3a59208d/resourceGroups/h-resources/providers/Microsoft.KeyVault/vaults/h
@khaliqgant
khaliqgant / slack.command
Created March 31, 2022 14:48
[Slack Subscriptions] Slack Github Subscriptions #slack #github
/github subscribe MyCorp/infrastructure comments reviews
@khaliqgant
khaliqgant / jq.bash
Created April 21, 2022 10:14
[JQ Boolean to Integer] Convert a boolean to an integer #jq #cli
cat customers/develop.json | jq '. [] | select(.creates_infrastructure_foundation == true)' | jq 'if .run_edge then 1 else 0 end'
Source: https://www.garysieling.com/blog/jq-boolean-integer/