Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terraform tips #40

Open
dmarcoux opened this issue Sep 26, 2024 · 0 comments
Open

Terraform tips #40

dmarcoux opened this issue Sep 26, 2024 · 0 comments
Labels
article Topic for a blog article

Comments

@dmarcoux
Copy link
Owner

dmarcoux commented Sep 26, 2024

How to rename/move/update the state of Terraform resources

Explain this with an example on how to remove duplication in Terraform configuration file with for_each and how the state can be moved to match the changes in the configuration file.

  1. terraform state list to list all the resources in the Terraform state.
  2. terraform state mv -dry-run SOURCE DESTINATION to double-check if what would happen is really what we want
  3. If happy with the previous step, remove -dry-run from previous command, so terraform state mv SOURCE DESTINATION

As an extra, show how to loop in shell (with zsh) to avoid error-prone manual typing as much as possible:

for bucket_name in bookmarks recipes documents; do terraform state mv "scaleway_object_bucket_policy.bucket_policy_$bucket_name" "scaleway_object_bucket_policy.bucket_policy[\"$bucket_name\"]"; done

How to retrieve a sensitive value

When looking at the state of a resource, its sensitive values will always be masked. This is great to prevent security leaks, but what if, only once, I need to store this sensitive value in my password manager.

resource "scaleway_iam_application" "API-Object_Storage" {
  name        = "API - Object Storage"
}

resource "scaleway_iam_api_key" "API-Object_Storage" {
  application_id = scaleway_iam_application.API-Object_Storage.id
}

It's possible with the command below:

terraform show -json | jq -r '.values.root_module.resources[] | select(.address=="scaleway_iam_api_key.API-Object_Storage").values.secret_key'
@dmarcoux dmarcoux changed the title Blog article - How to rename/move/update the state of resources Blog article - Terraform tips Sep 26, 2024
@dmarcoux dmarcoux added the article Topic for a blog article label Oct 4, 2024
@dmarcoux dmarcoux changed the title Blog article - Terraform tips Terraform tips Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
article Topic for a blog article
Projects
None yet
Development

No branches or pull requests

1 participant