< All Topics

Setting the Stage: Create Ubuntu Lab on AWS with Terraform

Info: The code and process described here may change as the lab deployment evolves. Always check for the latest updates in the repository before proceeding.

Introduction

The iac (Infrastructure as Code) directory in my blocks-lab GitHub repository provides an easy way to create a basic Ubuntu server on AWS. This setup is ideal for beginners wanting to prepare a cloud-based lab environment. The process uses Terraform for automated deployment, making it efficient and repeatable.

Directory Overview

The iac directory contains several key files that work together to deploy your Ubuntu server:

  • Provider.tf – Sets the AWS provider, so Terraform knows how to connect to your AWS account.
  • vars.tf – Contains variable definitions, such as region, instance type, and other customizable parameters.
  • blocks-srv.tf – Defines the main Ubuntu EC2 instance resource (the “lab server”) and its configuration.
  • victim-host.tf – (Optional) Defines an additional EC2 instance, potentially for attack/defense scenarios or multi-host labs.
  • SecGrp.tf – Sets up security groups (firewall rules) for your instances.
  • Keypair.tf – Manages the SSH key pair for accessing your EC2 instances.
  • jumpstart.sh – A helper shell script to deploy ansible for the next steps with lab.

Setting Up Terraform on Linux

  1. Update your package list:
    sudo apt update
  2. Install required tools:
    sudo apt install -y wget unzip
  3. Download Terraform:
    wget https://releases.hashicorp.com/terraform/1.12.2/terraform_1.12.2_linux_amd64.zip
  4. Unzip and move to your PATH:
    unzip terraform_1.12.2_linux_amd64.zip
    sudo mv terraform /usr/local/bin/
  5. Check the installation:
    terraform version

Adding AWS Credentials with the AWS CLI

  1. Install the AWS CLI:
    sudo apt install -y awscli
  2. Configure your AWS access keys (from your AWS technical user):
    aws configure
    Enter your AWS Access Key ID, Secret Access Key, default region (e.g., us-east-1), and output format (e.g., json).

Deployment Steps

  1. Clone the repository and enter the iac directory:
    git clone https://github.com/ottolukacs/blocks-lab.git
    cd blocks-lab/iac
  2. Initialize Terraform:
    terraform init
  3. Optionally edit vars.tf to customize your instance.
  4. Review the execution plan:
    terraform plan
  5. Apply the deployment:
    terraform apply
    Type yes when prompted to confirm.
  6. After a few minutes, Terraform will show the public IP and connection details for your new Ubuntu lab server.

Conclusion

By using the iac directory and following these steps, you can quickly spin up an Ubuntu server in AWS for lab purposes. For further customization or troubleshooting, check the iac directory on GitHub.

Table of Contents