Skip to content

Commit

Permalink
new
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulmuhd-dev committed Jul 24, 2024
0 parents commit 1f0524e
Show file tree
Hide file tree
Showing 10 changed files with 363 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/validate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Terraform Code Test

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
terraform:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_version: 1.9.0

- name: Initialize Terraform
run: terraform init

- name: Validate Terraform configuration
run: terraform validate

# - name: Plan Terraform changes
# run: terraform plan -out=tfplan
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

# Terraform Configuration for Provisioning EC2 Remote Development Enviroment

![CI](https://github.com/abdulmuhd-dev/remote-dev-env/workflows/validate.yaml/badge.svg)

## Overview
This README provides instructions for using Terraform to automate the provisioning of an EC2 instance for development purposes on AWS.
It enables you to use visual studio locally to communicate to the remote enviroment using (Remote-SSH) plugin.

## Prerequisites
Before you begin, ensure you have the following:
- AWS account credentials with appropriate permissions.
- Terraform installed locally. [Install Terraform](https://learn.hashicorp.com/tutorials/terraform/install-cli).

## Configuration Steps
1. **Clone the Repository:**
```bash
git clone <repository-url>
cd <repository-directory>
```

2. **Initialize Terraform:**
```bash
terraform init
```

3. **Review and Modify Variables:**
- Open `variables.tf` or `terraform.tfvars` to review and update any required variables such as `aws_region`, `instance_type`, etc.
```hcl
# Example terraform.tfvars
dev_region = "us-east-1"
host_os = "unix" # Specify 'windows' if your host OS is Windows
public_key_path_with_filename = "~/.ssh/dev-env-key.pub"
private_key_path_with_filename = "~/.ssh/dev-env-key"
```
**Feel free to customize `terraform.tfvars` with your specific variable values.**

4. **Review and Apply Configuration:**
- Ensure `main.tf` aligns with your requirements.
- Apply the Terraform configuration:
```bash
terraform apply
```
Follow prompts and confirm with `yes`.

5. **Accessing the EC2 Instance:**
- After Terraform completes provisioning, access the EC2 instance using SSH or appropriate methods.

## Cleanup
- To remove resources managed by Terraform when no longer needed:
```bash
terraform destroy
```
Confirm with `yes` when prompted.

## Notes
- Verify AWS credentials are correctly set in your environment (`~/.aws/credentials` or environment variables).
- Adjust `main.tf` as needed for your specific deployment requirements.

## Additional Resources
For more information on Terraform and AWS:
- [Terraform Documentation](https://www.terraform.io/docs/index.html)
- [AWS Documentation](https://docs.aws.amazon.com/index.html)

This README guides you through setting up and managing an EC2 instance using Terraform, ensuring efficient and reproducible infrastructure provisioning for development environments on AWS. Adjustments to variables and configurations can be made as per your project's needs.
9 changes: 9 additions & 0 deletions datasource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
data "aws_ami" "node_os" {
most_recent = true
owners = ["099720109477"]

filter {
name = "name"
values = ["ubuntu-pro-server/images/hvm-ssd-gp3/ubuntu-noble-24.04-amd64-pro-server-*"]
}
}
118 changes: 118 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# Provider Block
provider "aws" {
profile = "default"
region = var.dev_region
}

# Resources Block

# VPC
resource "aws_vpc" "dev_vpc" {
cidr_block = var.dev_vpc_cidr_block
enable_dns_hostnames = var.dev_dns_hostnames
enable_dns_support = var.dev_dns_support
tags = {
Name = var.dev_vpc_tagname
}

}

# subnet
resource "aws_subnet" "dev_public_subnet" {
vpc_id = aws_vpc.dev_vpc.id
cidr_block = var.dev_public_subnet_cidr_block
map_public_ip_on_launch = true
availability_zone = "us-east-1a"

tags = {
Name = var.dev_public_subnet_tagname
}
}

# internet gatway
resource "aws_internet_gateway" "dev_igw" {
vpc_id = aws_vpc.dev_vpc.id

tags = {
Name = var.dev_igw_tagname
}
}

# route table
resource "aws_route_table" "dev_route_table" {
vpc_id = aws_vpc.dev_vpc.id

route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.dev_igw.id
}

tags = {
Name = "dev-route-table"
}
}

resource "aws_route_table_association" "dev_associate" {
route_table_id = aws_route_table.dev_route_table.id
subnet_id = aws_subnet.dev_public_subnet.id
}

# security groups
resource "aws_security_group" "dev_security_group" {
description = "security group of the dev"
vpc_id = aws_vpc.dev_vpc.id

ingress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = var.dev_sg_ingress_ips
}

egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = var.dev_sg_egress_ips
}

tags = {
Name = "dev-security-group"
}
}

# Ec2 key pairs
resource "aws_key_pair" "dev_key_pair" {
key_name = "dev-public-key-pair"
public_key = file(var.public_key_path_with_filename)
}

# Ec2 instance
resource "aws_instance" "dev_ec2_node" {
ami = data.aws_ami.node_os.image_id
instance_type = var.dev_ec2_instance_type
subnet_id = aws_subnet.dev_public_subnet.id
vpc_security_group_ids = [aws_security_group.dev_security_group.id]
key_name = aws_key_pair.dev_key_pair.id
user_data = file("user_data.tpl")

root_block_device {
volume_size = 10
}

provisioner "local-exec" {
command = templatefile("${var.host_os}_ssh_config.tpl", {
hostname = self.public_ip,
user = "ubuntu",
identityfile = var.private_key_path_with_filename
})

interpreter = var.host_os == "windows" ? ["powershell", "-Command"] : ["bash", "-c"]
}


tags = {
Name = var.dev_ec2_instance_tagname
}

}
4 changes: 4 additions & 0 deletions output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "ec2_publc_ip" {
description = "Print out the public IP address of deployed instance"
value = aws_instance.dev_ec2_node.public_ip
}
12 changes: 12 additions & 0 deletions terraform.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# aws region
dev_region = "us-east-1"

# The OS currently running on this machine use unix for both mac and linux
# host_os = "unix"
host_os = "windows"

# key pair public filename
public_key_path_with_filename = "~/.ssh/dev-env-key.pub"

# Key pair private key or identity filename
private_key_path_with_filename = "~/.ssh/dev-env-key"
7 changes: 7 additions & 0 deletions unix_ssh_config.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cat << EOF >> ~/.ssh/config

Host $(hostname)
Hostname $(hostname)
User $(user)
Identityfile $(identityfile)
EOF
13 changes: 13 additions & 0 deletions user_data.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
sudo apt-get update -y &&
sudo apt-get Install -y \
apt-transport-https \
ca-certificate \
curl \
gnupg-agent \
software-properties-common &&
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - &&
sudo add-apt-repository "deb [arch-amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" &&
sudo apt-get update -y &&
sudo sudo apt-get Install docker-ce docker-ce-cli containerd.io -y &&
sudo usermod -aG docker ubuntu
99 changes: 99 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# AWS Region
variable "dev_region" {
description = "Region"
type = string
default = "us-east-1"
}

# VPC Settings
variable "dev_vpc_cidr_block" {
description = "cidr block value for the Dev-ENV vpc"
type = string
default = "10.0.0.0/16"
}

variable "dev_vpc_tagname" {
description = "dev-ENV tag name"
type = string
default = "dev-vpc"
}

variable "dev_dns_hostnames" {
description = "dns hostname for 'the DEV-ENV VPC"
type = bool
default = true
}

variable "dev_dns_support" {
description = "dns support for dev Dev-ENV VPC"
type = bool
default = true
}

# Subnet Variables
variable "dev_public_subnet_cidr_block" {
description = "cidr block for dev-ENV public subnet"
type = string
default = "10.0.1.0/24"
}

variable "dev_public_subnet_tagname" {
description = "tagname value for dev-ENV public subnet"
type = string
default = "dev-public-subnet"
}

# Dev internet gateway
variable "dev_igw_tagname" {
description = "dev-Env internet gateway tagname"
type = string
default = "dev-igw"
}

# Dev security group ip
variable "dev_sg_ingress_ips" {
description = "IP addresses attach to ingress security group"
type = list(string)
default = ["0.0.0.0/0"]
}

variable "dev_sg_egress_ips" {
description = "IP addresses attach to egress security group"
type = list(string)
default = ["0.0.0.0/0"]
}

# Ec2 instance tagname
variable "dev_ec2_instance_tagname" {
description = "Dev-ENV Ec2 instance tagname"
type = string
default = "dev-EC2-node"
}

# EC2 instance type
variable "dev_ec2_instance_type" {
description = "Name value for the dev-ENV instance type"
type = string
default = "t2.micro"
}

# Host OS
variable "host_os" {
description = "the host os running"
type = string
default = "windows"
}

# Key_pair public key path with filename
variable "public_key_path_with_filename" {
description = "The path of the public key including the filename"
type = string
default = "~/.ssh/dev-env-key.pub"
}

# Key_pair private key identity file with filename
variable "private_key_path_with_filename" {
description = "Identity file to be use in ssh full path and file name"
type = string
default = "~/.ssh/dev-env-key"
}
6 changes: 6 additions & 0 deletions windows_ssh_config.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Set-Content -Path "~/.ssh/config" -Value @"
Host ec2-remote-dev-env
HostName ${hostname}
User ${user}
IdentityFile ${identityfile}
"@

0 comments on commit 1f0524e

Please sign in to comment.