Add load balancer and scaling groups
@@ -2,6 +2,8 @@ provider "aws" {
region = "us-west-1"
}
+data "aws_availability_zones" "available" {}
+
resource "aws_instance" "cccatalog-api-server" {
# Amazon Linux 2 LTS Candidate 2
ami = "ami-00d8c660"
@@ -16,11 +18,36 @@ resource "aws_instance" "cccatalog-api-server" {
vpc_security_group_ids = ["${aws_security_group.cccatalog-api-ingress.id}",
"${aws_security_group.cccatalog-sg.id}"
]
+
+ lifecycle {
+ create_before_destroy = true
+ }
+}
+
+resource "aws_launch_configuration" "cccatalog-api-launch-config" {
+ image_id = "ami-00d8c660"
+ instance_type = "t2.micro"
+ security_groups = ["${aws_security_group.cccatalog-sg.id}"]
+
lifecycle {
create_before_destroy = true
}
}
+resource "aws_autoscaling_group" "cccatalog-api-asg" {
+ launch_configuration = "${aws_launch_configuration.cccatalog-api-launch-config.id}"
+ min_size = 2
+ max_size = 5
+ availability_zones = ["${data.aws_availability_zones.available.names}"]
+ load_balancers = ["${aws_alb.cccatalog-api-load-balancer.id}"]
+
+ tag {
+ key = "Name"
+ value = "cccatalog-api-autoscaling-group"
+ propagate_at_launch = true
+ }
+}
+
resource "aws_security_group" "cccatalog-api-ingress" {
name = "cccatalog-api-ingress"
@@ -51,20 +78,21 @@ resource "aws_security_group" "cccatalog-sg" {
}
# Public-facing load balancer
-resource "aws_lb" "cccatalog-api-load-balancer" {
- name = "cccatalog-api-lb"
+resource "aws_alb" "cccatalog-api-load-balancer" {
+ name = "cccatalog-api-alb"
internal = false
load_balancer_type = "application"
security_groups = ["${aws_security_group.cccatalog-sg.id}"]
enable_deletion_protection = false
tags {
+ Name = "cccatalog-api-load-balancer-dev"
Environment = "dev"
}
}
-resource "aws_security_group" "cccatalog-lb-ingress" {
- name = "cccatalog-lb-ingress"
+resource "aws_security_group" "cccatalog-alb-ingress" {
+ name = "cccatalog-alb-ingress"
vpc_id = "vpc-d6b1bfb4"
ingress {
