Skip to content

Commit 1f0524e

Browse files
committed
new
0 parents  commit 1f0524e

File tree

10 files changed

+363
-0
lines changed

10 files changed

+363
-0
lines changed

.github/workflows/validate.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Terraform Code Test
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
terraform:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v2
17+
18+
- name: Set up Terraform
19+
uses: hashicorp/setup-terraform@v1
20+
with:
21+
terraform_version: 1.9.0
22+
23+
- name: Initialize Terraform
24+
run: terraform init
25+
26+
- name: Validate Terraform configuration
27+
run: terraform validate
28+
29+
# - name: Plan Terraform changes
30+
# run: terraform plan -out=tfplan

README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
# Terraform Configuration for Provisioning EC2 Remote Development Enviroment
3+
4+
![CI](https://github.com/abdulmuhd-dev/remote-dev-env/workflows/validate.yaml/badge.svg)
5+
6+
## Overview
7+
This README provides instructions for using Terraform to automate the provisioning of an EC2 instance for development purposes on AWS.
8+
It enables you to use visual studio locally to communicate to the remote enviroment using (Remote-SSH) plugin.
9+
10+
## Prerequisites
11+
Before you begin, ensure you have the following:
12+
- AWS account credentials with appropriate permissions.
13+
- Terraform installed locally. [Install Terraform](https://learn.hashicorp.com/tutorials/terraform/install-cli).
14+
15+
## Configuration Steps
16+
1. **Clone the Repository:**
17+
```bash
18+
git clone <repository-url>
19+
cd <repository-directory>
20+
```
21+
22+
2. **Initialize Terraform:**
23+
```bash
24+
terraform init
25+
```
26+
27+
3. **Review and Modify Variables:**
28+
- Open `variables.tf` or `terraform.tfvars` to review and update any required variables such as `aws_region`, `instance_type`, etc.
29+
```hcl
30+
# Example terraform.tfvars
31+
dev_region = "us-east-1"
32+
host_os = "unix" # Specify 'windows' if your host OS is Windows
33+
public_key_path_with_filename = "~/.ssh/dev-env-key.pub"
34+
private_key_path_with_filename = "~/.ssh/dev-env-key"
35+
```
36+
**Feel free to customize `terraform.tfvars` with your specific variable values.**
37+
38+
4. **Review and Apply Configuration:**
39+
- Ensure `main.tf` aligns with your requirements.
40+
- Apply the Terraform configuration:
41+
```bash
42+
terraform apply
43+
```
44+
Follow prompts and confirm with `yes`.
45+
46+
5. **Accessing the EC2 Instance:**
47+
- After Terraform completes provisioning, access the EC2 instance using SSH or appropriate methods.
48+
49+
## Cleanup
50+
- To remove resources managed by Terraform when no longer needed:
51+
```bash
52+
terraform destroy
53+
```
54+
Confirm with `yes` when prompted.
55+
56+
## Notes
57+
- Verify AWS credentials are correctly set in your environment (`~/.aws/credentials` or environment variables).
58+
- Adjust `main.tf` as needed for your specific deployment requirements.
59+
60+
## Additional Resources
61+
For more information on Terraform and AWS:
62+
- [Terraform Documentation](https://www.terraform.io/docs/index.html)
63+
- [AWS Documentation](https://docs.aws.amazon.com/index.html)
64+
65+
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.

datasource.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
data "aws_ami" "node_os" {
2+
most_recent = true
3+
owners = ["099720109477"]
4+
5+
filter {
6+
name = "name"
7+
values = ["ubuntu-pro-server/images/hvm-ssd-gp3/ubuntu-noble-24.04-amd64-pro-server-*"]
8+
}
9+
}

main.tf

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# Provider Block
2+
provider "aws" {
3+
profile = "default"
4+
region = var.dev_region
5+
}
6+
7+
# Resources Block
8+
9+
# VPC
10+
resource "aws_vpc" "dev_vpc" {
11+
cidr_block = var.dev_vpc_cidr_block
12+
enable_dns_hostnames = var.dev_dns_hostnames
13+
enable_dns_support = var.dev_dns_support
14+
tags = {
15+
Name = var.dev_vpc_tagname
16+
}
17+
18+
}
19+
20+
# subnet
21+
resource "aws_subnet" "dev_public_subnet" {
22+
vpc_id = aws_vpc.dev_vpc.id
23+
cidr_block = var.dev_public_subnet_cidr_block
24+
map_public_ip_on_launch = true
25+
availability_zone = "us-east-1a"
26+
27+
tags = {
28+
Name = var.dev_public_subnet_tagname
29+
}
30+
}
31+
32+
# internet gatway
33+
resource "aws_internet_gateway" "dev_igw" {
34+
vpc_id = aws_vpc.dev_vpc.id
35+
36+
tags = {
37+
Name = var.dev_igw_tagname
38+
}
39+
}
40+
41+
# route table
42+
resource "aws_route_table" "dev_route_table" {
43+
vpc_id = aws_vpc.dev_vpc.id
44+
45+
route {
46+
cidr_block = "0.0.0.0/0"
47+
gateway_id = aws_internet_gateway.dev_igw.id
48+
}
49+
50+
tags = {
51+
Name = "dev-route-table"
52+
}
53+
}
54+
55+
resource "aws_route_table_association" "dev_associate" {
56+
route_table_id = aws_route_table.dev_route_table.id
57+
subnet_id = aws_subnet.dev_public_subnet.id
58+
}
59+
60+
# security groups
61+
resource "aws_security_group" "dev_security_group" {
62+
description = "security group of the dev"
63+
vpc_id = aws_vpc.dev_vpc.id
64+
65+
ingress {
66+
from_port = 0
67+
to_port = 0
68+
protocol = "-1"
69+
cidr_blocks = var.dev_sg_ingress_ips
70+
}
71+
72+
egress {
73+
from_port = 0
74+
to_port = 0
75+
protocol = "-1"
76+
cidr_blocks = var.dev_sg_egress_ips
77+
}
78+
79+
tags = {
80+
Name = "dev-security-group"
81+
}
82+
}
83+
84+
# Ec2 key pairs
85+
resource "aws_key_pair" "dev_key_pair" {
86+
key_name = "dev-public-key-pair"
87+
public_key = file(var.public_key_path_with_filename)
88+
}
89+
90+
# Ec2 instance
91+
resource "aws_instance" "dev_ec2_node" {
92+
ami = data.aws_ami.node_os.image_id
93+
instance_type = var.dev_ec2_instance_type
94+
subnet_id = aws_subnet.dev_public_subnet.id
95+
vpc_security_group_ids = [aws_security_group.dev_security_group.id]
96+
key_name = aws_key_pair.dev_key_pair.id
97+
user_data = file("user_data.tpl")
98+
99+
root_block_device {
100+
volume_size = 10
101+
}
102+
103+
provisioner "local-exec" {
104+
command = templatefile("${var.host_os}_ssh_config.tpl", {
105+
hostname = self.public_ip,
106+
user = "ubuntu",
107+
identityfile = var.private_key_path_with_filename
108+
})
109+
110+
interpreter = var.host_os == "windows" ? ["powershell", "-Command"] : ["bash", "-c"]
111+
}
112+
113+
114+
tags = {
115+
Name = var.dev_ec2_instance_tagname
116+
}
117+
118+
}

output.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "ec2_publc_ip" {
2+
description = "Print out the public IP address of deployed instance"
3+
value = aws_instance.dev_ec2_node.public_ip
4+
}

terraform.tfvars

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# aws region
2+
dev_region = "us-east-1"
3+
4+
# The OS currently running on this machine use unix for both mac and linux
5+
# host_os = "unix"
6+
host_os = "windows"
7+
8+
# key pair public filename
9+
public_key_path_with_filename = "~/.ssh/dev-env-key.pub"
10+
11+
# Key pair private key or identity filename
12+
private_key_path_with_filename = "~/.ssh/dev-env-key"

unix_ssh_config.tpl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cat << EOF >> ~/.ssh/config
2+
3+
Host $(hostname)
4+
Hostname $(hostname)
5+
User $(user)
6+
Identityfile $(identityfile)
7+
EOF

user_data.tpl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
sudo apt-get update -y &&
3+
sudo apt-get Install -y \
4+
apt-transport-https \
5+
ca-certificate \
6+
curl \
7+
gnupg-agent \
8+
software-properties-common &&
9+
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - &&
10+
sudo add-apt-repository "deb [arch-amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" &&
11+
sudo apt-get update -y &&
12+
sudo sudo apt-get Install docker-ce docker-ce-cli containerd.io -y &&
13+
sudo usermod -aG docker ubuntu

variables.tf

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# AWS Region
2+
variable "dev_region" {
3+
description = "Region"
4+
type = string
5+
default = "us-east-1"
6+
}
7+
8+
# VPC Settings
9+
variable "dev_vpc_cidr_block" {
10+
description = "cidr block value for the Dev-ENV vpc"
11+
type = string
12+
default = "10.0.0.0/16"
13+
}
14+
15+
variable "dev_vpc_tagname" {
16+
description = "dev-ENV tag name"
17+
type = string
18+
default = "dev-vpc"
19+
}
20+
21+
variable "dev_dns_hostnames" {
22+
description = "dns hostname for 'the DEV-ENV VPC"
23+
type = bool
24+
default = true
25+
}
26+
27+
variable "dev_dns_support" {
28+
description = "dns support for dev Dev-ENV VPC"
29+
type = bool
30+
default = true
31+
}
32+
33+
# Subnet Variables
34+
variable "dev_public_subnet_cidr_block" {
35+
description = "cidr block for dev-ENV public subnet"
36+
type = string
37+
default = "10.0.1.0/24"
38+
}
39+
40+
variable "dev_public_subnet_tagname" {
41+
description = "tagname value for dev-ENV public subnet"
42+
type = string
43+
default = "dev-public-subnet"
44+
}
45+
46+
# Dev internet gateway
47+
variable "dev_igw_tagname" {
48+
description = "dev-Env internet gateway tagname"
49+
type = string
50+
default = "dev-igw"
51+
}
52+
53+
# Dev security group ip
54+
variable "dev_sg_ingress_ips" {
55+
description = "IP addresses attach to ingress security group"
56+
type = list(string)
57+
default = ["0.0.0.0/0"]
58+
}
59+
60+
variable "dev_sg_egress_ips" {
61+
description = "IP addresses attach to egress security group"
62+
type = list(string)
63+
default = ["0.0.0.0/0"]
64+
}
65+
66+
# Ec2 instance tagname
67+
variable "dev_ec2_instance_tagname" {
68+
description = "Dev-ENV Ec2 instance tagname"
69+
type = string
70+
default = "dev-EC2-node"
71+
}
72+
73+
# EC2 instance type
74+
variable "dev_ec2_instance_type" {
75+
description = "Name value for the dev-ENV instance type"
76+
type = string
77+
default = "t2.micro"
78+
}
79+
80+
# Host OS
81+
variable "host_os" {
82+
description = "the host os running"
83+
type = string
84+
default = "windows"
85+
}
86+
87+
# Key_pair public key path with filename
88+
variable "public_key_path_with_filename" {
89+
description = "The path of the public key including the filename"
90+
type = string
91+
default = "~/.ssh/dev-env-key.pub"
92+
}
93+
94+
# Key_pair private key identity file with filename
95+
variable "private_key_path_with_filename" {
96+
description = "Identity file to be use in ssh full path and file name"
97+
type = string
98+
default = "~/.ssh/dev-env-key"
99+
}

windows_ssh_config.tpl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Set-Content -Path "~/.ssh/config" -Value @"
2+
Host ec2-remote-dev-env
3+
HostName ${hostname}
4+
User ${user}
5+
IdentityFile ${identityfile}
6+
"@

0 commit comments

Comments
 (0)