Setup on AWS EC2
To write and execute shell scripts effectively in a real-world DevOps workflow, a dedicated Linux environment is required. This chapter covers provisioning an Ubuntu-based Amazon EC2 instance on Amazon Web Services (AWS) under the Free Tier, configuring key-pair authentication, and exploring local environment alternatives such as WSL and VirtualBox.
1. Why Do We Need a Dedicated Execution Environment?
Before writing shell scripts, you must establish a stable Linux environment. While local machines running macOS or Windows can run bash scripts via terminal emulators or subsystems, using a cloud server closely mimics real-world enterprise deployments.
- Production Parity: DevOps engineers write scripts intended to run on production servers, which are predominantly cloud-hosted Linux instances.
- Isolation: Testing system-level commands, user management, and package installations on a cloud instance prevents accidental corruption of your personal machine.
- Consistency: Standardizing on Ubuntu ensures all commands, paths, and package managers (
apt) behave identically across different team members' environments.
2. AWS EC2 Instance Creation Step-by-Step
Setting up an Amazon Elastic Compute Cloud (EC2) instance involves configuring specific options in the AWS Management Console to stay within the Free Tier limits.
Step 1: Launch Instance Configuration
- Navigate to the AWS Management Console and open the EC2 Dashboard.
- Click on Launch Instance.
- Name Tag: Assign a descriptive name to your server, e.g.,
Shell Scripting for DevOps.
Step 2: Select OS Image (AMI)
- Operating System: Select Ubuntu (e.g., Ubuntu Server 22.04 LTS or 24.04 LTS).
- Rationale: Ubuntu is the industry standard distribution for beginner-to-advanced Linux and DevOps workflows due to its extensive package repositories and active community support.
Step 3: Choose Instance Type
- Instance Type: Select
t2.micro. - Cost Tier:
t2.microprovides 1 vCPU and 1 GB Memory, which is Free Tier Eligible (750 hours per month for the first 12 months).
Note: If
t2.microdoes not appear in your instance type selection dropdown, check your active AWS Region in the top-right corner of the console (e.g., switch tous-east-1N. Virginia orap-south-1Mumbai) wheret2.microis fully supported.
3. Environment Alternatives (AWS vs. Local)
If you do not have an AWS account or want to practice offline, alternative environments can be configured on your local machine.
4. Key Pair Generation & Security Configuration
Secure Shell (SSH) access to cloud servers requires cryptographic key pairs rather than traditional passwords.
Key Pair Setup Steps
- In the Key Pair (Login) section, click Create new key pair.
- Key Pair Name: Enter
shell-scripting-for-devops-key. - Key Pair Type: Select RSA.
- Private Key File Format: Select
.pem(suitable for OpenSSH on Linux, macOS, and Windows PowerShell). - Click Create key pair.
The .pem file will automatically download to your local machine. Keep this file secure, as AWS does not store a copy of your private key.
5. Environment Provisioning Flow
The following sequence illustrates the overall workflow from AWS navigation to instance execution:
6. Real-World Debugging & Common Setup Pitfalls
Pitfall 1: t2.micro Unavailable in Selected Region
- Symptom:
t2.microis greyed out or missing from the Instance Type list. - Fix: Switch your AWS region in the top navigation bar to a region like N. Virginia (
us-east-1) or Ohio (us-2-east). Alternatively, selectt3.microif specified as Free Tier eligible in your active region.
Pitfall 2: Key Pair Permission Error (Unprotected Private Key File)
When connecting to your instance from a local Linux/macOS terminal using SSH, OpenSSH requires strict permissions on the .pem file.
# SSH connection attempt fails if file permissions are too open
chmod 400 shell-scripting-for-devops-key.pem
# Connect to the instance using its allocated Public IP
ssh -i shell-scripting-for-devops-key.pem ubuntu@<EC2-PUBLIC-IP>
Summary and Key Takeaways
- Dedicated Environment Needed: A cloud-based Linux environment simulates real enterprise DevOps infrastructure for running shell scripts.
- Ubuntu on AWS EC2: Ubuntu Server is chosen for its stability, wide usage, and seamless compatibility with package management.
- Free Tier Eligibility: Selecting the
t2.microinstance type allows you to run workloads for free within AWS limits. - Region Selection: If
t2.microis missing, changing the AWS region resolves availability constraints. - Local Fallbacks: WSL (Windows Subsystem for Linux) or VirtualBox can be used as local alternatives if AWS access is unavailable.
- RSA .pem Keys: Access to the instance is secured using asymmetric RSA key pairs downloaded in
.pemformat during setup.