Skip to content

Instantly share code, notes, and snippets.

@mjmenger
Last active February 25, 2022 03:17
Show Gist options
  • Select an option

  • Save mjmenger/aa89490b418db22dadc41fad1ced67ff to your computer and use it in GitHub Desktop.

Select an option

Save mjmenger/aa89490b418db22dadc41fad1ced67ff to your computer and use it in GitHub Desktop.
terraform cli stunts
# create a .env file for use by the VS Code Rest Client extension
#
# extract the BIG-IP addresses from the Terraform output
export BIGIP_IPS=`terraform output --json | jq -cr '.bigip_mgmt_ips.value[]'`
export BIGIP_USER=admin
# extract the BIG-IP password from the Terraform state
export BIGIP_PASSWORD=`terraform show -json | jq .values.root_module.resources[] | jq -r 'select(.address | contains("random_password")).values.result'`
((i=0))
echo "#.env" > .env
for ip in $BIGIP_IPS; do
((i++))
echo bigip$i=$ip >> .env
done
echo user=admin >> .env
echo password=$BIGIP_PASSWORD >> .env

export TF_VAR_trusted_ip="$(dig +short myip.opendns.com @resolver1.opendns.com.)/32"

resource "local_file" "dotenv" {
content = <<EOF
bigip1=${module.bigip.mgmt_public_ips[0]}
bigip2=${module.bigip.mgmt_public_ips[1]}
user=admin
password=${random_password.password.result}
EOF
filename = "${path.module}/.env"
}
resource null_resource "webhook" {
provisioner "local-exec" {
command = <<-EOT
curl -s -k -X POST https://maker.ifttt.com/trigger/EVENT/with/key/iftttkey \
-H 'Content-Type: application/json' \
--max-time 600 \
--retry 50 \
--retry-delay 15 \
--retry-max-time 600 \
-d '{ "value1" : "${var.someterraformvar}", "value2" : "${var.anothervar}", "value3" : "" }'
EOT
}
}

If you've deleted everything with terragrunt or terraform and you want to save disk space on terraform providers and modules, you can try the following. YOU MAY LOSE STUFF THAT'S PRECIOUS TO YOU. IF YOU'RE NOT CERTAIN THEN DON'T DO THIS.

# remove the providers and modules you've downloaded
find . -type d -name ".terraform" -exec rm -rf "{}" \;
# remove the configuration lock file unless you don't want to 
find . -type f -name ".terraform.lock.hcl" -delete
# remove the state file because there's nothing to track the state of ... you hope
find . -type f -name "terraform.tfstate*" -delete
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -sha256 -days 365
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment