Learn how to streamline your Terraform deployments on Google Cloud Platform by using local files for startup scripts instead of cumbersome inline scripts.
When managing infrastructure as code with Terraform, you might need to run custom commands or scripts when a new virtual machine instance starts up. This is particularly useful for tasks like installing software, configuring settings, or running other automated processes. This guide will walk you through the steps of creating and running a startup script on a Google Cloud Platform (GCP) instance using Terraform.
Create your startup script:
#!/bin/bash
# Your startup commands here
echo "Hello from startup script!" > /tmp/startup.log
Save this script to a file, for example, startup.sh
.
Make the script executable:
chmod +x startup.sh
Reference the script in your Terraform configuration:
resource "google_compute_instance" "default" {
# ... other instance configurations ...
metadata_startup_script = file("startup.sh")
}
This code snippet does the following:
metadata_startup_script
: This parameter within the google_compute_instance
resource is used to specify the startup script.file("startup.sh")
: This Terraform function reads the content of the startup.sh
file. The content is then passed to metadata_startup_script
to be executed on the instance startup.Now, when you apply this Terraform configuration, it will read the content of your startup.sh
file and execute it when the new GCP instance starts.
This code demonstrates how to use Terraform to create a Google Cloud Platform (GCP) instance that runs a startup script. The script updates the package lists, installs Nginx, and writes a message to a log file. The Terraform configuration defines the instance properties, including the startup script to be executed. After deploying the infrastructure using Terraform commands, a new GCP instance is created, and the startup script is executed, installing Nginx and leaving a log message on the instance.
This example demonstrates how to create a GCP instance with a startup script using Terraform.
1. Create the startup script (startup.sh):
#!/bin/bash
# Update package lists and install nginx
sudo apt update
sudo apt install -y nginx
# Write a message to a log file
echo "Hello from startup script! Nginx installed." > /tmp/startup.log
2. Make the script executable:
chmod +x startup.sh
3. Create the Terraform configuration file (main.tf):
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "~> 4.0"
}
}
}
resource "google_compute_instance" "default" {
name = "terraform-instance-startup-script"
machine_type = "n1-standard-1"
zone = "us-central1-a"
boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
}
}
network_interface {
network = "default"
}
# Apply the startup script
metadata_startup_script = file("startup.sh")
}
Explanation:
google_compute_instance
resource named "default".metadata_startup_script = file("startup.sh")
to read the content of startup.sh
and pass it to the instance.4. Deploy the infrastructure:
terraform init
terraform apply
This will create a new GCP instance and execute the startup.sh
script upon startup. You can verify the script execution by checking the /tmp/startup.log
file on the instance.
Note: This is a basic example. You can customize the startup script and Terraform configuration to fit your specific needs. For example, you can:
windows-startup-script-ps1
parameter is used in the Terraform configuration for this purpose.This guide explains how to execute a custom bash script when a new Google Cloud Platform (GCP) instance is created using Terraform.
Steps:
Create the Startup Script:
startup.sh
).#!/bin/bash
echo "Hello from startup script!" > /tmp/startup.log
Make the Script Executable:
chmod +x startup.sh
to grant execute permissions.Integrate with Terraform:
google_compute_instance
resource.metadata_startup_script
parameter within the resource block.file()
function to read the content of your script file (e.g., file("startup.sh")
).Example Terraform Snippet:
resource "google_compute_instance" "default" {
# ... other instance configurations ...
metadata_startup_script = file("startup.sh")
}
How it Works:
file("startup.sh")
.metadata_startup_script
parameter.This approach allows you to automate tasks like installing software, configuring settings, or running custom logic when your new GCP instance boots up.
By following these steps, you can leverage startup scripts to automate instance configuration on GCP using Terraform, making your infrastructure deployments more efficient and repeatable. Remember to prioritize security, error handling, and best practices for robust and maintainable infrastructure as code.