-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathec2.tf
116 lines (102 loc) · 2.34 KB
/
ec2.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
resource "tls_private_key" "ssh_key_etcd" {
algorithm = "RSA"
rsa_bits = "2048"
}
resource "aws_key_pair" "ssh_access_etcd" {
key_name = "Generated key for ETCD ${var.cluster_name}"
public_key = tls_private_key.ssh_key_etcd.public_key_openssh
}
data "aws_ami" "main" {
most_recent = true
owners = [
var.ami_owner_id
]
filter {
name = "name"
values = [
var.ami_name_filter,
]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "architecture"
values = [
var.ami_architecture
]
}
}
// Used to list all public subnets in the VPC.
data "aws_subnets" "public" {
filter {
name = "vpc-id"
values = [
var.vpc_id
]
}
filter {
name = "tag:type"
values = [
"public"
]
}
}
data "aws_subnets" "private" {
filter {
name = "vpc-id"
values = [
var.vpc_id
]
}
filter {
name = "tag:type"
values = [
"private"
]
}
}
// Used to pick a subnet for nodes
resource "random_id" "index" {
byte_length = 1
}
locals {
subnet_ids_list = tolist(data.aws_subnets.public.ids) // used to distrubute nodes in subnets
}
// Create etcd instances
resource "aws_instance" "etcds" {
count = var.node_count
ami = data.aws_ami.main.id
instance_type = var.instance_type
user_data = templatefile("${path.module}/etcd.sh.tpl", {
etcd_name = "etcd${count.index}",
etcd_domain = "${var.cluster_name}-etcd${count.index}.${var.domain_name}",
etcd_initial_cluster = join(",", [for i in range(var.node_count) : "etcd${i}=http://${var.cluster_name}-etcd${i}.${var.domain_name}:2380"]),
ssh_authorized_key = tls_private_key.ssh_key_etcd.public_key_openssh,
etcd_peer_url = "http://${var.cluster_name}-etcd${count.index}.${var.domain_name}:2380"
})
# storage
root_block_device {
volume_type = var.disk_type
volume_size = var.disk_size
iops = var.disk_iops
encrypted = true
}
#network
vpc_security_group_ids = [
aws_security_group.etcd.id
]
associate_public_ip_address = true
subnet_id = element(local.subnet_ids_list, count.index)
lifecycle {
ignore_changes = [
subnet_id,
ami,
user_data,
]
}
tags = merge(var.tags, {
Name = "${var.cluster_name}-${count.index}"
})
}