-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmain.tf
70 lines (57 loc) · 2.03 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
data "aws_ami" "ami" {
most_recent = true
filter {
name = "name"
values = ["${var.ami_name_pattern}"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["${var.ami_publisher}"]
}
data "aws_subnet" "first" {
id = "${var.public_subnet_ids[0]}"
}
data "aws_vpc" "vpc" {
id = "${data.aws_subnet.first.vpc_id}"
}
data "aws_region" "current" { }
data "template_file" "user_data" {
template = "${file("${path.module}/nat-user-data.conf.tmpl")}"
count = "${var.instance_count}"
vars = {
name = "${var.name}"
mysubnet = "${element(var.private_subnet_ids, count.index)}"
vpc_cidr = "${data.aws_vpc.vpc.cidr_block}"
region = "${data.aws_region.current.name}"
awsnycast_deb_url = "${var.awsnycast_deb_url}"
identifier = "${var.route_table_identifier}"
}
}
resource "aws_instance" "nat" {
count = "${var.instance_count}"
ami = "${data.aws_ami.ami.id}"
instance_type = "${var.instance_type}"
source_dest_check = false
iam_instance_profile = "${aws_iam_instance_profile.nat_profile.id}"
key_name = "${var.aws_key_name}"
subnet_id = "${element(var.public_subnet_ids, count.index)}"
vpc_security_group_ids = "${var.vpc_security_group_ids}"
tags = "${merge(var.tags, map("Name", format("%s-nat%d", var.name, count.index+1)))}"
user_data = "${element(data.template_file.user_data.*.rendered, count.index)}"
provisioner "remote-exec" {
inline = [
"while sudo pkill -0 cloud-init; do sleep 2; done",
]
connection {
user = "ubuntu"
# If we are using a bastion host ssh in via the private IP
# If we set this to an empty string we get the default behaviour.
host = "${var.ssh_bastion_host != "" ? self.private_ip : ""}"
private_key = "${var.aws_private_key}"
bastion_host = "${var.ssh_bastion_host}"
bastion_user = "${var.ssh_bastion_user}"
}
}
}