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.

183 lines
7.2 KiB

  1. terraform {
  2. required_version = ">= 0.12.0"
  3. }
  4. provider "aws" {
  5. access_key = var.AWS_ACCESS_KEY_ID
  6. secret_key = var.AWS_SECRET_ACCESS_KEY
  7. region = var.AWS_DEFAULT_REGION
  8. }
  9. data "aws_availability_zones" "available" {}
  10. /*
  11. * Calling modules who create the initial AWS VPC / AWS ELB
  12. * and AWS IAM Roles for Kubernetes Deployment
  13. */
  14. module "aws-vpc" {
  15. source = "./modules/vpc"
  16. aws_cluster_name = var.aws_cluster_name
  17. aws_vpc_cidr_block = var.aws_vpc_cidr_block
  18. aws_avail_zones = slice(data.aws_availability_zones.available.names, 0, length(var.aws_cidr_subnets_public) <= length(data.aws_availability_zones.available.names) ? length(var.aws_cidr_subnets_public) : length(data.aws_availability_zones.available.names))
  19. aws_cidr_subnets_private = var.aws_cidr_subnets_private
  20. aws_cidr_subnets_public = var.aws_cidr_subnets_public
  21. default_tags = var.default_tags
  22. }
  23. module "aws-elb" {
  24. source = "./modules/elb"
  25. aws_cluster_name = var.aws_cluster_name
  26. aws_vpc_id = module.aws-vpc.aws_vpc_id
  27. aws_avail_zones = slice(data.aws_availability_zones.available.names, 0, length(var.aws_cidr_subnets_public) <= length(data.aws_availability_zones.available.names) ? length(var.aws_cidr_subnets_public) : length(data.aws_availability_zones.available.names))
  28. aws_subnet_ids_public = module.aws-vpc.aws_subnet_ids_public
  29. aws_elb_api_port = var.aws_elb_api_port
  30. k8s_secure_api_port = var.k8s_secure_api_port
  31. default_tags = var.default_tags
  32. }
  33. module "aws-iam" {
  34. source = "./modules/iam"
  35. aws_cluster_name = var.aws_cluster_name
  36. }
  37. /*
  38. * Create Bastion Instances in AWS
  39. *
  40. */
  41. resource "aws_instance" "bastion-server" {
  42. ami = data.aws_ami.distro.id
  43. instance_type = var.aws_bastion_size
  44. count = var.aws_bastion_num
  45. associate_public_ip_address = true
  46. availability_zone = element(slice(data.aws_availability_zones.available.names, 0, length(var.aws_cidr_subnets_public) <= length(data.aws_availability_zones.available.names) ? length(var.aws_cidr_subnets_public) : length(data.aws_availability_zones.available.names)), count.index)
  47. subnet_id = element(module.aws-vpc.aws_subnet_ids_public, count.index)
  48. vpc_security_group_ids = module.aws-vpc.aws_security_group
  49. key_name = var.AWS_SSH_KEY_NAME
  50. tags = merge(var.default_tags, tomap({
  51. Name = "kubernetes-${var.aws_cluster_name}-bastion-${count.index}"
  52. Cluster = var.aws_cluster_name
  53. Role = "bastion-${var.aws_cluster_name}-${count.index}"
  54. }))
  55. }
  56. /*
  57. * Create K8s Master and worker nodes and etcd instances
  58. *
  59. */
  60. resource "aws_instance" "k8s-master" {
  61. ami = data.aws_ami.distro.id
  62. instance_type = var.aws_kube_master_size
  63. count = var.aws_kube_master_num
  64. availability_zone = element(slice(data.aws_availability_zones.available.names, 0, length(var.aws_cidr_subnets_public) <= length(data.aws_availability_zones.available.names) ? length(var.aws_cidr_subnets_public) : length(data.aws_availability_zones.available.names)), count.index)
  65. subnet_id = element(module.aws-vpc.aws_subnet_ids_private, count.index)
  66. vpc_security_group_ids = module.aws-vpc.aws_security_group
  67. root_block_device {
  68. volume_size = var.aws_kube_master_disk_size
  69. }
  70. iam_instance_profile = module.aws-iam.kube_control_plane-profile
  71. key_name = var.AWS_SSH_KEY_NAME
  72. tags = merge(var.default_tags, tomap({
  73. Name = "kubernetes-${var.aws_cluster_name}-master${count.index}"
  74. "kubernetes.io/cluster/${var.aws_cluster_name}" = "member"
  75. Role = "master"
  76. }))
  77. }
  78. resource "aws_elb_attachment" "attach_master_nodes" {
  79. count = var.aws_kube_master_num
  80. elb = module.aws-elb.aws_elb_api_id
  81. instance = element(aws_instance.k8s-master.*.id, count.index)
  82. }
  83. resource "aws_instance" "k8s-etcd" {
  84. ami = data.aws_ami.distro.id
  85. instance_type = var.aws_etcd_size
  86. count = var.aws_etcd_num
  87. availability_zone = element(slice(data.aws_availability_zones.available.names, 0, length(var.aws_cidr_subnets_public) <= length(data.aws_availability_zones.available.names) ? length(var.aws_cidr_subnets_public) : length(data.aws_availability_zones.available.names)), count.index)
  88. subnet_id = element(module.aws-vpc.aws_subnet_ids_private, count.index)
  89. vpc_security_group_ids = module.aws-vpc.aws_security_group
  90. root_block_device {
  91. volume_size = var.aws_etcd_disk_size
  92. }
  93. key_name = var.AWS_SSH_KEY_NAME
  94. tags = merge(var.default_tags, tomap({
  95. Name = "kubernetes-${var.aws_cluster_name}-etcd${count.index}"
  96. "kubernetes.io/cluster/${var.aws_cluster_name}" = "member"
  97. Role = "etcd"
  98. }))
  99. }
  100. resource "aws_instance" "k8s-worker" {
  101. ami = data.aws_ami.distro.id
  102. instance_type = var.aws_kube_worker_size
  103. count = var.aws_kube_worker_num
  104. availability_zone = element(slice(data.aws_availability_zones.available.names, 0, length(var.aws_cidr_subnets_public) <= length(data.aws_availability_zones.available.names) ? length(var.aws_cidr_subnets_public) : length(data.aws_availability_zones.available.names)), count.index)
  105. subnet_id = element(module.aws-vpc.aws_subnet_ids_private, count.index)
  106. vpc_security_group_ids = module.aws-vpc.aws_security_group
  107. root_block_device {
  108. volume_size = var.aws_kube_worker_disk_size
  109. }
  110. iam_instance_profile = module.aws-iam.kube-worker-profile
  111. key_name = var.AWS_SSH_KEY_NAME
  112. tags = merge(var.default_tags, tomap({
  113. Name = "kubernetes-${var.aws_cluster_name}-worker${count.index}"
  114. "kubernetes.io/cluster/${var.aws_cluster_name}" = "member"
  115. Role = "worker"
  116. }))
  117. }
  118. /*
  119. * Create Kubespray Inventory File
  120. *
  121. */
  122. data "template_file" "inventory" {
  123. template = file("${path.module}/templates/inventory.tpl")
  124. vars = {
  125. public_ip_address_bastion = join("\n", formatlist("bastion ansible_host=%s", aws_instance.bastion-server.*.public_ip))
  126. connection_strings_master = join("\n", formatlist("%s ansible_host=%s", aws_instance.k8s-master.*.private_dns, aws_instance.k8s-master.*.private_ip))
  127. connection_strings_node = join("\n", formatlist("%s ansible_host=%s", aws_instance.k8s-worker.*.private_dns, aws_instance.k8s-worker.*.private_ip))
  128. list_master = join("\n", aws_instance.k8s-master.*.private_dns)
  129. list_node = join("\n", aws_instance.k8s-worker.*.private_dns)
  130. connection_strings_etcd = join("\n", formatlist("%s ansible_host=%s", aws_instance.k8s-etcd.*.private_dns, aws_instance.k8s-etcd.*.private_ip))
  131. list_etcd = join("\n", ((var.aws_etcd_num > 0) ? (aws_instance.k8s-etcd.*.private_dns) : (aws_instance.k8s-master.*.private_dns)))
  132. elb_api_fqdn = "apiserver_loadbalancer_domain_name=\"${module.aws-elb.aws_elb_api_fqdn}\""
  133. }
  134. }
  135. resource "null_resource" "inventories" {
  136. provisioner "local-exec" {
  137. command = "echo '${data.template_file.inventory.rendered}' > ${var.inventory_file}"
  138. }
  139. triggers = {
  140. template = data.template_file.inventory.rendered
  141. }
  142. }