You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.9 KiB

  1. resource "aws_vpc" "cluster-vpc" {
  2. cidr_block = "${var.aws_vpc_cidr_block}"
  3. #DNS Related Entries
  4. enable_dns_support = true
  5. enable_dns_hostnames = true
  6. tags {
  7. Name = "kubernetes-${var.aws_cluster_name}-vpc"
  8. }
  9. }
  10. resource "aws_eip" "cluster-nat-eip" {
  11. count = "${length(var.aws_cidr_subnets_public)}"
  12. vpc = true
  13. }
  14. resource "aws_internet_gateway" "cluster-vpc-internetgw" {
  15. vpc_id = "${aws_vpc.cluster-vpc.id}"
  16. tags {
  17. Name = "kubernetes-${var.aws_cluster_name}-internetgw"
  18. }
  19. }
  20. resource "aws_subnet" "cluster-vpc-subnets-public" {
  21. vpc_id = "${aws_vpc.cluster-vpc.id}"
  22. count="${length(var.aws_avail_zones)}"
  23. availability_zone = "${element(var.aws_avail_zones, count.index)}"
  24. cidr_block = "${element(var.aws_cidr_subnets_public, count.index)}"
  25. tags {
  26. Name = "kubernetes-${var.aws_cluster_name}-${element(var.aws_avail_zones, count.index)}-public"
  27. }
  28. }
  29. resource "aws_nat_gateway" "cluster-nat-gateway" {
  30. count = "${length(var.aws_cidr_subnets_public)}"
  31. allocation_id = "${element(aws_eip.cluster-nat-eip.*.id, count.index)}"
  32. subnet_id = "${element(aws_subnet.cluster-vpc-subnets-public.*.id, count.index)}"
  33. }
  34. resource "aws_subnet" "cluster-vpc-subnets-private" {
  35. vpc_id = "${aws_vpc.cluster-vpc.id}"
  36. count="${length(var.aws_avail_zones)}"
  37. availability_zone = "${element(var.aws_avail_zones, count.index)}"
  38. cidr_block = "${element(var.aws_cidr_subnets_private, count.index)}"
  39. tags {
  40. Name = "kubernetes-${var.aws_cluster_name}-${element(var.aws_avail_zones, count.index)}-private"
  41. }
  42. }
  43. #Routing in VPC
  44. #TODO: Do we need two routing tables for each subnet for redundancy or is one enough?
  45. resource "aws_route_table" "kubernetes-public" {
  46. vpc_id = "${aws_vpc.cluster-vpc.id}"
  47. route {
  48. cidr_block = "0.0.0.0/0"
  49. gateway_id = "${aws_internet_gateway.cluster-vpc-internetgw.id}"
  50. }
  51. tags {
  52. Name = "kubernetes-${var.aws_cluster_name}-routetable-public"
  53. }
  54. }
  55. resource "aws_route_table" "kubernetes-private" {
  56. count = "${length(var.aws_cidr_subnets_private)}"
  57. vpc_id = "${aws_vpc.cluster-vpc.id}"
  58. route {
  59. cidr_block = "0.0.0.0/0"
  60. gateway_id = "${element(aws_nat_gateway.cluster-nat-gateway.*.id, count.index)}"
  61. }
  62. tags {
  63. Name = "kubernetes-${var.aws_cluster_name}-routetable-private-${count.index}"
  64. }
  65. }
  66. resource "aws_route_table_association" "kubernetes-public" {
  67. count = "${length(var.aws_cidr_subnets_public)}"
  68. subnet_id = "${element(aws_subnet.cluster-vpc-subnets-public.*.id,count.index)}"
  69. route_table_id = "${aws_route_table.kubernetes-public.id}"
  70. }
  71. resource "aws_route_table_association" "kubernetes-private" {
  72. count = "${length(var.aws_cidr_subnets_private)}"
  73. subnet_id = "${element(aws_subnet.cluster-vpc-subnets-private.*.id,count.index)}"
  74. route_table_id = "${element(aws_route_table.kubernetes-private.*.id,count.index)}"
  75. }
  76. #Kubernetes Security Groups
  77. resource "aws_security_group" "kubernetes" {
  78. name = "kubernetes-${var.aws_cluster_name}-securitygroup"
  79. vpc_id = "${aws_vpc.cluster-vpc.id}"
  80. tags {
  81. Name = "kubernetes-${var.aws_cluster_name}-securitygroup"
  82. }
  83. }
  84. resource "aws_security_group_rule" "allow-all-ingress" {
  85. type = "ingress"
  86. from_port = 0
  87. to_port = 65535
  88. protocol = "-1"
  89. cidr_blocks= ["${var.aws_vpc_cidr_block}"]
  90. security_group_id = "${aws_security_group.kubernetes.id}"
  91. }
  92. resource "aws_security_group_rule" "allow-all-egress" {
  93. type = "egress"
  94. from_port = 0
  95. to_port = 65535
  96. protocol = "-1"
  97. cidr_blocks = ["0.0.0.0/0"]
  98. security_group_id = "${aws_security_group.kubernetes.id}"
  99. }
  100. resource "aws_security_group_rule" "allow-ssh-connections" {
  101. type = "ingress"
  102. from_port = 22
  103. to_port = 22
  104. protocol = "TCP"
  105. cidr_blocks = ["0.0.0.0/0"]
  106. security_group_id = "${aws_security_group.kubernetes.id}"
  107. }