Skip to content

Instantly share code, notes, and snippets.

@LeePorte
Last active September 5, 2022 07:44
Show Gist options
  • Save LeePorte/a4793d8d7f183d1914a43a8b41cad594 to your computer and use it in GitHub Desktop.
Save LeePorte/a4793d8d7f183d1914a43a8b41cad594 to your computer and use it in GitHub Desktop.
variable "deployment" {
type = string
}
variable "vpc_id" {
type = string
}
variable "instance_type" {
default = "t3.small"
}
variable "subnet_ids" {
type = list(string)
}
variable "desired_capacity" {
default = 1
}
locals {
syslog_log_group_name = "/${var.deployment}/myapp/web"
}
data "aws_ami" "ubuntu_focal" {
most_recent = true
owners = ["099720109477"]
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
}
output "syslog_log_group_name" {
value = local.syslog_log_group_name
}
resource "aws_launch_template" "web" {
name_prefix = "${var.deployment}-"
ebs_optimized = true
image_id = data.aws_ami.ubuntu_focal.id
instance_type = var.instance_type
vpc_security_group_ids = [aws_security_group.web.id]
user_data = base64encode(templatefile("${path.module}/files/cloud-init.sh", {
deployment = var.deployment,
syslog_log_group_name = local.syslog_log_group_name
}))
block_device_mappings {
device_name = "/dev/sda1"
ebs {
volume_size = 50
volume_type = "gp3"
}
}
iam_instance_profile {
name = aws_iam_instance_profile.web.name
}
tags = {
Deployment = var.deployment
}
}
resource "aws_security_group" "web" {
name = "${var.deployment}-web"
description = "${var.deployment}-web"
vpc_id = var.vpc_id
tags = {
Name = "${var.deployment}-web"
Deployment = var.deployment
}
}
resource "aws_security_group_rule" "ingress_to_443" {
type = "ingress"
protocol = "tcp"
from_port = 443
to_port = 443
security_group_id = aws_security_group.web.id
cidr_blocks = ["0.0.0.0/0"]
}
resource "aws_autoscaling_group" "web" {
name = "${var.deployment}-web"
max_size = var.desired_capacity * 2
min_size = 0
desired_capacity = var.desired_capacity
vpc_zone_identifier = var.subnet_ids
launch_template {
id = aws_launch_template.web.id
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment