Skip to content

Instantly share code, notes, and snippets.

@jpbarto
Created March 16, 2019 21:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpbarto/88a35ebebf3f6bb9386038f813ed03ab to your computer and use it in GitHub Desktop.
Save jpbarto/88a35ebebf3f6bb9386038f813ed03ab to your computer and use it in GitHub Desktop.
A Terraform template to create an AWS autoscaling group tied to a network load balancer, to test the ability to create a Socket.IO server that has no route to the Internet
##
## AMI created using
## curl -sL https://rpm.nodesource.com/setup_11.x | bash -
## yum install -y git nodejs
## git clone https://github.com/socketio/socket.io.git
##
## cd socket.io
## npm install
## cd examples/chat
## npm install
##
## npm i -g forever-service forever
## mv index.js app.js
## forever install chat
##
variable "public_subnet_ids" {
default = ["subnet-147ce96f"]
}
variable "private_subnet_ids" {
default = ["subnet-1758df6c"] # , "subnet-332af87e"]
}
variable "ec2_ami" {
default = "ami-0944afc53d6743596"
}
variable "vpc_id" {
default = "vpc-ba46d2d6"
}
variable "ec2_key_name" {
default = "my-eu-west-2-keypair"
}
resource "aws_lb" "ws_lb" {
name = "ws-lb"
internal = false
load_balancer_type = "network"
subnets = "${var.public_subnet_ids}"
enable_cross_zone_load_balancing = true
enable_deletion_protection = false
}
resource "aws_lb_target_group" "ws_tg" {
name = "ws-lb-tg"
port = 3000
protocol = "TCP"
vpc_id = "${var.vpc_id}"
}
resource "aws_lb_listener" "ws_lb_listener" {
load_balancer_arn = "${aws_lb.ws_lb.arn}"
port = "3000"
protocol = "TCP"
default_action {
type = "forward"
target_group_arn = "${aws_lb_target_group.ws_tg.arn}"
}
}
resource "aws_security_group" "allow_3000" {
name = "allow_3000"
vpc_id = "${var.vpc_id}"
ingress {
from_port = 3000
to_port = 3000
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_launch_configuration" "ws_node" {
name = "ws-node-lc"
image_id = "${var.ec2_ami}"
instance_type = "t2.micro"
security_groups = ["${aws_security_group.allow_3000.id}"]
associate_public_ip_address = false
key_name = "${var.ec2_key_name}"
}
resource "aws_autoscaling_group" "ws_asg" {
name = "ws-asg"
vpc_zone_identifier = "${var.private_subnet_ids}"
# vpc_zone_identifier = "${var.public_subnet_ids}"
desired_capacity = 1
max_size = 2
min_size = 0
launch_configuration = "${aws_launch_configuration.ws_node.name}"
target_group_arns = ["${aws_lb_target_group.ws_tg.arn}"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment