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.

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