86294778 by Volodymyr Tsap

Adding VPC and ELB modules to sample5

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