Terraform module for KMS Key Rings and KMS Keys in GCP
This upgrade addresses changes in upgrading to Terraform Google Cloud Platform provider 4.0.0.
- Usage of
self_link
forgoogle_kms_key_ring
resource is no longer supported. Hence,output
of this module has been changed as follows:key_ring_link
is now removed in favor ofkey_ring
.symmetric_key_self_links
is now removed in favor ofsymmetric_keys
.asymmetric_key_self_links
is now removed in favor ofasymmetric_keys
.signature_key_self_links
is now removed in favor ofsignature_keys
.
This upgrade addresses The problem of "shifting all items" in an array.
-
Ensure you have run
terraform plan
&terraform apply
withkms_keyring
modulev2.0.0
first. -
Now change
kms_keyring
module version tov2.1.0
. -
Run
terraform plan
- Expect the plan to fail saying "Error: Instance cannot be destroyed".
- This error is expected.
- For this upgrade process to move through, you would need to set
lifecycle { prevent_destroy = false }
before proceeding.- Locate this module's source code in
.terraform/modules
directory and change the values all 3lifecycle { prevent_destroy = true }
lines fromtrue
tofalse
. - This step is only to let the
terraform plan
pass so that we can see the changes proposed by terrform. - By knowing the changes proposed by terraform, we will use
terraform state mv
to move the state positions so that we won't actually need to runterraform apply
at all. - Thus, the KMS keys & keyrings in GCP will not be destroyed/recreated following this guideline.
- Locate this module's source code in
- After setting the above to
false
, runterraform plan
again. - This time the plan will pass gracefully showing an equal number of
google_kms_crypto_key
resources will be destroyed and recreated under new named indexes. - We want to avoid any kind of destruction and/or recreation.
-
Move the terraform state positions:
- Notice the following that the plan says:
- Your existing symmetric_keys (let's say
SymmX
) will be destroyed and new symmetric_keys (let's saySymmY
) will be created. - Your existing asymmetric_keys (let's say
AsymmX
) will be destroyed and new asymmetric_keys (let's sayAsymmY
) will be created. - Your existing signature_keys (let's say
SignX
) will be destroyed and new signature_keys (let's saySignY
) will be created.
- Your existing symmetric_keys (let's say
- P.S. if you happen to have multiple keys, then the plan will show these destructions and recreations multiple times - you will need to move the states for EACH of the respective resources one-by-one.
- Pay attention to the array indexes:
- The
*X
resources (the ones to be destroyed) start with array index[0]
- although it may not show the[0]
in the plan. - The
*Y
resources (the ones to be created) will show array indexes with new named indexes.
- The
- Use
terraform state mv
to manually move the states of each of*X
to*Y
- Refer to https://www.terraform.io/docs/commands/state/mv.html to learn more about how to move Terraform state positions
- Once a resource is moved, it will say Successfully moved 1 object(s).
- Repeat until all relevant states are moved to their desired positions.
- Notice the following that the plan says:
As per the named indexes produced by the terraform plan
above, a sample script for moving the states could look like this:
terraform state mv \
"module.kms_keyring.google_kms_crypto_key.symmetric_keys[0]" \
"module.kms_keyring.google_kms_crypto_key.symmetric_keys[\"SymmY\"]"
terraform state mv \
"module.kms_keyring.google_kms_crypto_key.asymmetric_keys[0]" \
"module.kms_keyring.google_kms_crypto_key.asymmetric_keys[\"AsymmY\"]"
terraform state mv \
"module.kms_keyring.google_kms_crypto_key.signature_keys[0]" \
"module.kms_keyring.google_kms_crypto_key.signature_keys[\"SignY\"]"
Upon succesful execution, it will produce an output like this:
Move "module.kms_keyring.google_kms_crypto_key.symmetric_keys[0]" to "module.kms_keyring.google_kms_crypto_key.symmetric_keys[\"SymmY\"]"
Successfully moved 1 object(s).
Move "module.kms_keyring.google_kms_crypto_key.asymmetric_keys[0]" to "module.kms_keyring.google_kms_crypto_key.asymmetric_keys[\"AsymmY\"]"
Successfully moved 1 object(s).
Move "module.kms_keyring.google_kms_crypto_key.signature_keys[0]" to "module.kms_keyring.google_kms_crypto_key.signature_keys[\"SignY\"]"
Successfully moved 1 object(s).
-
Now run
terraform plan
again- The plan should now show that no changes required
- This confirms that you have successfully moved all your resources' states to their new position as required by
v2.1.0
. - You should never have to run
terraform apply
for this upgrade exercise.
-
DONE