main.tf 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. provider "aws" {
  2. region = var.region
  3. profile = var.profile
  4. }
  5. module "vpc" {
  6. source = "git::http://gogs.mague.com/chris/tfmodule-aws-2tier-vpc.git"
  7. region = var.region
  8. profile = var.profile
  9. vpc-name = var.vpc-name
  10. vpc-cidr = var.vpc-cidr
  11. vpc-azs = var.vpc-azs
  12. enable-private = false
  13. common-tags = {
  14. "Owner" = "maguec"
  15. }
  16. }
  17. module "nodes" {
  18. source = "github.com/Redislabs-Solution-Architects/tfmodule-aws-redis-enterprise"
  19. region = var.region
  20. profile = var.profile
  21. open-nets = ["76.14.80.208/32"]
  22. data-node-count = 3
  23. enable-volumes = false
  24. re-instance-type = "c5a.4xlarge"
  25. vpc-cidr = var.vpc-cidr
  26. vpc-azs = var.vpc-azs
  27. vpc-name = var.vpc-name
  28. vpc-id = module.vpc.vpc-id
  29. vpc-subnets = module.vpc.subnets-public
  30. allow-public-ssh = 1
  31. enable-flash = false
  32. common-tags = {
  33. "Owner" = "maguec"
  34. "Project" = "AWS Testing Environment"
  35. }
  36. }
  37. module "tmux" {
  38. source = "github.com/shokunin/tfmodule-cloud-tmux"
  39. session-name = "cpredis"
  40. ssh-key = "${var.vpc-name}.pem"
  41. user = "ubuntu"
  42. nodes = concat(module.nodes.node-ips, [aws_eip.re-eip.public_ip])
  43. outpath = "${path.module}"
  44. }
  45. # Start CP Redis Testernode
  46. resource "aws_instance" "re" {
  47. ami = data.aws_ami.re-ami.id
  48. instance_type = "m5a.xlarge"
  49. availability_zone = element(var.vpc-azs, 1)
  50. subnet_id = element(module.vpc.subnets-public, 1)
  51. vpc_security_group_ids = [module.nodes.re-security-group]
  52. source_dest_check = false
  53. key_name = var.vpc-name
  54. tags = merge({ Name = "Tester-${var.vpc-name}-1" }, var.common-tags)
  55. }
  56. resource "aws_eip" "re-eip" {
  57. vpc = true
  58. tags = merge({ Name = "${var.vpc-name}-node-eip-1" }, var.common-tags)
  59. }
  60. resource "aws_eip_association" "re-eip-assoc" {
  61. instance_id = element(aws_instance.re.*.id, 1)
  62. allocation_id = element(aws_eip.re-eip.*.id, 1)
  63. depends_on = [aws_instance.re, aws_eip.re-eip]
  64. }
  65. resource "null_resource" "remote-config" {
  66. provisioner "remote-exec" {
  67. connection {
  68. user = "ubuntu"
  69. host = aws_eip.re-eip.public_ip
  70. private_key = file("~/.ssh/${var.vpc-name}.pem")
  71. agent = true
  72. }
  73. #inline = ["sudo apt update > /dev/null && sudo apt install -y python-pip > /dev/null"]
  74. inline = ["sudo apt update > /dev/null"]
  75. }
  76. depends_on = [aws_instance.re, aws_eip_association.re-eip-assoc]
  77. }
  78. data "template_file" "raft_group_setup" {
  79. template = file("${path.module}/raft_group_setup.tpl")
  80. vars = {
  81. node1 = module.nodes.node-internal-ips[0]
  82. node2 = module.nodes.node-internal-ips[1]
  83. node3 = module.nodes.node-internal-ips[2]
  84. }
  85. }
  86. resource "null_resource" "raft_group_setup" {
  87. provisioner "local-exec" {
  88. command = "echo \"${data.template_file.raft_group_setup.rendered}\" > ${path.module}/ansible/raft_group_setup"
  89. }
  90. depends_on = [data.template_file.raft_group_setup]
  91. }
  92. resource "local_file" "inventory" {
  93. content = templatefile("${path.module}/inventories/inventory.ctmpl",
  94. {
  95. nodes = module.nodes.node-ips
  96. tester = aws_eip.re-eip.public_ip
  97. path = abspath(path.module)
  98. }
  99. )
  100. filename = "${path.module}/inventories/inventory.ini"
  101. file_permission = "0644"
  102. }
  103. resource "local_file" "ssh_config" {
  104. content = templatefile("${path.module}/inventories/ssh.ctmpl",
  105. {
  106. ssh-key = "${var.vpc-name}.pem"
  107. ssh-user = "ubuntu"
  108. }
  109. )
  110. filename = "${path.module}/inventories/ssh.cfg"
  111. file_permission = "0644"
  112. }
  113. output "tmux" {
  114. value = "run ./cloud-tmux to enable tmux connections"
  115. }
  116. output "ansible1" {
  117. value = "Setup Tester Node\n\t\tansible-playbook -i inventories/inventory.ini ansible/tester.yml"
  118. }
  119. output "ansible2" {
  120. value = "Setup CP Redis Nodes\n\t\tansible-playbook -i inventories/inventory.ini ansible/nodes.yml -e 'NODE_IPS=${join(",", module.nodes.node-internal-ips)}'"
  121. }
  122. output "cluster-setup" {
  123. value = "To set up the cluster run /usr/local/bin/setup-raft-cluster from any one of the nodes"
  124. }
  125. output "insight" {
  126. value = "http://${aws_eip.re-eip.public_ip}:8001"
  127. }
  128. output "cpredisnodes" {
  129. value = module.nodes.node-internal-ips
  130. }