-
Notifications
You must be signed in to change notification settings - Fork 0
/
terraform.tf
186 lines (151 loc) · 5.37 KB
/
terraform.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
provider "aws" {
region = var.aws_region // Choose your preferred region
}
provider "tls" {}
locals {
ssh_key_name = "${var.desktop_name}-key"
local_tags = merge(var.tags, {
"CloudDesktop" = var.desktop_name
})
}
resource "aws_resourcegroups_group" "project_group" {
name = "${var.desktop_name}-resource-group"
description = "Resource group for all resources with Project tag matching ${var.desktop_name}"
resource_query {
query = jsonencode({
"ResourceTypeFilters": ["AWS::AllSupported"],
"TagFilters": [
{
"Key": "CloudDesktop",
"Values": [var.desktop_name]
}
]
})
}
}
resource "aws_key_pair" "cloud_desktop_key" {
key_name = local.ssh_key_name
public_key = file("~/.ssh/cloud-desktop-key.pub")
tags = merge(local.local_tags, {})
}
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
tags = merge(local.local_tags, {})
}
resource "aws_subnet" "public" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
map_public_ip_on_launch = true
tags = merge(local.local_tags, {})
}
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.main.id
tags = merge(local.local_tags, {})
}
resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
tags = merge(local.local_tags, {})
}
resource "aws_route_table_association" "public_association" {
subnet_id = aws_subnet.public.id
route_table_id = aws_route_table.public.id
}
resource "aws_security_group" "instance" {
vpc_id = aws_vpc.main.id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"] // Open SSH to the world (configure as needed)
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = merge(local.local_tags, {})
}
resource "aws_instance" "cloud_desktop" {
ami = var.ami // Amazon Linux 2023 AMI
instance_type = var.instance_type
key_name = aws_key_pair.cloud_desktop_key.key_name
subnet_id = aws_subnet.public.id
vpc_security_group_ids = [aws_security_group.instance.id]
associate_public_ip_address = true
root_block_device {
volume_size = var.instance_volume_size // Size in GB, adjust as needed
}
tags = merge(local.local_tags, {
Name = "CloudDesktop-${var.desktop_name}"
})
}
resource "null_resource" "setup_cloud_desktop" {
depends_on = [aws_instance.cloud_desktop]
provisioner "remote-exec" {
inline = [
# Update the system
"sudo dnf update -y", # Amazon Linux (adjust for other AMIs if needed)
# Install Git
"sudo dnf install -y git", # Amazon Linux (adjust for other AMIs if needed)
# Install Development Tools
"sudo dnf groupinstall -y \"Development Tools\"",
# Install Zsh
"sudo dnf install -y zsh",
# Change the default shell to Zsh for ec2-user
"sudo usermod -s $(which zsh) ec2-user",
# Install Oh My Zsh for ec2-user
"sudo -u ec2-user sh -c \"$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh) --unattended\"",
# Set Oh My Zsh theme to agnoster
"sudo -u ec2-user sed -i 's/ZSH_THEME=\"robbyrussell\"/ZSH_THEME=\"agnoster\"/' /home/ec2-user/.zshrc",
# Install AWS CLI v2
"curl \"https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip\" -o \"awscliv2.zip\"",
"unzip awscliv2.zip",
"sudo ./aws/install",
# Install Docker
"sudo dnf install docker -y",
"sudo systemctl enable docker",
"sudo systemctl start docker",
"sudo usermod -aG docker ec2-user",
# Install Minikube dependencies
"sudo dnf install -y conntrack",
# Download and install Minikube
"curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64",
"chmod +x minikube",
"sudo mv minikube /usr/local/bin/",
# Download and install kubectl
"curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl",
"chmod +x kubectl",
"sudo mv kubectl /usr/local/bin/",
# Verify installations
"docker --version",
"minikube version",
"kubectl version --client",
# Install Rust
"curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y",
# Install Terraform
"sudo yum install -y yum-utils shadow-utils",
"sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo",
"sudo yum -y install terraform",
# Install jq
"sudo dnf install -y jq", # Amazon Linux (adjust for other AMIs if needed)
# Install K9s
"curl -Lo k9s_Linux_amd64.tar.gz https://github.com/derailed/k9s/releases/download/v0.32.5/k9s_Linux_amd64.tar.gz",
"mkdir -p /tmp/k9s_install",
"tar -xzf k9s_Linux_amd64.tar.gz -C /tmp/k9s_install",
"sudo mv /tmp/k9s_install/k9s /usr/local/bin/",
"rm -f k9s_Linux_amd64.tar.gz",
"rm -rf /tmp/k9s_install",
]
connection {
type = "ssh"
host = aws_instance.cloud_desktop.public_ip
user = "ec2-user" # Adjust based on your AMI
private_key = file("~/.ssh/${local.ssh_key_name}")
}
}
}