DEV Community

Ebenezer Lamptey
Ebenezer Lamptey

Posted on

1 1 1 1 1

Automate Your Amazon EC2 Instance Setup with LAMP Stack Using Bash

Introduction

Setting up an Amazon EC2 instance manually can be time-consuming, especially when configuring a LAMP stack (Linux, Apache, MySQL, PHP). This guide introduces a Bash script that simplifies the process, automating tasks such as AWS CLI installation, EC2 instance configuration, and LAMP stack setup. Whether you're new to AWS or a seasoned cloud engineer, this script can save you significant time and effort.

Prerequisites

Before running the script, ensure you have the following:

  • AWS account: Permissions to launch EC2 instances and configure security groups.
  • Key pair: Created in the AWS region you plan to use.
  • System requirements: A Debian-based Linux system with apt-get package manager.

Features

The script provides the following functionalities:

  1. AWS CLI Check & Installation: Verifies if the AWS CLI is installed and installs it if missing.
  2. Instance Launch: Automates EC2 instance creation with user-specified parameters.
  3. Security Group Configuration: Sets up security rules to allow SSH access from your current IP.
  4. LAMP Stack Installation: Installs and configures Apache, MySQL, and PHP on the instance.

Usage

  1. Clone or download the script.
  2. Make it executable:
   chmod +x setup_ec2.sh
Enter fullscreen mode Exit fullscreen mode
  1. Run the script:
   ./setup_ec2.sh
Enter fullscreen mode Exit fullscreen mode
  1. Follow the prompts to input:
    • AMI Image ID
    • Key Pair Name
    • Instance Type (e.g., t2.micro)
    • AWS Region
    • Security Group ID
    • Path to your private SSH key

Script Workflow

The script performs the following steps:

1. AWS CLI Check & Installation

If the AWS CLI is not found on your system, the script installs it using apt-get:

sudo apt-get update
sudo apt install -y awscli
Enter fullscreen mode Exit fullscreen mode

2. AWS CLI Configuration

The script configures the AWS CLI using your access credentials:

aws configure
Enter fullscreen mode Exit fullscreen mode

3. EC2 Instance Launch

Prompts you to enter required parameters, then launches an EC2 instance:

aws ec2 run-instances --image-id $image_id --key-name $key_name --instance-type $instance_type --region $region --query 'Instances[0].InstanceId' --output text
Enter fullscreen mode Exit fullscreen mode

4. Security Group Configuration

Sets up SSH access by authorizing your current IP:

aws ec2 authorize-security-group-ingress --group-id $security_group --protocol tcp --port 22 --cidr $(curl -s https://checkip.amazonaws.com)/32 --region $region
Enter fullscreen mode Exit fullscreen mode

5. Connect and Install LAMP Stack

Uses SSH to log in to the EC2 instance and install the LAMP stack:

ssh -i $PRIVATE_KEY_PATH ubuntu@$public_ip <<EOF
    sudo apt update && sudo apt -y upgrade
    sudo apt install -y apache2 mysql-server php libapache2-mod-php php-mysql
    sudo systemctl enable apache2
    sudo systemctl restart apache2
EOF
Enter fullscreen mode Exit fullscreen mode

Example Output

An example of what to expect when running the script:

2024-11-09 12:00:00 - AWS CLI not found. Installing...
2024-11-09 12:01:00 - Configuring AWS CLI
2024-11-09 12:02:00 - EC2 instance i-1234567890abcdef0 launched successfully.
2024-11-09 12:03:00 - EC2 instance public IP: 18.217.123.456
2024-11-09 12:04:00 - Connecting to EC2 instance via SSH...
2024-11-09 12:05:00 - Setting up LAMP server
2024-11-09 12:10:00 - LAMP setup completed on EC2 instance i-1234567890abcdef0
Enter fullscreen mode Exit fullscreen mode

Troubleshooting

  • AWS CLI Installation Fails: Ensure your system is Debian-based and supports apt-get.
  • Instance Launch Fails: Double-check your AWS credentials, region, and instance type.
  • SSH Connection Issues: Verify the private key path and ensure security group rules are configured correctly.

Conclusion

This Bash script streamlines the process of setting up an EC2 instance with a LAMP stack, automating repetitive tasks and reducing the potential for errors. By following the steps outlined in this guide, you can deploy a fully functional LAMP server in minutes. Try it out, and feel free to share feedback or suggestions for improvement!

see code here: Automate Your Amazon EC2 Instance Setup with LAMP Stack Using Bash

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay