In order to authenticate to a private GCS bucket, we need to connect the Cloud CDN load balancer through a Backend Service, instead of a Backend Bucket. See Configure private origin authentication for more details.
As of April 2024, the
Terraform compute_backend_service
resource does not support setting security_settings.aws_v4_authentication, even though it should be.
Until this is supported, we have to set it manually via gcloud. Below are the steps, to be run AFTER the Terraform plan has been applied:
-
Start by updating your
gcloudcli, as older versions do not support settingsecuritySettings.awsV4Authentication:gcloud components update
-
Export the current backend service resource:
gcloud compute backend-services export cdn-backend-service --destination cdn-backend-service.yaml --global -
Prepend the
securitySettingsblock at the end of the file:... selfLink: ... sessionAffinity: ... timeoutSec: ... securitySettings: awsV4Authentication: accessKeyId: <access_key_id> accessKey: <access_key_secret> originRegion: us-central1
Refer to Manage HMAC keys for service accounts to create your HMAC access key.
- Save the new configuration:
gcloud compute backend-services import cdn-backend-service --source cdn-backend-service.yaml --global rm cdn-backend-service.yaml # delete as it contains secrets!
That's it!
Kudos to Navya Dwarakanath for her great article https://medium.com/@thetechbytes/private-gcs-bucket-access-through-google-cloud-cdn-430d940ebad9.
Warning
It is worth noting that, since we set up securitySettings.awsV4Authentication via gcloud, any future
updates to the Terraform compute_backend_service resource will fail. There is already an issue opened in this
regard: hashicorp/terraform-provider-google#16897.
To get unblocked, we need to follow the same steps as above to remove the securitySettings block, apply the
Terraform plan, then follow the steps again to re-add the securitySettings.awsV4Authentication. There will be CDN downtime during this operation. This is only an issue because we use Terraform.