main.tf 3.3 KB

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