mirror of
				https://github.com/mr-vercetti/tf-aws-demo.git
				synced 2025-11-03 17:15:45 +01:00 
			
		
		
		
	Add autoscaling policy and create ami_data module
This commit is contained in:
		
							
								
								
									
										20
									
								
								modules/ami_data/main.tf
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										20
									
								
								modules/ami_data/main.tf
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,20 @@
 | 
			
		||||
data "aws_ami" "amazon_linux" {
 | 
			
		||||
  most_recent = true
 | 
			
		||||
 | 
			
		||||
  filter {
 | 
			
		||||
    name   = "name"
 | 
			
		||||
    values = ["amzn2-ami-*-gp2"]
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  filter {
 | 
			
		||||
    name   = "architecture"
 | 
			
		||||
    values = ["x86_64"]
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  filter {
 | 
			
		||||
    name   = "virtualization-type"
 | 
			
		||||
    values = ["hvm"]
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  owners = ["amazon"]
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										3
									
								
								modules/ami_data/outputs.tf
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										3
									
								
								modules/ami_data/outputs.tf
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,3 @@
 | 
			
		||||
output "amazon_linux" {
 | 
			
		||||
  value = data.aws_ami.amazon_linux
 | 
			
		||||
}
 | 
			
		||||
@@ -50,7 +50,7 @@ data "template_file" "bootstrap" {
 | 
			
		||||
resource "aws_launch_configuration" "this" {
 | 
			
		||||
  name = "demo-app-launch-configuration"
 | 
			
		||||
 | 
			
		||||
  image_id      = var.EC2_IMAGE_ID
 | 
			
		||||
  image_id      = var.EC2_AMI
 | 
			
		||||
  instance_type = var.EC2_TYPE
 | 
			
		||||
 | 
			
		||||
  key_name        = var.EC2_KEY_NAME
 | 
			
		||||
@@ -80,10 +80,62 @@ resource "aws_autoscaling_group" "this" {
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
resource "aws_autoscaling_policy" "this" {
 | 
			
		||||
  name                   = "demo-app-autoscaling-policy"
 | 
			
		||||
  scaling_adjustment     = 1
 | 
			
		||||
  adjustment_type        = "ChangeInCapacity"
 | 
			
		||||
  cooldown               = 300
 | 
			
		||||
# asg scale up policy
 | 
			
		||||
resource "aws_autoscaling_policy" "cpu-policy" {
 | 
			
		||||
  name = "demo-app-cpu-policy"
 | 
			
		||||
 | 
			
		||||
  autoscaling_group_name = aws_autoscaling_group.this.name
 | 
			
		||||
  adjustment_type        = "ChangeInCapacity"
 | 
			
		||||
  scaling_adjustment     = "1"
 | 
			
		||||
  cooldown               = "300"
 | 
			
		||||
  policy_type            = "SimpleScaling"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
resource "aws_cloudwatch_metric_alarm" "cpu-alarm" {
 | 
			
		||||
  alarm_name          = "cpu-alarm"
 | 
			
		||||
  alarm_description   = "cpu-alarm"
 | 
			
		||||
  comparison_operator = "GreaterThanOrEqualToThreshold"
 | 
			
		||||
  evaluation_periods  = "2"
 | 
			
		||||
  metric_name         = "CPUUtilization"
 | 
			
		||||
  namespace           = "AWS/EC2"
 | 
			
		||||
  period              = "120"
 | 
			
		||||
  statistic           = "Average"
 | 
			
		||||
  threshold           = "30"
 | 
			
		||||
 | 
			
		||||
  dimensions = {
 | 
			
		||||
    "AutoScalingGroupName" = "${aws_autoscaling_group.this.name}"
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  actions_enabled = true
 | 
			
		||||
  alarm_actions   = ["${aws_autoscaling_policy.cpu-policy.arn}"]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# asg scale down policy
 | 
			
		||||
resource "aws_autoscaling_policy" "cpu-policy-scaledown" {
 | 
			
		||||
  name = "demo-app-cpu-policy-scaledown"
 | 
			
		||||
 | 
			
		||||
  autoscaling_group_name = aws_autoscaling_group.this.name
 | 
			
		||||
  adjustment_type        = "ChangeInCapacity"
 | 
			
		||||
  scaling_adjustment     = "-1"
 | 
			
		||||
  cooldown               = "300"
 | 
			
		||||
  policy_type            = "SimpleScaling"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
resource "aws_cloudwatch_metric_alarm" "cpu-alarm-scaledown" {
 | 
			
		||||
  alarm_name          = "cpu-alarm-scaledown"
 | 
			
		||||
  alarm_description   = "cpu-alarm-scaledown"
 | 
			
		||||
  comparison_operator = "LessThanOrEqualToThreshold"
 | 
			
		||||
  evaluation_periods  = "2"
 | 
			
		||||
  metric_name         = "CPUUtilization"
 | 
			
		||||
  namespace           = "AWS/EC2"
 | 
			
		||||
  period              = "120"
 | 
			
		||||
  statistic           = "Average"
 | 
			
		||||
  threshold           = "5"
 | 
			
		||||
 | 
			
		||||
  dimensions = {
 | 
			
		||||
    "AutoScalingGroupName" = "${aws_autoscaling_group.this.name}"
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  actions_enabled = true
 | 
			
		||||
  alarm_actions   = ["${aws_autoscaling_policy.cpu-policy-scaledown.arn}"]
 | 
			
		||||
}
 | 
			
		||||
@@ -8,7 +8,7 @@ variable "VPC_SUBNETS_IDS" {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# EC2
 | 
			
		||||
variable "EC2_IMAGE_ID" {
 | 
			
		||||
variable "EC2_AMI" {
 | 
			
		||||
  type = string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,24 +1,3 @@
 | 
			
		||||
data "aws_ami" "amazon-linux" {
 | 
			
		||||
  most_recent = true
 | 
			
		||||
 | 
			
		||||
  filter {
 | 
			
		||||
    name   = "name"
 | 
			
		||||
    values = ["amzn2-ami-*-gp2"]
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  filter {
 | 
			
		||||
    name   = "architecture"
 | 
			
		||||
    values = ["x86_64"]
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  filter {
 | 
			
		||||
    name   = "virtualization-type"
 | 
			
		||||
    values = ["hvm"]
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  owners = ["amazon"]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
resource "aws_security_group" "this" {
 | 
			
		||||
  name = "bastion-host-sg"
 | 
			
		||||
 | 
			
		||||
@@ -50,9 +29,9 @@ resource "aws_network_interface" "this" {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
resource "aws_instance" "this" {
 | 
			
		||||
  ami           = data.aws_ami.amazon-linux.id
 | 
			
		||||
  instance_type = var.INSTANCE_TYPE
 | 
			
		||||
  key_name = var.KEY_NAME
 | 
			
		||||
  ami           = var.EC2_AMI
 | 
			
		||||
  instance_type = var.EC2_TYPE
 | 
			
		||||
  key_name = var.EC2_KEY_NAME
 | 
			
		||||
 | 
			
		||||
  network_interface {
 | 
			
		||||
    network_interface_id = aws_network_interface.this.id
 | 
			
		||||
@@ -60,6 +39,6 @@ resource "aws_instance" "this" {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  tags = {
 | 
			
		||||
    Name = var.INSTANCE_NAME
 | 
			
		||||
    Name = var.EC2_INSTANCE_NAME
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -6,15 +6,19 @@ variable "SUBNET_ID" {
 | 
			
		||||
  type = string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
variable "INSTANCE_TYPE" {
 | 
			
		||||
variable "EC2_AMI" {
 | 
			
		||||
  type = string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
variable "EC2_TYPE" {
 | 
			
		||||
  type = string
 | 
			
		||||
  default = "t2.micro"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
variable "INSTANCE_NAME" {
 | 
			
		||||
variable "EC2_INSTANCE_NAME" {
 | 
			
		||||
  type = string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
variable "KEY_NAME" {
 | 
			
		||||
variable "EC2_KEY_NAME" {
 | 
			
		||||
  type = string
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										37
									
								
								prod/main.tf
									
									
									
									
									
								
							
							
						
						
									
										37
									
								
								prod/main.tf
									
									
									
									
									
								
							@@ -27,13 +27,18 @@ module "vpc" {
 | 
			
		||||
 | 
			
		||||
# Get VPC data
 | 
			
		||||
module "vpc_data" {
 | 
			
		||||
  depends_on = [module.vpc]
 | 
			
		||||
  source     = "../modules/vpc_data"
 | 
			
		||||
  depends_on = [module.vpc]
 | 
			
		||||
 | 
			
		||||
  VPC_NAME        = var.DEMO_VPC_NAME
 | 
			
		||||
  BASTION_HOST_AZ = var.DEMO_BASTION_HOST_AZ
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Get AMI data
 | 
			
		||||
module "ami_data" {
 | 
			
		||||
  source = "../modules/ami_data"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Create bastion host
 | 
			
		||||
module "bastion_host" {
 | 
			
		||||
  source = "../modules/bastion_host"
 | 
			
		||||
@@ -41,30 +46,10 @@ module "bastion_host" {
 | 
			
		||||
  VPC_ID    = module.vpc_data.vpc.id
 | 
			
		||||
  SUBNET_ID = module.vpc_data.bastion_host_subnet.id
 | 
			
		||||
 | 
			
		||||
  INSTANCE_TYPE = var.DEMO_BASTION_HOST_TYPE
 | 
			
		||||
  INSTANCE_NAME = var.DEMO_BASTION_HOST_NAME
 | 
			
		||||
  KEY_NAME      = var.DEMO_BASTION_HOST_KEY_NAME
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
data "aws_ami" "amazon-linux" {
 | 
			
		||||
  most_recent = true
 | 
			
		||||
 | 
			
		||||
  filter {
 | 
			
		||||
    name   = "name"
 | 
			
		||||
    values = ["amzn2-ami-*-gp2"]
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  filter {
 | 
			
		||||
    name   = "architecture"
 | 
			
		||||
    values = ["x86_64"]
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  filter {
 | 
			
		||||
    name   = "virtualization-type"
 | 
			
		||||
    values = ["hvm"]
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  owners = ["amazon"]
 | 
			
		||||
  EC2_AMI           = module.ami_data.amazon_linux.id
 | 
			
		||||
  EC2_TYPE          = var.DEMO_BASTION_HOST_TYPE
 | 
			
		||||
  EC2_INSTANCE_NAME = var.DEMO_BASTION_HOST_NAME
 | 
			
		||||
  EC2_KEY_NAME      = var.DEMO_BASTION_HOST_KEY_NAME
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Create demo app
 | 
			
		||||
@@ -74,7 +59,7 @@ module "app" {
 | 
			
		||||
  VPC_ID          = module.vpc_data.vpc.id
 | 
			
		||||
  VPC_SUBNETS_IDS = module.vpc_data.private_subnets.ids
 | 
			
		||||
 | 
			
		||||
  EC2_IMAGE_ID = data.aws_ami.amazon-linux.id
 | 
			
		||||
  EC2_AMI      = module.ami_data.amazon_linux.id
 | 
			
		||||
  EC2_TYPE     = var.DEMO_APP_EC2_TYPE
 | 
			
		||||
  EC2_KEY_NAME = var.DEMO_APP_EC2_KEY_NAME
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user