Преглед изворни кода

infra: add configuration for VPC and EC2 modules

- Call VPC and EC2 module in terraform.tf file
- Output of modules in output.tf file
- Add variables.tf to substitute actual values present in terraform.tfvars file
- Add provider.tf for aws provider config
Aashish Goyal пре 1 година
родитељ
комит
7c4a306047
5 измењених фајлова са 279 додато и 1 уклоњено
  1. 33 0
      outputs.tf
  2. 5 0
      provider.tf
  3. 34 1
      terraform.tf
  4. 126 0
      terraform.tfvars
  5. 81 0
      variables.tf

+ 33 - 0
outputs.tf

@@ -0,0 +1,33 @@
+## Outputs
+
+output "vpc_id" {
+  value = module.vpc.vpc_id
+}
+
+output "public_subnet_id" {
+  value = module.vpc.public_subnet_id
+}
+
+output "private_subnet_id" {
+  value = module.vpc.private_subnet_id
+}
+
+output "alb_security_group_id" {
+  value = module.ec2.alb_security_group_id
+}
+
+output "ec2_security_group_id" {
+  value = module.ec2.ec2_security_group_id
+}
+
+output "alb_arn" {
+  value = module.ec2.alb_arn
+}
+
+output "alb_dns_name" {
+  value = module.ec2.alb_dns_name
+}
+
+output "autoscaling_group_arn" {
+  value = module.ec2.autoscaling_group_arn
+}

+ 5 - 0
provider.tf

@@ -0,0 +1,5 @@
+## AWS Provider Configuration
+
+provider "aws" {
+  region = var.region
+}

+ 34 - 1
terraform.tf

@@ -1 +1,34 @@
-provider "aws" {}
+## VPC Module
+
+module "vpc" {
+  source = "./modules/vpc"
+
+  cidr_block          = var.cidr_block
+  public_subnet_az    = var.public_subnet_az
+  public_subnet_cidr  = var.public_subnet_cidr
+  private_subnet_az   = var.private_subnet_az
+  private_subnet_cidr = var.private_subnet_cidr
+
+  tags = var.tags
+}
+
+## EC2 Module
+
+module "ec2" {
+  source = "./modules/ec2"
+
+  vpc_id                = module.vpc.vpc_id
+  ingress_alb_sg_rule   = var.ingress_alb_sg_rule
+  egress_alb_sg_rule    = var.egress_alb_sg_rule
+  ingress_ec2_sg_rule   = var.ingress_ec2_sg_rule
+  egress_ec2_sg_rule    = var.egress_ec2_sg_rule
+  ec2_launch_template   = var.ec2_launch_template
+  ec2_alb_target_group  = var.ec2_alb_target_group
+  alb_subnet_ids        = module.vpc.public_subnet_id
+  ec2_subnet_id         = module.vpc.private_subnet_id
+  alb_ec2               = var.alb_ec2
+  alb_listener          = var.alb_listener
+  ec2_autoscaling_group = var.ec2_autoscaling_group
+
+  tags = var.tags
+}

+ 126 - 0
terraform.tfvars

@@ -0,0 +1,126 @@
+region = "us-east-1"
+
+cidr_block = "10.0.0.0/16"
+
+public_subnet_az   = ["us-east-1a", "us-east-1b"]
+public_subnet_cidr = ["10.0.0.0/20", "10.0.16.0/20"]
+
+private_subnet_az   = "us-east-1b"
+private_subnet_cidr = "10.0.32.0/20"
+
+tags = {
+  "Infra" = "LiveLike"
+}
+
+ingress_alb_sg_rule = {
+  inbound_80 = {
+    from_port   = 80
+    to_port     = 80
+    protocol    = "TCP"
+    cidr_blocks = ["0.0.0.0/0"]
+  },
+
+  inbound_443 = {
+    from_port   = 443
+    to_port     = 443
+    protocol    = "TCP"
+    cidr_blocks = ["0.0.0.0/0"]
+  }
+}
+
+egress_alb_sg_rule = {
+  ec2_egress = {
+    from_port = 0
+    to_port   = 0
+    protocol  = "-1"
+  }
+}
+
+ingress_ec2_sg_rule = {
+  alb_ingress = {
+    from_port = 0
+    to_port   = 0
+    protocol  = "-1"
+  },
+  allow_ssh_from_vpc = {
+    from_port   = 22
+    to_port     = 22
+    protocol    = "TCP"
+    cidr_blocks = ["10.0.0.0/16"]
+  }
+}
+
+egress_ec2_sg_rule = {
+  egress_80 = {
+    from_port   = 80
+    to_port     = 80
+    protocol    = "TCP"
+    cidr_blocks = ["0.0.0.0/0"]
+  },
+  egress_443 = {
+    from_port   = 443
+    to_port     = 443
+    protocol    = "TCP"
+    cidr_blocks = ["0.0.0.0/0"]
+  }
+}
+
+ec2_alb_target_group = {
+  name                 = "ec2-alb-target-group"
+  port                 = 80
+  protocol             = "HTTP"
+  deregistration_delay = "60"
+}
+
+alb_listener = {
+  # redirect_80 = {
+  #   port        = "80"
+  #   protocol    = "HTTP"
+  #   action_type = "redirect"
+
+  #   redirect = {
+  #     status_code = "HTTP_301"
+  #     port        = "443"
+  #     protocol    = "HTTPS"
+  #   }
+  # },
+  # listener_443 = {
+  #   port            = "443"
+  #   protocol        = "HTTPS"
+  #   ssl_policy      = "ELBSecurityPolicy-TLS13-1-2-2021-06"
+  #   certificate_arn = ""
+  #   action_type     = "forward"
+  # }
+
+  listener_80 = {
+    port        = "80"
+    protocol    = "HTTP"
+    action_type = "forward"
+  }
+}
+
+alb_ec2 = {
+  name                       = "alb-for-ec2"
+  enable_deletion_protection = false
+  internal                   = false
+  load_balancer_type         = "application"
+}
+
+ec2_launch_template = {
+  name            = "ec2-launch-template"
+  device_name     = "/dev/sda1"
+  ebs_volume_size = 20
+  ebs_volume_type = "gp3"
+  instance_type   = "t3.micro"
+  ebs_optimized   = true
+  key_name        = "livelike"
+}
+
+ec2_autoscaling_group = {
+  name                      = "ec2-autoscaling-group"
+  min_size                  = 1
+  max_size                  = 2
+  desired_capacity          = 1
+  default_cooldown          = 60
+  health_check_grace_period = 120
+}

+ 81 - 0
variables.tf

@@ -0,0 +1,81 @@
+## Variables
+
+variable "region" {
+  type    = string
+  default = ""
+}
+
+variable "tags" {
+  type    = map(string)
+  default = {}
+}
+
+variable "cidr_block" {
+  type    = string
+  default = ""
+}
+
+variable "public_subnet_az" {
+  type    = list(string)
+  default = [""]
+}
+
+variable "public_subnet_cidr" {
+  type    = list(string)
+  default = [""]
+}
+
+variable "private_subnet_az" {
+  type    = string
+  default = ""
+}
+
+variable "private_subnet_cidr" {
+  type    = string
+  default = ""
+}
+
+variable "ingress_alb_sg_rule" {
+  type    = any
+  default = {}
+}
+
+variable "egress_alb_sg_rule" {
+  type    = any
+  default = {}
+}
+
+variable "ingress_ec2_sg_rule" {
+  type    = any
+  default = {}
+}
+
+variable "egress_ec2_sg_rule" {
+  type    = any
+  default = {}
+}
+
+variable "ec2_launch_template" {
+  type    = map(string)
+  default = {}
+}
+
+variable "ec2_alb_target_group" {
+  type    = map(string)
+  default = {}
+}
+
+variable "alb_ec2" {
+  type    = map(string)
+  default = {}
+}
+
+variable "alb_listener" {
+  type    = any
+  default = {}
+}
+
+variable "ec2_autoscaling_group" {
+  type    = map(string)
+  default = {}
+}