Chapter 7. Advanced Topics

Now that you have an understanding of the basics of Compute Engine, let’s take a look at some of the more advanced features of Compute Engine, including startup scripts, custom images, and the metadata server.

Both startup scripts and custom images allow you to customize your Compute Engine instances with specific software, configurations, or even operating systems. The metadata server provides an instance access to information about itself, its owning project, and customizable key/value pairs.

Startup Scripts

In Chapter 1, you started a VM, logged into the machine, and installed Apache. Wouldn’t this process be much easier if you could avoid the manual software installation step? One way to avoid manual setup is through the use of a startup script. A startup script is a bash script that runs as the last step when a Compute Engine instance boots or restarts. The startup script can contain any number of commands that install software or libraries, create files, or update system users, with all commands running as the root user.

Let’s take a look at how to start an instance with a startup script first using gcloud compute and then the Compute Engine API.

gcloud compute

The gcloud compute instances create command allows you to start a Compute Engine VM with a startup script. The manner in which your script is stored influences how the command is used. The script can be stored in one of four ways: it can be provided as a literal string value within the ...

Get Google Compute Engine now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.