86294778 by Volodymyr Tsap

Adding VPC and ELB modules to sample5

1 parent 47bc539f
## Create LoadBalancer
module "m-elb-xpdays" {
source = "./elb"
elb_name = "elb-xpdays"
vpc_id = "${module.vpc.vpc_id}"
subnet_az1 = "${aws_subnet.default_subnet.id}"
subnet_az2 = "${aws_subnet.default_subnet.id}"
backend_port = "3334"
backend_protocol = "http"
# ssl_certificate_id = "${data.aws_acm_certificate.wisehands.me.arn}"
health_check_target = "HTTP:3334/"
# elb_security_group = "${aws_security_group.elb-sg.id}"
}
## Add rule for access to ELB SG into default SG
resource "aws_security_group_rule" "allow_3334_xpdays" {
type = "ingress"
from_port = 3334
to_port = 3334
protocol = "tcp"
source_security_group_id = "${module.m-elb-xpdays.elb_sg_id}"
security_group_id = "${module.vpc.vpc_default_security_group}"
}
resource "aws_elb" "elb" {
name = "${var.elb_name}"
subnets = ["${var.subnet_az1}", "${var.subnet_az2}"]
internal = "${var.elb_is_internal}"
security_groups = ["${aws_security_group.elb-sg.id}"]
# listener {
# instance_port = "${var.backend_port}"
# instance_protocol = "${var.backend_protocol}"
# lb_port = 443
# lb_protocol = "https"
# # ssl_certificate_id = "${var.ssl_certificate_id}"
# }
listener {
instance_port = "${var.backend_port}"
instance_protocol = "${var.backend_protocol}"
lb_port = 80
lb_protocol = "http"
}
health_check {
healthy_threshold = 2
unhealthy_threshold = 2
timeout = 3
target = "${var.health_check_target}"
interval = 30
}
cross_zone_load_balancing = true
}
resource "aws_security_group" "elb-sg" {
vpc_id = "${var.vpc_id}"
name = "elb-sg-${var.elb_name}"
description = "Security Group for ELB"
}
resource "aws_security_group_rule" "allow_http" {
type = "ingress"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_security_group.elb-sg.id}"
}
resource "aws_security_group_rule" "allow_https" {
type = "ingress"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_security_group.elb-sg.id}"
}
resource "aws_security_group_rule" "allow_outbound_all" {
type = "egress"
from_port = 0
to_port = 65535
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_security_group.elb-sg.id}"
}
output "elb_id" {
value = "${aws_elb.elb.id}"
}
output "elb_sg_id" {
value = "${aws_security_group.elb-sg.id}"
}
output "elb_name" {
value = "${aws_elb.elb.name}"
}
output "elb_dns_name" {
value = "${aws_elb.elb.dns_name}"
}
variable "elb_name" {}
variable "vpc_id" {}
variable "elb_is_internal" {
description = "Determines if the ELB is internal or not"
default = false
// Defaults to false, which results in an external IP for the ELB
}
#variable "elb_security_group" {}
// See README.md for details on finding the
// ARN of an SSL certificate in EC2
#variable "ssl_certificate_id" {
# description = "The ARN of the SSL Certificate in EC2"
#}
variable "subnet_az1" {
description = "The subnet for AZ1"
}
variable "subnet_az2" {
description = "The subnet for AZ2"
}
variable "backend_port" {
description = "The port the service on the EC2 instances listens on"
}
variable "backend_protocol" {
description = "The protocol the backend service speaks"
// Possible options are
// - http
// - https
// - tcp
// - ssl (secure tcp)
}
variable "health_check_target" {
description = "The URL the ELB should use for health checks"
// This is primarily used with `http` or `https` backend protocols
// The format is like `HTTPS:443/health`
}
# Template for initial configuration bash script
## Template for initial configuration bash script
data "template_file" "init" {
template = "${file("files/init.tpl")}"
count = "${length(var.instance_suffix)}"
......@@ -9,42 +9,38 @@ data "template_file" "init" {
}
}
## Creating lauch configuration:
resource "aws_launch_configuration" "launch-xpdays" {
# name = "${var.environment}-launch-xpdays${count.index + 1}"
image_id = "${data.aws_ami.xpdays-ami.id}"
instance_type = "${lookup(var.instance_type, var.environment)}"
iam_instance_profile = "${aws_iam_instance_profile.ec2-instance-profile.name}"
associate_public_ip_address = true
enable_monitoring = true
user_data = "${data.template_file.init.*.rendered[count.index]}"
lifecycle {
create_before_destroy = true
}
lifecycle { create_before_destroy = true }
count = "${length(var.instance_suffix)}"
}
## Add Autoscaling group
resource "aws_autoscaling_group" "asg-xpdays" {
lifecycle { create_before_destroy = true }
# depends_on = ["aws_launch_configuration.launch-xpdays"]
desired_capacity = "${element(var.instance_count_xpdays_desired[var.environment],count.index)}"
# lifecycle { create_before_destroy = true }
# depends_on = ["aws_launch_configuration.launch-xpdays"]
desired_capacity = "${lookup(var.instance_count_xpdays_desired, var.environment)}"
max_size = "${lookup(var.instance_count_xpdays_max, var.environment)}"
min_size = "${lookup(var.instance_count_xpdays_min, var.environment)}"
health_check_grace_period = 300
health_check_type = "EC2"
launch_configuration = "${element(aws_launch_configuration.launch-xpdays.*.name, count.index)}"
name = "asg-xpdays${count.index + 1}-${var.environment}"
availability_zones = ["${lookup(var.default_subnet_availability_zone, var.environment)}"]
name = "asg-xpdays-${var.instance_suffix[count.index]}"
vpc_zone_identifier = ["${list(aws_subnet.default_subnet.id)}"]
# load_balancers = ["${module.m-elb-xpdays.elb_id}"]
#wait_for_elb_capacity = "${element(var.instance_count_xpdays_desired[var.environment],count.index)}"
# enabled_metrics = "${var.asg_enabled_metrics}"
availability_zones = ["${lookup(var.default_subnet_availability_zone, var.environment)}"]
load_balancers = ["${module.m-elb-xpdays.elb_id}"]
# wait_for_elb_capacity = "${element(var.instance_count_xpdays_desired[var.environment],count.index)}"
enabled_metrics = "${var.asg_enabled_metrics}"
tag {
key = "Name"
value = "xpdays-${var.instance_suffix[count.index]}-${count.index}"
value = "xpdays-${var.instance_suffix[count.index]}"
propagate_at_launch = true
}
count = "${length(var.instance_suffix)}"
}
......
## Define provider
provider "aws" {
region = "${var.region}"
}
resource "aws_db_subnet_group" "default_db_subnet_group" {
name = "main"
subnet_ids = ["${var.default_db_subnet_group_subnet_ids[var.region]}"]
subnet_ids = ["${aws_subnet.default_subnet.id}","${aws_subnet.default_db_subnet.id}"]
tags {
Name = "Default DB subnet group"
}
......
## Autscale policy
resource "aws_autoscaling_policy" "scale_in_xpdays" {
name = "autoscale_in_policy_xpdays-${var.instance_suffix[count.index]}"
adjustment_type = "ChangeInCapacity"
policy_type = "StepScaling"
estimated_instance_warmup = 35
autoscaling_group_name = "${element(aws_autoscaling_group.asg-xpdays.*.name, count.index)}"
step_adjustment {
scaling_adjustment = 0
metric_interval_upper_bound = 1
}
step_adjustment {
scaling_adjustment = 1
metric_interval_lower_bound = 1
}
count = "${length(var.instance_suffix)}"
}
## Autoscale Alarm Metrics
resource "aws_cloudwatch_metric_alarm" "metric_alarm_cpu_high_xpdays" {
alarm_name = "metric_alarm_high_cpu_xpdays_${var.instance_suffix[count.index]}"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = "300"
statistic = "Average"
threshold = "79"
dimensions {
AutoScalingGroupName = "${element(aws_autoscaling_group.asg-xpdays.*.name, count.index)}"
}
alarm_description = "This metric monitors xpdays group cpu utilization"
alarm_actions = ["${element(aws_autoscaling_policy.scale_in_xpdays.*.arn, count.index)}"]
count = "${length(var.instance_suffix)}"
}
resource "aws_autoscaling_policy" "scale_out_xpdays" {
name = "autoscale_out_policy_xpdays-${var.instance_suffix[count.index]}"
adjustment_type = "ChangeInCapacity"
policy_type = "StepScaling"
estimated_instance_warmup = 35
autoscaling_group_name = "${element(aws_autoscaling_group.asg-xpdays.*.name, count.index)}"
step_adjustment {
scaling_adjustment = -1 # remove one node
# scaling_adjustment = 0 # disable downscale
metric_interval_upper_bound = 1
}
step_adjustment {
scaling_adjustment = 0
metric_interval_lower_bound = 1
}
count = "${length(var.instance_suffix)}"
}
resource "aws_cloudwatch_metric_alarm" "metric_alarm_cpu_low_xpdays" {
alarm_name = "metric_alarm_low_cpu_xpdays_${var.instance_suffix[count.index]}"
comparison_operator = "LessThanThreshold"
evaluation_periods = "1"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = "300"
statistic = "Average"
threshold = "39"
dimensions {
AutoScalingGroupName = "${element(aws_autoscaling_group.asg-xpdays.*.name, count.index)}"
}
alarm_description = "This metric monitors xpdays group cpu utilization"
alarm_actions = ["${element(aws_autoscaling_policy.scale_out_xpdays.*.arn, count.index)}"]
count = "${length(var.instance_suffix)}"
}
## Print Output
output "xpdays_elb_dns_name" {
value = "${module.m-elb-xpdays.elb_dns_name}"
}
# String
region = "eu-central-1"
### AWS related
# List
vpc_security_group_ids = [ "sg-84e649ed", "sg-90ea45fa" ]
region = {
production = "eu-central-1"
development = "eu-west-1"
}
vpc_cidr = {
production = "10.10.0.0/16"
development = "10.3.0.0/16"
}
default_subnet_cidr_block = {
production = "10.10.0.0/22"
development = "10.3.0.0/22"
}
default_db_subnet_cidr_block = {
production = "10.10.4.0/22"
development = "10.3.0.0/22"
}
default_subnet_availability_zone = {
production = "eu-central-1a"
development = "eu-west-1a"
}
default_db_subnet_availability_zone = {
production = "eu-central-1b"
development = "eu-west-1b"
}
# Map
instance_type = {
......@@ -16,6 +40,9 @@ default_db_subnet_group_subnet_ids = {
eu-west-1 = [ "subnet-f1e92d8a", "subnet-304b7f7a" ]
}
asg_enabled_metrics = [ "GroupDesiredCapacity", "GroupPendingInstances", "GroupInServiceInstances", "GroupMaxSize",
"GroupStandbyInstances", "GroupTotalInstances", "GroupMinSize" , "GroupTerminatingInstances" ]
#
instance_suffix = ["blue","green"]
......@@ -25,7 +52,7 @@ instance_count_xpdays_desired = {
development = 1
}
instance_count_xpdays_min = {
production = 1
production = 0
development = 1
}
instance_count_xpdays_max = {
......
# VPC related stuff
variable "region" {
type = "string"
default = "eu-central-1"
description = "The AWS region"
type = "map"
default = {}
description = "The AWS region."
}
variable "environment" {
description = "The Environment Type"
default = "production"
}
variable "default_db_subnet_group_subnet_ids" {
type = "map"
default = {}
variable "vpc_cidr" {
type = "map"
description = "Target VPC default CIDR block"
default = {}
}
variable "vpc_security_group_ids" {
type = "list"
variable "default_subnet_cidr_block" {
type = "map"
description = "The default public local subnet CIDR"
default = {}
}
variable "default_db_subnet_cidr_block" {
type = "map"
description = "The default public local subnet CIDR"
default = {}
}
variable "default_subnet_availability_zone" {
type = "map"
description = "The defaultu environemnt AZ"
default = {}
}
variable "default_db_subnet_availability_zone" {
type = "map"
description = "The defaultu environemnt AZ"
default = {}
}
## Instance definition
variable "instance_type" {
type = "map"
default = {}
}
variable "asg_enabled_metrics" {
type = "list"
}
variable "instance_suffix" {
type = "list"
description = "Add instance suffix"
}
variable "instance_count_xpdays_min" {
type = "map"
default = {}
}
variable "instance_count_xpdays_max" {
type = "map"
default = {}
}
variable "instance_count_xpdays_desired" {
type = "map"
default = {}
}
......
provider "aws" {
region = "${lookup(var.region, var.environment)}"
}
module "vpc" {
source = "./vpc"
name = "${var.environment}"
cidr = "${lookup(var.vpc_cidr, var.environment)}"
enable_dns_support = true
enable_dns_hostnames = true
tags {
Name = "${var.environment}"
"Environment" = "${var.environment}"
}
}
resource "aws_subnet" "default_subnet" {
vpc_id = "${module.vpc.vpc_id}"
cidr_block = "${lookup(var.default_subnet_cidr_block, var.environment)}"
availability_zone = "${lookup(var.default_subnet_availability_zone, var.environment)}"
map_public_ip_on_launch = true
tags {
Name = "default-subnet-${var.environment}"
"Environment" = "${var.environment}"
}
}
resource "aws_subnet" "default_db_subnet" {
vpc_id = "${module.vpc.vpc_id}"
cidr_block = "${lookup(var.default_db_subnet_cidr_block, var.environment)}"
availability_zone = "${lookup(var.default_db_subnet_availability_zone, var.environment)}"
map_public_ip_on_launch = true
tags {
Name = "default-db-subnet${var.environment}"
"Environment" = "${var.environment}"
}
}
resource "aws_internet_gateway" "main" {
vpc_id = "${module.vpc.vpc_id}"
tags {
Name = "${var.environment}"
"Environment" = "${var.environment}"
}
}
resource "aws_route" "default_route" {
route_table_id = "${module.vpc.vpc_main_route_table_id}"
destination_cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.main.id}"
}
resource "aws_security_group_rule" "allow_ssh" {
type = "ingress"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${module.vpc.vpc_default_security_group}"
}
resource "aws_security_group_rule" "allow_icmp" {
type = "ingress"
from_port = 0
to_port = 0
protocol = "icmp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${module.vpc.vpc_default_security_group}"
}
resource "aws_vpc" "vpc" {
cidr_block = "${var.cidr}"
enable_dns_support = "${var.enable_dns_support}"
enable_dns_hostnames = "${var.enable_dns_hostnames}"
tags = "${merge(var.tags, map("Name", format("%s", var.name)))}"
lifecycle {
create_before_destroy = true
}
}
output "vpc_id" {
value = "${aws_vpc.vpc.id}"
}
output "vpc_cidr" {
value = "${aws_vpc.vpc.cidr_block}"
}
output "vpc_default_security_group" {
value = "${aws_vpc.vpc.default_security_group_id}"
}
output "vpc_main_route_table_id" {
value = "${aws_vpc.vpc.main_route_table_id}"
}
variable "name" {
default = "vpc"
}
variable "cidr" {}
variable "enable_dns_support" {
description = "should be true if you want to use private DNS within the VPC"
default = false
}
variable "enable_dns_hostnames" {
description = "should be true if you want to use private DNS within the VPC"
default = false
}
variable "tags" {
description = "A map of tags to add to all resources"
default = {}
}