AWS EC2: Automating Instance Setup For Different OS Images
October 5, 2023 • 7 min read

Image Banner

AWS EC2 (Elastic Compute Cloud) is a flexible and scalable cloud service that allows users to create virtual machines (instances) on demand. One of the key advantages of EC2 is the ability to automate the setup and configuration of instances, even when using different operating system (OS) images. This automation is crucial for ensuring consistency, security, and efficiency across your infrastructure.

Here's a note on how to automate instance setup for different OS images on AWS EC2:

Use EC2 User Data

  • EC2 User Data is a feature that allows you to provide scripts or metadata to your instances when they launch. You can use this to automate various setup tasks.
  • For Linux-based instances, you can use shell scripts, while for Windows-based instances, you can use PowerShell scripts.

The snippets below are for Amazon Linux and Ubuntu respectively

    #!/bin/bash
    # install httpd (Linux 2 version)
    yum update -y

    # Install Apache Server
    yum install -y httpd
    systemctl start httpd
    systemctl enable httpd

    echo "<h1>Hello World From Anthony's Amazon Linux @ $(hostname -f)</h1>" > /var/www/html/index.html
    #!/bin/bash

    # Update the package list and upgrade the installed packages
    sudo apt-get update
    sudo apt-get upgrade -y

    # Install essential packages
    sudo apt-get install -y software-properties-common
    sudo apt-get install -y python3-pip

    # Example: Install and configure a web server (Nginx)
    sudo apt-get install -y nginx
    sudo systemctl start nginx
    sudo systemctl enable nginx

    # Example: Install a database server (Postgresql)
    sudo apt-get install -y postgresql
    sudo systemctl status postgresql

    # Configure firewall rules (example: allow SSH and HTTP)
    # sudo ufw allow OpenSSH
    # sudo ufw allow 80/tcp
    # sudo ufw enable

    # Optional: Add your user to the sudo group if not done during instance creation
    # sudo usermod -aG sudo your-username

    # Clean up any unnecessary packages
    # sudo apt-get autoremove -y
    # sudo apt-get clean


    echo "<h1>Hello World From Anthony's Ubuntu @ $(hostname -f)</h1>" > /var/www/html/index.html

Operating System Agnostic Automation

  • To make your automation OS-agnostic, consider using tools like AWS Systems Manager (SSM) or cloud-init.
  • AWS Systems Manager provides a suite of services for automation, including Run Command for executing commands across instances and State Manager for defining and applying instance configurations.
  • cloud-init is a package available on many Linux distributions that allows for instance initialization. It can be used to configure settings, users, and packages during instance launch.

AMI (Amazon Machine Image) Customization

  • Customize your base OS image by creating a custom AMI. Install necessary software, apply security configurations, and create a new AMI. Then, use this AMI when launching instances.
  • This approach ensures that your instances start with a pre-configured OS that aligns with your requirements.

Infrastructure as Code (IaC)

  • Leverage Infrastructure as Code tools like AWS CloudFormation, Terraform, or AWS CDK (Cloud Development Kit) to define and provision your EC2 instances, including their setup.
  • These tools allow you to codify your infrastructure, making it version-controlled and repeatable.

By automating instance setup for different OS images on AWS EC2, you ensure that your infrastructure is consistent, reliable, and easily maintainable. This approach helps reduce human error, accelerates deployment, and enhances security and compliance across your cloud environment.


2024 Anthony Nwobodo