This project uses Terraform to set up a development environment on AWS. It includes the configuration of SSH access, user data scripts, and the provisioning of resources such as EC2 instances, VPC, and security groups.
.terraform/: Directory containing Terraform state and configuration files..gitignore: Git ignore file to exclude certain files and directories from version control..terraform.lock.hcl: Lock file to ensure consistent Terraform operations.datasources.tf: Terraform file defining data sources.linux-ssh-config.tpl: Template file for SSH configuration on Linux instances.main.tf: Main Terraform configuration file defining resources.outputs.tf: Terraform file defining outputs from the Terraform state.providers.tf: Terraform file defining provider configurations.terraform.tfstate: State file tracking the state of the infrastructure.terraform.tfstate.backup: Backup of the state file.terraform.tfvars: File containing variable values.userdata.tpl: Template file for user data scripts to configure instances.variables.tf: Terraform file defining input variables.windows-ssh-config.tpl: Template file for SSH configuration on Windows instances.README.md: This file, containing project documentation.
- Terraform installed on your local machine.
- AWS CLI configured with appropriate credentials and permissions.
-
Initialize Terraform:
terraform init
-
Review and Modify Variables: Ensure the
terraform.tfvarsfile contains the correct values for your environment. Example:host_os = "linux" home_ip = "123.456.789.0" ssh_public_path = "~/.ssh/id_rsa.pub" ssh_private_path = "~/.ssh/id_rsa"
-
Plan the Deployment: Review the execution plan for your infrastructure.
terraform plan
-
Apply the Configuration: Deploy the infrastructure.
terraform apply
This Terraform configuration sets up a complete development environment on AWS:
Configures the AWS provider to interact with AWS services.
Defines the main infrastructure resources:
- VPC with DNS support and hostnames enabled
- Public subnet with automatic public IP assignment
- Internet Gateway
- Route Table with default route
- Security Group allowing inbound traffic from a specific IP
- SSH Key Pair
- EC2 Instance with user data scripts and SSH access setup
Defines data sources to dynamically fetch data from existing infrastructure or cloud provider configurations.
Defines input variables:
host_os: Host operating system (linux or windows)home_ip: Home IP address for security group ingressssh_public_path: Path to SSH public keyssh_private_path: Path to SSH private key
Defines the output dev_ip, which provides the public IP address of the EC2 instance.
The EC2 instance is automatically configured using the userdata.tpl script. This script is executed when the instance is first launched and sets up the environment with Docker and other essential tools.
The userdata.tpl file contains a bash script that runs on instance launch. Here's a detailed breakdown of each command:
#!/bin/bashThis shebang line specifies that the script should be executed by the Bash shell.
sudo apt-get update -yUpdates the package lists for upgrades and new package installations. The -y flag automatically answers "yes" to prompts, allowing for non-interactive execution.
sudo apt-get install -y \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-commonInstalls necessary dependencies:
apt-transport-https: Allows the package manager to transfer files and data over httpsca-certificates: Allows SSL-based applications to check for the authenticity of SSL connectionscurl: A tool for transferring data using various protocolsgnupg-agent: GNU privacy guard - a tool for secure communication and data storagesoftware-properties-common: Provides an abstraction of the used apt repositories
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -Downloads Docker's GPG key and adds it to the system's keyring. This ensures that the Docker packages we'll install are authenticated.
-fsSL: Flags for curl (fail silently, show error, silent mode, follow redirects)
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"Adds the official Docker repository to the system:
deb: Specifies that it's a Debian-style repository[arch=amd64]: Specifies the architecture$(lsb_release -cs): Dynamically fetches the Ubuntu release codename (e.g., focal, bionic)stable: Indicates we're using the stable version of Docker
sudo apt-get update -yUpdates the package lists again, now including the newly added Docker repository.
sudo apt-get install docker-ce docker-ce-cli containerd.io -yInstalls Docker Community Edition:
docker-ce: The Docker daemon, which manages containersdocker-ce-cli: The Docker command-line interfacecontainerd.io: An industry-standard container runtime
sudo usermod -aG docker ubuntuAdds the ubuntu user to the docker group. This allows the user to run Docker commands without using sudo, which is more convenient and secure.
-a: Append the user to the group, don't remove from other groups-G: Specifies that we're modifying group membership
This script ensures that your EC2 instance is fully prepared for Docker-based development immediately after launch. It installs all necessary dependencies, sets up the Docker repository, installs Docker CE, and configures user permissions for Docker usage.
The project generates an SSH config file based on your host OS for easy connection to the EC2 instance.
For Linux/MacOS users:
ssh ubuntu@<dev_ip>
For Windows users, use PuTTY or Windows Subsystem for Linux (WSL) with the provided SSH key.
You can modify main.tf to adjust the configuration according to your needs, such as changing the region, instance type, or adding additional resources.
To destroy the infrastructure created by Terraform:
terraform destroyIf you encounter issues:
- Ensure all variables are correctly set in
terraform.tfvars - Check AWS credentials and permissions
- Verify that your IP address hasn't changed if you're having connection issues
For more help, please open an issue in the project repository.
- Add support for multiple environments (dev, staging, prod)
- Implement auto-scaling for the EC2 instances
- Add more comprehensive monitoring and logging
- Integrate with CI/CD pipelines