Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modules refactor #1

Merged
merged 3 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions modules/examples/asg/one-instance/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module "asg" {
source = "../../../modules/cluster/asg-rolling-deploy"
cluster_name = var.cluster_name
image_id = data.aws_ami.ubuntu.id
instance_type = "t2.micro"
min_size = 1
max_size = 1
enable_autoscaling = false
subnet_ids = data.aws_subnets.default.ids
}

data "aws_vpc" "default" {
default = true
}

data "aws_subnets" "default" {
filter {
name = "vpc-id"
values = [data.aws_vpc.default.id]
}
}

data "aws_ami" "ubuntu" {
most_recent = true
owners = ["099720109477"] # Canonical
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
}
4 changes: 4 additions & 0 deletions modules/examples/asg/one-instance/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "asg_name" {
value = module.asg.asg_name
description = "The name of the Auto Scaling Group"
}
15 changes: 15 additions & 0 deletions modules/examples/asg/one-instance/terraform.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
terraform {
required_version = ">= 1.0.0, < 2.0.0"

required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
}

provider "aws" {
region = "us-east-1"
}

4 changes: 4 additions & 0 deletions modules/examples/asg/one-instance/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
variable "cluster_name" {
type = string
default = "one-instance-test"
}
3 changes: 3 additions & 0 deletions modules/modules/cluster/asg-rolling-deploy/dependencies.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "aws_ec2_instance_type" "instance" {
instance_type = var.instance_type
}
138 changes: 138 additions & 0 deletions modules/modules/cluster/asg-rolling-deploy/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
locals {
tcp_protocol = "tcp"
all_ips = ["0.0.0.0/0"]
}

resource "aws_launch_configuration" "example" {
image_id = var.image_id
instance_type = var.instance_type
security_groups = [aws_security_group.instance.id]

user_data = var.user_data

lifecycle {
create_before_destroy = true
precondition {
condition = data.aws_ec2_instance_type.instance.free_tier_eligible
error_message = "${var.instance_type} is not free tier eligible"
}
}
}

resource "aws_autoscaling_group" "example" {
name = var.cluster_name
launch_configuration = aws_launch_configuration.example.name
vpc_zone_identifier = var.subnet_ids

lifecycle {
postcondition {
condition = length(self.availability_zones) > 1
error_message = "At least 2 availability zones are required for this cluster"
}
}

target_group_arns = var.target_group_arns
health_check_type = var.health_check_type

min_size = var.min_size
max_size = var.max_size


instance_refresh {
strategy = "Rolling"
preferences {
min_healthy_percentage = 50
}
}

tag {
key = "Name"
value = var.cluster_name
propagate_at_launch = true
}

dynamic "tag" {
for_each = {
for key, value in var.custom_tags :
key => upper(value)
if key != "Name"
}
content {
key = tag.key
value = tag.value
propagate_at_launch = true
}
}
}

resource "aws_autoscaling_schedule" "scale_out_during_business_hours" {
count = var.enable_autoscaling ? 1 : 0

scheduled_action_name = "${var.cluster_name}-scale-out-during-business-hours"
min_size = 2
max_size = 10
desired_capacity = 10
recurrence = "0 9 * * *"
autoscaling_group_name = aws_autoscaling_group.example.name
}

resource "aws_autoscaling_schedule" "scale_in_at_night" {
count = var.enable_autoscaling ? 1 : 0

scheduled_action_name = "${var.cluster_name}-scale-in-at-night"
min_size = 2
max_size = 10
desired_capacity = 2
recurrence = "0 17 * * *"
autoscaling_group_name = aws_autoscaling_group.example.name
}

resource "aws_security_group" "instance" {
name = "${var.cluster_name}-instance"
}

resource "aws_security_group_rule" "allow_server_http_inbound" {
type = "ingress"
security_group_id = aws_security_group.instance.id

from_port = var.server_port
to_port = var.server_port
protocol = local.tcp_protocol
cidr_blocks = local.all_ips
}

resource "aws_cloudwatch_metric_alarm" "high_cpu_utilization" {
alarm_name = "${var.cluster_name}-high-cpu-utilization"
namespace = "AWS/EC2"
metric_name = "CPUUtilization"

dimensions = {
AutoScalingGroupName = aws_autoscaling_group.example.name
}

comparison_operator = "GreaterThanThreshold"
evaluation_periods = 1
period = 300
statistic = "Average"
threshold = 90
unit = "Percent"
}

resource "aws_cloudwatch_metric_alarm" "low_cpu_credit_balance" {
count = format("%.1s", var.instance_type) == "t" ? 1 : 0

alarm_name = "${var.cluster_name}-low-cpu-credit-balance"
namespace = "AWS/EC2"
metric_name = "CPUCreditBalance"

dimensions = {
AutoScalingGroupName = aws_autoscaling_group.example.name
}

comparison_operator = "LessThanThreshold"
evaluation_periods = 1
period = 300
statistic = "Minimum"
threshold = 10
unit = "Count"
}
9 changes: 9 additions & 0 deletions modules/modules/cluster/asg-rolling-deploy/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
output "asg_name" {
value = aws_autoscaling_group.example.name
description = "The name of the Auto Scaling Group"
}

output "instance_security_group_id" {
value = aws_security_group.instance.id
description = "The ID of the EC2 Instance Security Group"
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
terraform {
required_version = ">= 1.0.0, < 2.0.0"

required_providers {
aws = {
source = "hashicorp/aws"
version = "5.50.0"
version = "~> 4.0"
}
}
}

63 changes: 63 additions & 0 deletions modules/modules/cluster/asg-rolling-deploy/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
variable "cluster_name" {
description = "The name to use for all cluster resources"
type = string
}

variable "image_id" {
description = "The ID of the machine image (AMI) to use for the instance"
type = string
}

variable "instance_type" {
description = "The type of instance to start"
type = string
}

variable "min_size" {
description = "The minimum size of the Auto Scaling group"
type = number
}

variable "max_size" {
description = "The maximum size of the Auto Scaling group"
type = number
}

variable "custom_tags" {
description = "Costum tag to set on the instanc in the ASG"
default = {}
}

variable "server_port" {
description = "The port the server will use for HTTP requests"
type = number
default = 8080
}

variable "enable_autoscaling" {
description = "If set to true enable the Auto Scaling"
type = bool
}

variable "subnet_ids" {
description = "The subnet IDs to deploy to"
type = list(string)
}

variable "target_group_arns" {
description = "The ARNs of ELB target groups in which to register Instances"
type = list(string)
default = []
}

variable "health_check_type" {
description = "The type of health check to perform. Must be one of: EC2, ELB."
type = string
default = "EC2"
}

variable "user_data" {
description = "The User Data script to run in each Instance at boot"
type = string
default = null
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
terraform {
required_version = ">= 1.0.0, < 2.0.0"

required_providers {
aws = {
source = "hashicorp/aws"
version = "5.50.0"
version = "~> 4.0"
}
}
}

11 changes: 11 additions & 0 deletions modules/modules/eks-cluster/terraform.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
terraform {
required_version = ">= 1.0.0, < 2.0.0"

required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ variable "name" {
variable "min_size" {
description = "Minimum number of nodes to have in the EKS cluster"
type = number

validation {
condition = var.min_size > 0
error_message = "ASG can be empty or we'll have an outage!"
}

validation {
condition = var.max_size <= 10
error_message = "ASG must have 10 or less instances to keep costs down"
}
}

variable "max_size" {
Expand All @@ -21,4 +31,9 @@ variable "desired_size" {
variable "instance_types" {
description = "The types of EC2 instances to run in the node group"
type = list(string)

validation {
condition = contains(["t2.micro", "t3.micro"], var.instance_types)
error_message = "Only free tier instances are supported | t2.micro, t3.micro"
}
}
File renamed without changes.
File renamed without changes.
11 changes: 11 additions & 0 deletions modules/modules/k8s/terraform.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
terraform {
required_version = ">= 1.0.0, < 2.0.0"

required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
}

File renamed without changes.
Loading