main.tf 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. resource "aws_vpc" "main" {
  2. cidr_block = "${var.cidr}"
  3. enable_dns_hostnames = "${var.enable_dns_hostnames}"
  4. enable_dns_support = "${var.enable_dns_support}"
  5. tags {
  6. Name = "${var.name}"
  7. managed_by = "terraform"
  8. }
  9. }
  10. resource "aws_internet_gateway" "main" {
  11. vpc_id = "${aws_vpc.main.id}"
  12. tags {
  13. Name = "${var.name}-igw"
  14. managed_by = "terraform"
  15. }
  16. }
  17. resource "aws_route_table" "public" {
  18. vpc_id = "${aws_vpc.main.id}"
  19. propagating_vgws = ["${var.public_propagating_vgws}"]
  20. tags {
  21. Name = "${var.name}-rt-public"
  22. managed_by = "terraform"
  23. }
  24. }
  25. resource "aws_route" "public_internet_gateway" {
  26. route_table_id = "${aws_route_table.public.id}"
  27. destination_cidr_block = "0.0.0.0/0"
  28. gateway_id = "${aws_internet_gateway.main.id}"
  29. }
  30. resource "aws_route" "private_nat_gateway" {
  31. route_table_id = "${aws_route_table.private.id}"
  32. destination_cidr_block = "0.0.0.0/0"
  33. nat_gateway_id = "${aws_nat_gateway.natgw.id}"
  34. }
  35. resource "aws_route_table" "private" {
  36. vpc_id = "${aws_vpc.main.id}"
  37. propagating_vgws = ["${var.private_propagating_vgws}"]
  38. tags {
  39. Name = "${var.name}-rt-private-${data.aws_availability_zones.available.names[1]}"
  40. managed_by = "terraform"
  41. }
  42. }
  43. resource "aws_subnet" "public" {
  44. vpc_id = "${aws_vpc.main.id}"
  45. cidr_block = "${var.public_subnet}"
  46. availability_zone = "${data.aws_availability_zones.available.names[0]}"
  47. tags {
  48. Name = "${var.name}-subnet-public-${data.aws_availability_zones.available.names[0]}"
  49. managed_by = "terraform"
  50. }
  51. map_public_ip_on_launch = "${var.map_public_ip_on_launch}"
  52. }
  53. resource "aws_subnet" "private" {
  54. vpc_id = "${aws_vpc.main.id}"
  55. cidr_block = "${var.private_subnet}"
  56. availability_zone = "${data.aws_availability_zones.available.names[1]}"
  57. tags {
  58. Name = "${var.name}-rt-private-${data.aws_availability_zones.available.names[1]}"
  59. managed_by = "terraform"
  60. }
  61. }
  62. resource "aws_eip" "nateip" {
  63. vpc = true
  64. }
  65. resource "aws_nat_gateway" "natgw" {
  66. allocation_id = "${aws_eip.nateip.id}"
  67. subnet_id = "${aws_subnet.public.id}"
  68. depends_on = ["aws_internet_gateway.main"]
  69. }
  70. resource "aws_route_table_association" "public" {
  71. subnet_id = "${aws_subnet.public.id}"
  72. route_table_id = "${aws_route_table.public.id}"
  73. }
  74. resource "aws_route_table_association" "private" {
  75. subnet_id = "${aws_subnet.private.id}"
  76. route_table_id = "${aws_route_table.private.id}"
  77. }
  78. resource "aws_security_group" "inbound" {
  79. name = "allow-ssh-wnb"
  80. vpc_id = "${aws_vpc.main.id}"
  81. description = "Allows external incoming ssh and web traffic"
  82. ingress {
  83. from_port = 22
  84. to_port = 22
  85. protocol = "tcp"
  86. cidr_blocks = ["0.0.0.0/0"]
  87. }
  88. ingress {
  89. from_port = 80
  90. to_port = 80
  91. protocol = "tcp"
  92. cidr_blocks = ["0.0.0.0/0"]
  93. }
  94. ingress {
  95. from_port = 8080
  96. to_port = 8080
  97. protocol = "tcp"
  98. cidr_blocks = ["0.0.0.0/0"]
  99. }
  100. ingress {
  101. from_port = 443
  102. to_port = 443
  103. protocol = "tcp"
  104. cidr_blocks = ["0.0.0.0/0"]
  105. }
  106. egress {
  107. from_port = 0
  108. to_port = 0
  109. protocol = -1
  110. cidr_blocks = ["0.0.0.0/0"]
  111. }
  112. lifecycle {
  113. create_before_destroy = true
  114. }
  115. }