mirror of
https://github.com/mr-vercetti/tf-aws-demo.git
synced 2025-01-18 11:25:35 +01:00
Add autoscaling policy and create ami_data module
This commit is contained in:
parent
9cef061162
commit
92afe542a3
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,12 +50,12 @@ data "template_file" "bootstrap" {
|
|||||||
resource "aws_launch_configuration" "this" {
|
resource "aws_launch_configuration" "this" {
|
||||||
name = "demo-app-launch-configuration"
|
name = "demo-app-launch-configuration"
|
||||||
|
|
||||||
image_id = var.EC2_IMAGE_ID
|
image_id = var.EC2_AMI
|
||||||
instance_type = var.EC2_TYPE
|
instance_type = var.EC2_TYPE
|
||||||
|
|
||||||
key_name = var.EC2_KEY_NAME
|
key_name = var.EC2_KEY_NAME
|
||||||
security_groups = [aws_security_group.this.id]
|
security_groups = [aws_security_group.this.id]
|
||||||
user_data = data.template_file.bootstrap.rendered
|
user_data = data.template_file.bootstrap.rendered
|
||||||
|
|
||||||
lifecycle {
|
lifecycle {
|
||||||
create_before_destroy = true
|
create_before_destroy = true
|
||||||
@ -65,8 +65,8 @@ resource "aws_launch_configuration" "this" {
|
|||||||
resource "aws_autoscaling_group" "this" {
|
resource "aws_autoscaling_group" "this" {
|
||||||
name = "demo-app-autoscaling-group"
|
name = "demo-app-autoscaling-group"
|
||||||
|
|
||||||
min_size = var.ASG_MIN_SIZE
|
min_size = var.ASG_MIN_SIZE
|
||||||
max_size = var.ASG_MAX_SIZE
|
max_size = var.ASG_MAX_SIZE
|
||||||
|
|
||||||
launch_configuration = aws_launch_configuration.this.name
|
launch_configuration = aws_launch_configuration.this.name
|
||||||
vpc_zone_identifier = var.VPC_SUBNETS_IDS
|
vpc_zone_identifier = var.VPC_SUBNETS_IDS
|
||||||
@ -80,10 +80,62 @@ resource "aws_autoscaling_group" "this" {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
resource "aws_autoscaling_policy" "this" {
|
# asg scale up policy
|
||||||
name = "demo-app-autoscaling-policy"
|
resource "aws_autoscaling_policy" "cpu-policy" {
|
||||||
scaling_adjustment = 1
|
name = "demo-app-cpu-policy"
|
||||||
adjustment_type = "ChangeInCapacity"
|
|
||||||
cooldown = 300
|
|
||||||
autoscaling_group_name = aws_autoscaling_group.this.name
|
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
|
# EC2
|
||||||
variable "EC2_IMAGE_ID" {
|
variable "EC2_AMI" {
|
||||||
type = string
|
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" {
|
resource "aws_security_group" "this" {
|
||||||
name = "bastion-host-sg"
|
name = "bastion-host-sg"
|
||||||
|
|
||||||
@ -50,9 +29,9 @@ resource "aws_network_interface" "this" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
resource "aws_instance" "this" {
|
resource "aws_instance" "this" {
|
||||||
ami = data.aws_ami.amazon-linux.id
|
ami = var.EC2_AMI
|
||||||
instance_type = var.INSTANCE_TYPE
|
instance_type = var.EC2_TYPE
|
||||||
key_name = var.KEY_NAME
|
key_name = var.EC2_KEY_NAME
|
||||||
|
|
||||||
network_interface {
|
network_interface {
|
||||||
network_interface_id = aws_network_interface.this.id
|
network_interface_id = aws_network_interface.this.id
|
||||||
@ -60,6 +39,6 @@ resource "aws_instance" "this" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tags = {
|
tags = {
|
||||||
Name = var.INSTANCE_NAME
|
Name = var.EC2_INSTANCE_NAME
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -6,15 +6,19 @@ variable "SUBNET_ID" {
|
|||||||
type = string
|
type = string
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "INSTANCE_TYPE" {
|
variable "EC2_AMI" {
|
||||||
|
type = string
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "EC2_TYPE" {
|
||||||
type = string
|
type = string
|
||||||
default = "t2.micro"
|
default = "t2.micro"
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "INSTANCE_NAME" {
|
variable "EC2_INSTANCE_NAME" {
|
||||||
type = string
|
type = string
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "KEY_NAME" {
|
variable "EC2_KEY_NAME" {
|
||||||
type = string
|
type = string
|
||||||
}
|
}
|
41
prod/main.tf
41
prod/main.tf
@ -27,13 +27,18 @@ module "vpc" {
|
|||||||
|
|
||||||
# Get VPC data
|
# Get VPC data
|
||||||
module "vpc_data" {
|
module "vpc_data" {
|
||||||
depends_on = [module.vpc]
|
|
||||||
source = "../modules/vpc_data"
|
source = "../modules/vpc_data"
|
||||||
|
depends_on = [module.vpc]
|
||||||
|
|
||||||
VPC_NAME = var.DEMO_VPC_NAME
|
VPC_NAME = var.DEMO_VPC_NAME
|
||||||
BASTION_HOST_AZ = var.DEMO_BASTION_HOST_AZ
|
BASTION_HOST_AZ = var.DEMO_BASTION_HOST_AZ
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Get AMI data
|
||||||
|
module "ami_data" {
|
||||||
|
source = "../modules/ami_data"
|
||||||
|
}
|
||||||
|
|
||||||
# Create bastion host
|
# Create bastion host
|
||||||
module "bastion_host" {
|
module "bastion_host" {
|
||||||
source = "../modules/bastion_host"
|
source = "../modules/bastion_host"
|
||||||
@ -41,30 +46,10 @@ module "bastion_host" {
|
|||||||
VPC_ID = module.vpc_data.vpc.id
|
VPC_ID = module.vpc_data.vpc.id
|
||||||
SUBNET_ID = module.vpc_data.bastion_host_subnet.id
|
SUBNET_ID = module.vpc_data.bastion_host_subnet.id
|
||||||
|
|
||||||
INSTANCE_TYPE = var.DEMO_BASTION_HOST_TYPE
|
EC2_AMI = module.ami_data.amazon_linux.id
|
||||||
INSTANCE_NAME = var.DEMO_BASTION_HOST_NAME
|
EC2_TYPE = var.DEMO_BASTION_HOST_TYPE
|
||||||
KEY_NAME = var.DEMO_BASTION_HOST_KEY_NAME
|
EC2_INSTANCE_NAME = var.DEMO_BASTION_HOST_NAME
|
||||||
}
|
EC2_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"]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Create demo app
|
# Create demo app
|
||||||
@ -74,10 +59,10 @@ module "app" {
|
|||||||
VPC_ID = module.vpc_data.vpc.id
|
VPC_ID = module.vpc_data.vpc.id
|
||||||
VPC_SUBNETS_IDS = module.vpc_data.private_subnets.ids
|
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_TYPE = var.DEMO_APP_EC2_TYPE
|
||||||
EC2_KEY_NAME = var.DEMO_APP_EC2_KEY_NAME
|
EC2_KEY_NAME = var.DEMO_APP_EC2_KEY_NAME
|
||||||
|
|
||||||
ASG_MIN_SIZE = var.DEMO_APP_ASG_MIN_SIZE
|
ASG_MIN_SIZE = var.DEMO_APP_ASG_MIN_SIZE
|
||||||
ASG_MAX_SIZE = var.DEMO_APP_ASG_MAX_SIZE
|
ASG_MAX_SIZE = var.DEMO_APP_ASG_MAX_SIZE
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user