Guest User

Untitled

a guest
Dec 30th, 2023
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1.  
  2. module "vpc" {
  3. source = "terraform-aws-modules/vpc/aws"
  4. version = "~> 5.0"
  5. name = "ddemo-vpc"
  6. cidr = "10.0.0.0/16"
  7. azs = ["us-west-1b", "us-west-1c"]
  8. public_subnets = ["10.0.10.0/24", "10.0.20.0/24"]
  9. private_subnets = ["10.0.100.0/24", "10.0.200.0/24"]
  10.  
  11. manage_default_route_table = true
  12. enable_nat_gateway = true
  13. single_nat_gateway = true
  14. }
  15.  
  16. module "asg" {
  17. source = "terraform-aws-modules/autoscaling/aws"
  18. name = "ddemo-asg"
  19. instance_type = "t2.micro"
  20. image_id = "ami-014d05e6b24240371"
  21. min_size = "2"
  22. max_size = "2"
  23. desired_capacity = "2"
  24. min_elb_capacity = "2"
  25. vpc_zone_identifier = module.vpc.private_subnets
  26. # key_name = "foobar-aws2023-key"
  27. user_data = base64encode(local.user_data)
  28. security_groups = [module.alb.security_group_id]
  29. target_group_arns = [for k, v in module.alb.target_groups : v.arn]
  30. }
  31.  
  32. module "alb" {
  33. source = "terraform-aws-modules/alb/aws"
  34. vpc_id = module.vpc.vpc_id
  35. subnets = module.vpc.private_subnets
  36. enable_deletion_protection = false
  37. security_group_ingress_rules = {
  38. all_http = {
  39. from_port = 80
  40. to_port = 80
  41. ip_protocol = "tcp"
  42. description = "HTTP"
  43. cidr_ipv4 = "0.0.0.0/0"
  44. }
  45. }
  46. security_group_egress_rules = {
  47. all = {
  48. ip_protocol = "-1"
  49. cidr_ipv4 = "0.0.0.0/0"
  50. }
  51. }
  52. listeners = {
  53. ddemo-http = {
  54. port = 80
  55. protocol = "HTTP"
  56. forward = {
  57. target_group_key = "ddemo-tg"
  58. }
  59. }
  60. }
  61. target_groups = {
  62. ddemo-tg = {
  63. name_prefix = "ddemo-"
  64. protocol = "HTTP"
  65. port = 8080
  66. target_type = "instance"
  67. create_attachment = false
  68. }
  69. }
  70. }
  71.  
  72. locals {
  73. user_data = <<-EOT
  74. #!/bin/bash
  75. set -e
  76. /usr/bin/apt update
  77. /usr/bin/apt install -y nginx
  78. sed -i 's/listen 80 default_server;/listen 8080 default_server;/g' /etc/nginx/sites-available/default
  79. echo "hello how are you." > /var/www/html/index.html
  80. /usr/bin/systemctl restart nginx
  81. EOT
  82. }
Add Comment
Please, Sign In to add comment