[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
![[How to] write Startup Scripts for your VM Instance 1 1*5nwp7GR8d3 duML4VC m8A](https://cdn-images-1.medium.com/max/800/1*5nwp7GR8d3_duML4VC_m8A.jpeg)
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
- Log into your Cloud Provider (e.g., Google Cloud Platform or AWS).
- Navigate to the VM Instances section.
- 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
#!/bin/bash
This is the shebang line, indicating that the script should be executed in the Bash shell.apt update
Updates the package list to ensure you’re downloading the latest version of available packages.apt -y install apache2
Installs the Apache2 web server without prompting for confirmation (-yautomatically answers “yes” to prompts).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
echocommand 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
- Wait a few moments for the script to complete (this depends on the operations in your script).
- 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!
