terraform-google-modules / terraform-docs-samples

Terraform samples intended for inclusion in cloud.google.com
Apache License 2.0
282 stars 207 forks source link

feat: automated terraform remote backend template #660

Closed ferrarimarco closed 2 months ago

ferrarimarco commented 2 months ago

Description

In this example, we show how to provision a Cloud Storage bucket and then generate a Terraform backend configuration file.

Checklist

Readiness

Style

Testing

Intended location

API enablement

Review

snippet-bot[bot] commented 2 months ago

Here is the summary of changes.

You are about to add 3 region tags. - [storage/remote_terraform_backend_template/main.tf:15](https://github.com/terraform-google-modules/terraform-docs-samples/blob/efce2d54ebf6b98e6e9dc8640fe6d1f13a836ad7/storage/remote_terraform_backend_template/main.tf#L15), tag `storage_remote_terraform_backend_template` - [storage/remote_terraform_backend_template/main.tf:16](https://github.com/terraform-google-modules/terraform-docs-samples/blob/efce2d54ebf6b98e6e9dc8640fe6d1f13a836ad7/storage/remote_terraform_backend_template/main.tf#L16), tag `storage_bucket_tf_with_versioning_pap_uap_no_destroy` - [storage/remote_terraform_backend_template/main.tf:35](https://github.com/terraform-google-modules/terraform-docs-samples/blob/efce2d54ebf6b98e6e9dc8640fe6d1f13a836ad7/storage/remote_terraform_backend_template/main.tf#L35), tag `storage_remote_backend_local_file`

This comment is generated by snippet-bot. If you find problems with this result, please file an issue at: https://github.com/googleapis/repo-automation-bots/issues. To update this comment, add snippet-bot:force-run label or use the checkbox below:

ferrarimarco commented 2 months ago

The build is failing on terraform destroy because this example generates a file to configure a gcs backend after running terraform apply. Thus, it needs to run terraform init -migrate-state to make use of the gcs state after generating that file, as we'll describe in the docs.

Considering that force_destroy is set to false for the Cloud Storage bucket that we configure as a remote backend, deletion will likely fail even if we run terraform init -migrate-state after running terraform apply because that bucket is going to be non-empty, and I would like to leave forse_destroy set to false.

Given that there should be a copy of the local state in the working directory because the first terraform apply run used a local backend, I would suggest running command right after running terraform apply to delete the generated backend.tf file. Does the test framework support this scenario? I'm not familiar with blueprint-test, so I don't know the answer to this question :(

glasnt commented 2 months ago

If this is what a user should be doing to use this functionality, but CI can't do it, we can add a flag to validate the Terraform but not apply/destroy it in CI.

I've also made some style updates, and moved the additional instructions to a README (these would appear inline in the region tag, when they should be part of the docs around the region tag)

ferrarimarco commented 2 months ago

Thanks for reviewing, and for the fixes.

The lint workflow is failing and references a non-existing Make target:

Error: Documentation generation has not been run, please run the
'make docker_generate_docs' command and commit the above changes.

The Cloud Build job is failing, but I don't see details about the failure in the log, besides the check name :(

Thanks for your help here @glasnt !

glasnt commented 2 months ago

The README issue is from the CFT system, which expects a very specific format. Having a sample README is unusual, so I will update it just so the lint checks are ok.

The Cloud Build test failures are unrelated to this change. I'll re-run them when I update the branch

ferrarimarco commented 2 months ago

Thanks @glasnt !

apeabody commented 2 months ago

If this is what a user should be doing to use this functionality, but CI can't do it, we can add a flag to validate the Terraform but not apply/destroy it in CI.

I've also made some style updates, and moved the additional instructions to a README (these would appear inline in the region tag, when they should be part of the docs around the region tag)

@glasnt This would be a good use case for the new tf "plan" validation (no apply/destroy) option: https://github.com/GoogleCloudPlatform/cloud-foundation-toolkit/pull/2258/files

Would require updates to the local repo and sample_test.go, perhaps store the validation plan under the relevant samples? I know of several other examples which are currently skipped for which this would be useful.