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.

179 lines
5.7 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 = 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-nlb" {
  24. source = "./modules/nlb"
  25. aws_cluster_name = var.aws_cluster_name
  26. aws_vpc_id = module.aws-vpc.aws_vpc_id
  27. aws_avail_zones = data.aws_availability_zones.available.names
  28. aws_subnet_ids_public = module.aws-vpc.aws_subnet_ids_public
  29. aws_nlb_api_port = var.aws_nlb_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. subnet_id = element(module.aws-vpc.aws_subnet_ids_public, count.index)
  47. vpc_security_group_ids = module.aws-vpc.aws_security_group
  48. key_name = var.AWS_SSH_KEY_NAME
  49. tags = merge(var.default_tags, tomap({
  50. Name = "kubernetes-${var.aws_cluster_name}-bastion-${count.index}"
  51. Cluster = var.aws_cluster_name
  52. Role = "bastion-${var.aws_cluster_name}-${count.index}"
  53. }))
  54. }
  55. /*
  56. * Create K8s Master and worker nodes and etcd instances
  57. *
  58. */
  59. resource "aws_instance" "k8s-master" {
  60. ami = data.aws_ami.distro.id
  61. instance_type = var.aws_kube_master_size
  62. count = var.aws_kube_master_num
  63. subnet_id = element(module.aws-vpc.aws_subnet_ids_private, count.index)
  64. vpc_security_group_ids = module.aws-vpc.aws_security_group
  65. root_block_device {
  66. volume_size = var.aws_kube_master_disk_size
  67. }
  68. iam_instance_profile = module.aws-iam.kube_control_plane-profile
  69. key_name = var.AWS_SSH_KEY_NAME
  70. tags = merge(var.default_tags, tomap({
  71. Name = "kubernetes-${var.aws_cluster_name}-master${count.index}"
  72. "kubernetes.io/cluster/${var.aws_cluster_name}" = "member"
  73. Role = "master"
  74. }))
  75. }
  76. resource "aws_lb_target_group_attachment" "tg-attach_master_nodes" {
  77. count = var.aws_kube_master_num
  78. target_group_arn = module.aws-nlb.aws_nlb_api_tg_arn
  79. target_id = element(aws_instance.k8s-master.*.private_ip, count.index)
  80. }
  81. resource "aws_instance" "k8s-etcd" {
  82. ami = data.aws_ami.distro.id
  83. instance_type = var.aws_etcd_size
  84. count = var.aws_etcd_num
  85. subnet_id = element(module.aws-vpc.aws_subnet_ids_private, count.index)
  86. vpc_security_group_ids = module.aws-vpc.aws_security_group
  87. root_block_device {
  88. volume_size = var.aws_etcd_disk_size
  89. }
  90. key_name = var.AWS_SSH_KEY_NAME
  91. tags = merge(var.default_tags, tomap({
  92. Name = "kubernetes-${var.aws_cluster_name}-etcd${count.index}"
  93. "kubernetes.io/cluster/${var.aws_cluster_name}" = "member"
  94. Role = "etcd"
  95. }))
  96. }
  97. resource "aws_instance" "k8s-worker" {
  98. ami = data.aws_ami.distro.id
  99. instance_type = var.aws_kube_worker_size
  100. count = var.aws_kube_worker_num
  101. subnet_id = element(module.aws-vpc.aws_subnet_ids_private, count.index)
  102. vpc_security_group_ids = module.aws-vpc.aws_security_group
  103. root_block_device {
  104. volume_size = var.aws_kube_worker_disk_size
  105. }
  106. iam_instance_profile = module.aws-iam.kube-worker-profile
  107. key_name = var.AWS_SSH_KEY_NAME
  108. tags = merge(var.default_tags, tomap({
  109. Name = "kubernetes-${var.aws_cluster_name}-worker${count.index}"
  110. "kubernetes.io/cluster/${var.aws_cluster_name}" = "member"
  111. Role = "worker"
  112. }))
  113. }
  114. /*
  115. * Create Kubespray Inventory File
  116. *
  117. */
  118. data "template_file" "inventory" {
  119. template = file("${path.module}/templates/inventory.tpl")
  120. vars = {
  121. public_ip_address_bastion = join("\n", formatlist("bastion ansible_host=%s", aws_instance.bastion-server.*.public_ip))
  122. connection_strings_master = join("\n", formatlist("%s ansible_host=%s", aws_instance.k8s-master.*.private_dns, aws_instance.k8s-master.*.private_ip))
  123. connection_strings_node = join("\n", formatlist("%s ansible_host=%s", aws_instance.k8s-worker.*.private_dns, aws_instance.k8s-worker.*.private_ip))
  124. list_master = join("\n", aws_instance.k8s-master.*.private_dns)
  125. list_node = join("\n", aws_instance.k8s-worker.*.private_dns)
  126. connection_strings_etcd = join("\n", formatlist("%s ansible_host=%s", aws_instance.k8s-etcd.*.private_dns, aws_instance.k8s-etcd.*.private_ip))
  127. list_etcd = join("\n", ((var.aws_etcd_num > 0) ? (aws_instance.k8s-etcd.*.private_dns) : (aws_instance.k8s-master.*.private_dns)))
  128. nlb_api_fqdn = "apiserver_loadbalancer_domain_name=\"${module.aws-nlb.aws_nlb_api_fqdn}\""
  129. }
  130. }
  131. resource "null_resource" "inventories" {
  132. provisioner "local-exec" {
  133. command = "echo '${data.template_file.inventory.rendered}' > ${var.inventory_file}"
  134. }
  135. triggers = {
  136. template = data.template_file.inventory.rendered
  137. }
  138. }