-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.tf
158 lines (136 loc) · 4.7 KB
/
main.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
locals {
# Directories start with "C:..." on Windows; All other OSs use "/" for root.
is_windows = substr(pathexpand("~"), 0, 1) == "/" ? false : true
}
provider "aws" {
region = var.region
access_key = var.access_key
secret_key = var.secret_key
token = var.token
}
resource "aws_security_group" "Hadoop_cluster_sc" {
name = "Hadoop_cluster_sc"
# inbound internet access
ingress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
# outbound internet access
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
lifecycle {
create_before_destroy = true
}
}
# namenode (master)
resource "aws_instance" "Namenode" {
count = var.namenode_count
ami = var.ami_image
instance_type = var.instance_type
key_name = var.aws_key_name
tags = {
Name = "s01"
}
private_ip = "172.31.0.101"
vpc_security_group_ids = [aws_security_group.Hadoop_cluster_sc.id]
provisioner "file" {
source = "install-all.sh"
destination = "/tmp/install-all.sh"
connection {
host = self.public_dns
type = "ssh"
user = "ubuntu"
private_key = file(var.amz_key_path)
}
}
provisioner "file" {
source = "app/"
destination = "/home/ubuntu/"
connection {
host = self.public_dns
type = "ssh"
user = "ubuntu"
private_key = file(var.amz_key_path)
}
}
provisioner "local-exec" {
interpreter = local.is_windows ? ["PowerShell"] : []
command = "cat ${var.key_path}/${var.key_name}.pub | ssh -o StrictHostKeyChecking=no -i ${var.amz_key_path} ubuntu@${self.public_dns} 'cat >> .ssh/authorized_keys'"
}
provisioner "local-exec" {
interpreter = local.is_windows ? ["PowerShell"] : []
command = "cat ${var.key_path}/${var.key_name}.pub | ssh -o StrictHostKeyChecking=no -i ${var.amz_key_path} ubuntu@${self.public_dns} 'cat >> .ssh/id_rsa.pub'"
}
provisioner "local-exec" {
interpreter = local.is_windows ? ["PowerShell"] : []
command = "cat ${var.key_path}/${var.key_name} | ssh -o StrictHostKeyChecking=no -i ${var.amz_key_path} ubuntu@${self.public_dns} 'cat >> .ssh/id_rsa'"
}
# execute the configuration script
provisioner "remote-exec" {
inline = [
"chmod +x /tmp/install-all.sh",
"/bin/bash /tmp/install-all.sh",
"/opt/hadoop-2.7.7/bin/hadoop namenode -format"
]
connection {
host = self.public_dns
type = "ssh"
user = "ubuntu"
private_key = file(var.amz_key_path)
}
}
}
# datanode (slaves)
resource "aws_instance" "Datanode" {
count = var.datanode_count
ami = var.ami_image
instance_type = var.instance_type
key_name = var.aws_key_name
tags = {
Name = lookup(var.hostnames, count.index)
}
private_ip = lookup(var.ips, count.index)
vpc_security_group_ids = [aws_security_group.Hadoop_cluster_sc.id]
# copy the initialization script to the remote machines
provisioner "file" {
source = "install-all.sh"
destination = "/tmp/install-all.sh"
connection {
host = self.public_dns
type = "ssh"
user = "ubuntu"
private_key = file(var.amz_key_path)
}
}
provisioner "local-exec" {
interpreter = local.is_windows ? ["PowerShell"] : []
command = "cat ${var.key_path}/${var.key_name}.pub | ssh -o StrictHostKeyChecking=no -i ${var.amz_key_path} ubuntu@${self.public_dns} 'cat >> .ssh/authorized_keys'"
}
provisioner "local-exec" {
interpreter = local.is_windows ? ["PowerShell"] : []
command = "cat ${var.key_path}/${var.key_name}.pub | ssh -o StrictHostKeyChecking=no -i ${var.amz_key_path} ubuntu@${self.public_dns} 'cat >> .ssh/id_rsa.pub'"
}
provisioner "local-exec" {
interpreter = local.is_windows ? ["PowerShell"] : []
command = "cat ${var.key_path}/${var.key_name} | ssh -o StrictHostKeyChecking=no -i ${var.amz_key_path} ubuntu@${self.public_dns} 'cat >> .ssh/id_rsa'"
}
# execute the configuration script
provisioner "remote-exec" {
inline = [
"chmod +x /tmp/install-all.sh",
"/bin/bash /tmp/install-all.sh",
]
connection {
host = self.public_dns
type = "ssh"
user = "ubuntu"
private_key = file(var.amz_key_path)
}
}
}