Skip to content

fix(vm-instance, batch-job-template): let VM's service account read startup scripts - #6816

Open
scott-nag wants to merge 2 commits into
GoogleCloudPlatform:developfrom
nagconsulting:fix/vm-instance-startup-bucket-viewer
Open

scott-nag wants to merge 2 commits into
GoogleCloudPlatform:developfrom
nagconsulting:fix/vm-instance-startup-bucket-viewer

Conversation

@scott-nag

@scott-nag scott-nag commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

vm-instance: let the VM's service account read its startup scripts

Problem

modules/compute/vm-instance wraps its startup_script in a startup-script module (startup_from_network_storage.tf). That module stages the runners in a bucket the module creates, <deployment>-startup-scripts-<suffix>, and the VM downloads them at boot.

Only the default Compute Engine account, which usually has project Editor, can read that bucket. A VM given its own least-privilege account through service_account_email (for example from community/modules/iam/service-account-profiles) gets HTTP 403 on storage.objects.get. It never runs its startup script:

ERROR: (gcloud.storage.cp) HTTPError 403: <sa>@<project>.iam.gserviceaccount.com
does not have storage.objects.get access to ... /<deployment>-startup-scripts-<suffix>/early_run_hotfixes.sh-...
=== execution of early_run_hotfixes.sh-... failed, exiting ===

This hasn't shown up before because examples either leave the default account in place or use accounts with project-wide storage roles.

service-account-profiles makes least-privilege accounts the norm, so the gap now matters.

A blueprint can work around it only by granting the account read access to every bucket in the project, because the bucket name isn't an output of the module.

modules/scheduler/batch-job-template shares the same file and has the same problem for Batch VMs.

Change

In startup_from_network_storage.tf, which is kept identical in both modules (tools/duplicate-diff.py), pass the VM's account to the wrapped startup-script module as a bucket viewer.

Each module sets the account in its own main.tf, so the shared file stays identical:

  • vm-instance: startup_script_service_account = var.service_account_email
  • batch-job-template: startup_script_service_account = try(var.service_account.email, null)

The descriptions of service_account_email and service_account in both modules' variables and README tables now mention the read access.

Safety

  • No account set: nothing changes. bucket_viewers stays [], as it is today, when the default Compute Engine account is used.
  • A bucket you supply: nothing changes. startup-script applies bucket_viewers with google_storage_bucket_iam_binding, which is authoritative for roles/storage.objectViewer. Adding a member to a user-provided startup_script_path bucket would remove its other viewers, so the grant applies only to the bucket the module creates.
  • Existing deployments that set an account: the next apply adds one objectViewer member on that module's own bucket. Nothing is replaced.
  • Precedent: this follows examples/image-builder.yaml, which grants a profile's account bucket_viewers on its startup-script bucket.

Related

community/modules/remote-desktop/novnc-desktop stages its own runners in a second startup-script bucket and needs the same one-line grant for its service_account_email. That fix will be rolled into a separate PR (IAP Gateway handling in OFE - once #6751 lands).

…ad its startup scripts

Both modules wrap the startup script in a startup-script module whose
staging bucket only the default Compute Engine account could read, so a
VM given its own least-privilege account (e.g. from
service-account-profiles) got 403s fetching its runners and never ran its
startup script. Grant the module's service account read access through
bucket_viewers, only on the bucket the module creates: a user-provided
startup_script_path bucket is left as it is, since that binding is
authoritative.

startup_from_network_storage.tf stays identical in both modules
(tools/duplicate-diff.py); each module provides its account as
local.startup_script_service_account.
@scott-nag
scott-nag requested a review from a team as a code owner December 3, 2026 16:37
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a permissions issue where VM instances and Batch jobs using custom, least-privilege service accounts were unable to read their startup scripts from the module-generated GCS bucket. By explicitly passing the service account as a bucket viewer to the underlying startup-script module, the change ensures that the VM can successfully download and execute its startup scripts without requiring additional manual IAM configuration.

Highlights

  • Service Account Permissions: Updated the VM instance and batch job template modules to grant the configured service account read access to the automatically created startup-script GCS bucket.
  • Resolution of 403 Errors: Prevents HTTP 403 errors during VM boot when using least-privilege service accounts that previously lacked access to the startup script storage.
  • Documentation Updates: Updated variable descriptions in README files and variable definitions to reflect the new automatic bucket access grant.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the vm-instance and batch-job-template modules to grant the configured service account read access to the automatically created startup-script GCS bucket. It also updates the corresponding variable descriptions and README documentation. However, in both modules, if the service account is set to an empty string, the current condition will evaluate to true and attempt to add an invalid IAM member ("serviceAccount:"), which will cause a Terraform apply error. It is recommended to update the condition to also check that the service account is not an empty string.

Comment on lines +66 to +68
startup_script_bucket_viewers = (
local.startup_script_gcs_bucket_path == null && local.startup_script_service_account != null
) ? ["serviceAccount:${local.startup_script_service_account}"] : []

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If local.startup_script_service_account is set to an empty string (""), the condition local.startup_script_service_account != null will evaluate to true. This results in ["serviceAccount:"] being passed to bucket_viewers, which is an invalid IAM member format and will cause a Terraform apply error.

To prevent this, we should also check that the service account is not an empty string.

  startup_script_bucket_viewers = (
    local.startup_script_gcs_bucket_path == null && local.startup_script_service_account != null && local.startup_script_service_account != ""
  ) ? ["serviceAccount:${local.startup_script_service_account}"] : []

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

valid. applied the fix now.

Comment on lines +66 to +68
startup_script_bucket_viewers = (
local.startup_script_gcs_bucket_path == null && local.startup_script_service_account != null
) ? ["serviceAccount:${local.startup_script_service_account}"] : []

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If local.startup_script_service_account is set to an empty string (""), the condition local.startup_script_service_account != null will evaluate to true. This results in ["serviceAccount:"] being passed to bucket_viewers, which is an invalid IAM member format and will cause a Terraform apply error.

To prevent this, we should also check that the service account is not an empty string.

  startup_script_bucket_viewers = (
    local.startup_script_gcs_bucket_path == null && local.startup_script_service_account != null && local.startup_script_service_account != ""
  ) ? ["serviceAccount:${local.startup_script_service_account}"] : []

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see above!

@github-actions github-actions Bot added the external PR from external contributor label Dec 3, 2026
…or an empty service account

An empty service_account_email leaves the VM on the default Compute Engine
account, as before; granting "serviceAccount:" would fail the apply.
@ctk-pr-bot ctk-pr-bot added the release-bugfix Added to release notes under the "Bug fixes" heading. label Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external PR from external contributor release-bugfix Added to release notes under the "Bug fixes" heading.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants