main.tf 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. ///////////////////////////////////////////////////////////////////////
  2. // RESOURCES
  3. ///////////////////////////////////////////////////////////////////////
  4. #-------------
  5. # DNS Entry for Cloud Customer
  6. #-------------
  7. #resource "aws_route53_record" "dns" {
  8. # zone_id = "Z1EFT3O5K9NMCJ" // Zone ID for nuxeocloud.com
  9. # name = "${name}"
  10. # type = "CNAME"
  11. # ttl = "300"
  12. # weighted_routing_policy {
  13. # weight = 90
  14. # }
  15. # set_identifier = "${var.stack_name}"
  16. # records = ["${var.stack_name}.nuxeocloud.com"]
  17. #}
  18. #-------------
  19. # CREATE SUBNETS
  20. #-------------
  21. resource "aws_subnet" "public" {
  22. vpc_id = "${var.vpc_id}"
  23. cidr_block = "${var.public_subnets[count.index]}"
  24. availability_zone = "${var.region}${var.azs[count.index]}"
  25. count = "${length(var.public_subnets)}"
  26. tags {
  27. Name = "${var.stack_name}-subnet-public-${var.region}${element(var.azs, count.index)}"
  28. billing-category = "customers"
  29. billing-subcategory = "${var.stack_name}"
  30. role = "nuxeo.aws-subnet"
  31. }
  32. map_public_ip_on_launch = "true"
  33. }
  34. resource "aws_subnet" "private" {
  35. vpc_id = "${var.vpc_id}"
  36. cidr_block = "${var.private_subnets[count.index]}"
  37. #availability_zone = "${var.region}${var.azs[count.index]}"
  38. count = "${length(var.private_subnets)}"
  39. tags {
  40. Name = "${var.stack_name}-subnet-private-${var.region}${element(var.azs, count.index)}"
  41. billing-category = "customers"
  42. billing-subcategory = "${var.stack_name}"
  43. role = "nuxeo.aws-subnet"
  44. }
  45. }
  46. resource "aws_subnet" "db_private" {
  47. vpc_id = "${var.vpc_id}"
  48. cidr_block = "${var.private_db_subnets[count.index]}"
  49. availability_zone = "${var.region}${var.azs[count.index]}"
  50. count = "${length(var.private_db_subnets)}"
  51. tags {
  52. Name = "${var.stack_name}-db-subnet-private-${var.region}${element(var.azs, count.index)}"
  53. billing-category = "customers"
  54. billing-subcategory = "${var.stack_name}"
  55. role = "nuxeo.aws-subnet"
  56. }
  57. }
  58. #-------------
  59. # CREATE NAT GATEWAY
  60. #-------------
  61. # > Create EIP to associate to NAT GW
  62. resource "aws_eip" "nateip" {
  63. vpc = true
  64. count = "${length(var.private_subnets) * lookup(map(var.enable_nat_gateway, 1), "true", 0)}"
  65. tags {
  66. Name = "${var.vpc_id}-nat-eip"
  67. billing-category = "customers"
  68. role = "nuxeo.aws-nat_eip"
  69. }
  70. }
  71. # > Create NAT GW
  72. resource "aws_nat_gateway" "natgw" {
  73. allocation_id = "${element(aws_eip.nateip.*.id, count.index)}"
  74. subnet_id = "${element(aws_subnet.public.*.id, count.index)}"
  75. count = "${length(var.private_subnets) * lookup(map(var.enable_nat_gateway, 1), "true", 0)}"
  76. tags {
  77. Name = "${var.stack_name}-natgw"
  78. billing-category = "customers"
  79. billing-subcategory = "${var.stack_name}"
  80. role = "nuxeo.aws-natgw"
  81. }
  82. depends_on = ["aws_internet_gateway.igw"]
  83. }
  84. # > Create IGW
  85. resource "aws_internet_gateway" "igw" {
  86. vpc_id = "${var.vpc_id}"
  87. }
  88. #-------------
  89. # CREATE ROUTES
  90. #-------------
  91. # > Route Tables
  92. # >> Route Table for Public Subnets
  93. resource "aws_route_table" "public" {
  94. vpc_id = "${var.vpc_id}"
  95. propagating_vgws = ["${var.public_propagating_vgws}"]
  96. tags {
  97. Name = "${var.stack_name}-rt-public"
  98. billing-category = "customers"
  99. billing-subcategory = "${var.stack_name}"
  100. role = "nuxeo.aws-rtb"
  101. }
  102. }
  103. # >> Route Table for Private Subnets
  104. resource "aws_route_table" "private" {
  105. vpc_id = "${var.vpc_id}"
  106. propagating_vgws = ["${var.private_propagating_vgws}"]
  107. count = "${length(var.private_subnets)}"
  108. tags {
  109. Name = "${var.stack_name}-rt-private-${element(var.azs, count.index)}"
  110. billing-category = "customers"
  111. billing-subcategory = "${var.stack_name}"
  112. role = "nuxeo.aws-rtb"
  113. }
  114. }
  115. # > Associations
  116. # >> for Private Subnets
  117. resource "aws_route_table_association" "private" {
  118. count = "${length(var.private_subnets)}"
  119. subnet_id = "${element(aws_subnet.private.*.id, count.index)}"
  120. route_table_id = "${element(aws_route_table.private.*.id, count.index)}"
  121. }
  122. # >> for Public Subnets
  123. resource "aws_route_table_association" "public" {
  124. count = "${length(var.public_subnets)}"
  125. subnet_id = "${element(aws_subnet.public.*.id, count.index)}"
  126. route_table_id = "${aws_route_table.public.id}"
  127. }
  128. # > Routes
  129. # >> Route for IGW
  130. resource "aws_route" "public_internet_gateway" {
  131. route_table_id = "${aws_route_table.public.id}"
  132. destination_cidr_block = "0.0.0.0/0"
  133. gateway_id = "${aws_internet_gateway.igw.id}"
  134. }
  135. # Route for NAT GW
  136. resource "aws_route" "private_nat_gateway" {
  137. route_table_id = "${element(aws_route_table.private.*.id, count.index)}"
  138. destination_cidr_block = "0.0.0.0/0"
  139. nat_gateway_id = "${element(aws_nat_gateway.natgw.*.id, count.index)}"
  140. count = "${length(var.private_subnets) * lookup(map(var.enable_nat_gateway, 1), "true", 0)}"
  141. }
  142. #-------------------
  143. # Security Groups
  144. #-------------------
  145. resource "aws_security_group" "sg_external_elb" {
  146. name = "${format("%s-sg-external-elb", var.stack_name)}"
  147. vpc_id = "${var.vpc_id}"
  148. description = "Allows external ELB traffic"
  149. ingress {
  150. from_port = 80
  151. to_port = 80
  152. protocol = "tcp"
  153. cidr_blocks = ["0.0.0.0/0"]
  154. }
  155. ingress {
  156. from_port = 443
  157. to_port = 443
  158. protocol = "tcp"
  159. cidr_blocks = ["0.0.0.0/0"]
  160. }
  161. egress {
  162. from_port = 0
  163. to_port = 0
  164. protocol = -1
  165. cidr_blocks = ["0.0.0.0/0"]
  166. }
  167. lifecycle {
  168. create_before_destroy = true
  169. }
  170. tags {
  171. Name = "${format("%s external elb", var.stack_name)}"
  172. billing-category = "customers"
  173. billing-subcategory = "${var.stack_name}"
  174. role = "nuxeo.aws-sg.external"
  175. }
  176. }
  177. resource "aws_security_group" "sg_internal" {
  178. name = "${format("%s-sg-internal-elb", var.stack_name)}"
  179. vpc_id = "${var.vpc_id}"
  180. description = "Allows external ELB traffic"
  181. ingress {
  182. from_port = 80
  183. to_port = 80
  184. protocol = "tcp"
  185. security_groups = ["${aws_security_group.sg_external_elb.id}"]
  186. }
  187. ingress {
  188. from_port = 8080
  189. to_port = 8080
  190. protocol = "tcp"
  191. security_groups = ["${aws_security_group.sg_external_elb.id}"]
  192. }
  193. ingress {
  194. from_port = 22
  195. to_port = 22
  196. protocol = "tcp"
  197. # TOFIX to replace by bastion host
  198. cidr_blocks = ["0.0.0.0/0"]
  199. }
  200. egress {
  201. from_port = 0
  202. to_port = 0
  203. protocol = -1
  204. cidr_blocks = ["0.0.0.0/0"]
  205. }
  206. lifecycle {
  207. create_before_destroy = true
  208. }
  209. tags {
  210. Name = "${format("%s internal elb", var.stack_name)}"
  211. billing-category = "customers"
  212. billing-subcategory = "${var.stack_name}"
  213. role = "nuxeo.aws-sg.internal"
  214. }
  215. }
  216. resource "aws_security_group" "external_ssh" {
  217. name = "${format("%s-sg-external-ssh", var.stack_name)}"
  218. description = "Allows ssh from the world"
  219. vpc_id = "${var.vpc_id}"
  220. ingress {
  221. from_port = 22
  222. to_port = 22
  223. protocol = "tcp"
  224. cidr_blocks = ["0.0.0.0/0"]
  225. }
  226. egress {
  227. from_port = 0
  228. to_port = 0
  229. protocol = "-1"
  230. cidr_blocks = ["0.0.0.0/0"]
  231. }
  232. lifecycle {
  233. create_before_destroy = true
  234. }
  235. tags {
  236. Name = "${format("%s external ssh", var.stack_name)}"
  237. billing-category = "customers"
  238. billing-subcategory = "${var.stack_name}"
  239. role= "nuxeo.aws-sg-ssh"
  240. }
  241. tags {
  242. }
  243. }
  244. #-------------------
  245. # ELB
  246. #-------------------
  247. resource "aws_elb" "elb" {
  248. name = "elb-${var.stack_name}"
  249. internal = true
  250. cross_zone_load_balancing = true
  251. subnets = ["${aws_subnet.public.id}"]
  252. security_groups = ["${aws_security_group.sg_external_elb.id}"]
  253. idle_timeout = 30
  254. connection_draining = true
  255. connection_draining_timeout = 15
  256. listener {
  257. lb_port = 80
  258. lb_protocol = "http"
  259. instance_port = 80
  260. instance_protocol = "http"
  261. }
  262. # listener {
  263. # lb_port = 443
  264. # lb_protocol = "https"
  265. # instance_port = 80
  266. # instance_protocol = "http"
  267. # #ssl_certificate_id = "${var.ssl_certificate_id}"
  268. # }
  269. health_check {
  270. healthy_threshold = 2
  271. unhealthy_threshold = 2
  272. timeout = 5
  273. target = "TCP:8080"
  274. interval = 30
  275. }
  276. # access_logs {
  277. # bucket = "${var.log_bucket}"
  278. # }
  279. tags {
  280. Name = "elb-${var.stack_name}"
  281. billing-category = "customers"
  282. billing-subcategory = "${var.stack_name}"
  283. role= "nuxeo.elb"
  284. }
  285. tags {
  286. }
  287. }