Cloud ServicesHow-Tos

[How to] write Startup Scripts for your VM Instance

How to series

To automate the installation of any software or OS Patches at the startup using Startup Scripts

1*5nwp7GR8d3 duML4VC m8A

When creating virtual machines (VMs), automating software installations and updates at startup can save time and streamline processes. This blog will guide you through using Startup Scripts to set up your VM instance and automate tasks like installing software or applying OS patches.

Let’s explore this step-by-step process with an example.

Step 1: Create a VM Instance

  1. Log into your Cloud Provider (e.g., Google Cloud Platform or AWS).
  2. Navigate to the VM Instances section.
  3. Click on Create Instance to start configuring your new VM.

Step 2: Select Your Desired Instance Configuration

Choose the required machine type, boot disk, and other specifications according to your needs.

Before proceeding:

  • Ensure you Allow HTTP traffic to make the server accessible from the internet. This setting is available under the network options.

Step 3: Add a Startup Script

Scroll down to the Advanced Options section and expand the Management tab. In the Automation section, paste your Startup Script.

Here’s a sample script for reference:

#!/bin/bash
apt update
apt -y install apache2
echo "Hello world from $(hostname) $(hostname -I)" > /var/www/html/index.html

Breaking Down the Script

  1. #!/bin/bash
    This is the shebang line, indicating that the script should be executed in the Bash shell.
  2. apt update
    Updates the package list to ensure you’re downloading the latest version of available packages.
  3. apt -y install apache2
    Installs the Apache2 web server without prompting for confirmation (-y automatically answers “yes” to prompts).
  4. echo "Hello world from $(hostname) $(hostname -I)" > /var/www/html/index.html
  • $(hostname) Fetches the hostname of the instance.
  • $(hostname -I) Retrieves the external IP of the VM.
  • The echo command writes the message into the default Apache web server directory (/var/www/html/index.html). When you access the server’s IP in a browser, this file is displayed as the homepage.

Step 4: Launch the VM

Click Create to launch your VM. Once the instance is created, the Startup Script will execute automatically.


Step 5: Verify the Setup

  1. Wait a few moments for the script to complete (this depends on the operations in your script).
  2. Navigate to your VM instance’s External IP address.
  • You can find this under the VM Instances list.

If everything is configured correctly, you should see a webpage displaying the message:
Hello world from [hostname] [external IP]


Conclusion

Using Startup Scripts simplifies the initial setup process for VMs. You can extend this approach to automate more complex tasks like configuring services, setting up databases, or applying patches. This ensures consistency and saves valuable time, especially when deploying multiple instances.

Ready to automate your next VM setup? Give Startup Scripts a try today! 🚀


A Note from the writer…

Welcome to my How-to Tech Blog Series, where I break down technical tasks into simple, actionable steps. Whether you’re coding in your favorite language, navigating the complexities of cloud technologies, or tackling day-to-day programming challenges, this series aims to provide clear and concise solutions. Stay tuned for quick guides that empower you to solve problems and enhance your skills!

Rajesh Mishra

I'm a developer who loves sharing insights, technical how-tos, and lessons learned from the world of code. While much of what I write may not be groundbreaking, I believe in documenting for future me—and for anyone else who might find it useful. Beyond tech, I also dive into life's experiences and moments, reflecting on personal growth and sharing stories that resonate. Whether you're here for practical tips or a fresh perspective on life, I hope you find something meaningful.

Leave a Reply

Your email address will not be published. Required fields are marked *