Skip to content

Commit

Permalink
fix(wp): single asg for multiple purpose (spot, on-demand, warm-pool) (
Browse files Browse the repository at this point in the history
…#70)

* fix(wp): single asg for multiple purpose (spot, on-demand, warm-pool)

* fix(wp): remove warm pool resources

* fix(wp): update warm-pool example

* fix(ssm): remove policy_arns variable

* feat(arm64): new test example for multiple node group types
  • Loading branch information
Young-ook authored May 2, 2022
1 parent 58f25ad commit 6141964
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 244 deletions.
38 changes: 38 additions & 0 deletions examples/arm64/fixture.tc3.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name = "ssm-x86-wp-arm64-tc3"
tags = {
env = "dev"
test = "warm-pool"
}
aws_region = "ap-northeast-2"
node_groups = [
{
name = "x86"
desired_size = 1
instance_type = "t3.small"
ami_type = "AL2_x86_64"
warm_pool = {
pool_state = "Stopped"
max_group_prepared_capacity = 2
}
},
{
name = "arm64"
desired_size = 1
instance_type = "m6g.medium"
ami_type = "AL2_ARM_64"
instances_distribution = {
on_demand_percentage_above_base_capacity = 50
spot_allocation_strategy = "capacity-optimized"
}
instances_override = [
{
instance_type = "m6g.medium"
weighted_capacity = 2
},
{
instance_type = "m6g.large"
weighted_capacity = 1
}
]
},
]
8 changes: 0 additions & 8 deletions examples/ssm-document/default.auto.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,3 @@ tags = {
env = "dev"
}
aws_region = "ap-northeast-2"
node_groups = [
{
name = "default"
desired_size = 1
instance_type = "t3.small"
ami_type = "AL2_x86_64"
},
]
19 changes: 13 additions & 6 deletions examples/ssm-document/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,19 @@ module "vpc" {

# ec2
module "ec2" {
source = "../../"
name = var.name
tags = var.tags
subnets = values(module.vpc.subnets["public"])
policy_arns = ["arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy"]
node_groups = var.node_groups
source = "../../"
name = var.name
tags = var.tags
subnets = values(module.vpc.subnets["public"])
node_groups = [
{
name = "default"
desired_size = 1
instance_type = "t3.small"
ami_type = "AL2_x86_64"
policy_arns = ["arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy"]
},
]
}

# ssm/document
Expand Down
76 changes: 36 additions & 40 deletions examples/warm-pools/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,49 @@ provider "aws" {
region = var.aws_region
}

resource "random_pet" "asg" {}
resource "random_pet" "wp" {}

# default vpc
module "vpc" {
source = "Young-ook/vpc/aws"
version = "1.0.1"
name = random_pet.wp.id
tags = var.tags
}

# ec2
module "ec2" {
source = "../../"
name = random_pet.wp.id
tags = var.tags
subnets = values(module.vpc.subnets["public"])
node_groups = [
{
name = "default"
desired_size = 0
min_size = 0
max_size = 3
instance_type = "t3.small"
user_data = templatefile("${path.module}/userdata.tpl", { lc_name = random_pet.wp.id })
policy_arns = [aws_iam_policy.lc.arn]
warm_pool = {
pool_state = "Stopped"
max_group_prepared_capacity = 2
}
}
]
}

# lifecycle hook complete signal policy
resource "aws_iam_policy" "lc" {
name = join("-", [random_pet.asg.id, "complete-lifecycle-action"])
name = join("-", [random_pet.wp.id, "lc-action"])
tags = var.tags
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = [
"autoscaling:CompleteLifecycleAction",
]
Effect = "Allow"
Resource = [module.ec2.cluster.data_plane.warm_pools.default.arn]
},
{
Action = [
"autoscaling:DescribeAutoScalingInstances",
]
Effect = "Allow"
Expand All @@ -41,46 +66,17 @@ resource "aws_iam_policy" "lc" {
})
}

# default vpc
module "vpc" {
source = "Young-ook/vpc/aws"
version = "1.0.1"
name = var.name
tags = var.tags
}

# ec2
module "ec2" {
source = "../../"
name = random_pet.asg.id
tags = var.tags
subnets = values(module.vpc.subnets["public"])
policy_arns = [aws_iam_policy.lc.arn]
warm_pools = [
{
name = "default"
desired_size = 0
min_size = 0
max_size = 3
instance_type = "t3.small"
pool_state = "Stopped"
max_group_prepared_capacity = 2
user_data = templatefile("${path.module}/userdata.tpl", { lc_name = random_pet.asg.id })
}
]
}

resource "aws_autoscaling_lifecycle_hook" "lc" {
name = random_pet.asg.id
autoscaling_group_name = module.ec2.cluster.data_plane.warm_pools.default.name
name = random_pet.wp.id
autoscaling_group_name = module.ec2.cluster.data_plane.node_groups.default.name
default_result = "ABANDON"
heartbeat_timeout = 900
lifecycle_transition = "autoscaling:EC2_INSTANCE_LAUNCHING"
}

resource "local_file" "elapsedtime" {
content = templatefile("${path.module}/elapsedtime.tpl", {
asg_name = module.ec2.cluster.data_plane.warm_pools.default.name,
asg_name = module.ec2.cluster.data_plane.node_groups.default.name
region = var.aws_region
})
filename = "${path.module}/elapsedtime.sh"
Expand Down
5 changes: 0 additions & 5 deletions examples/warm-pools/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ variable "node_groups" {
default = []
}

variable "warm_pools" {
description = "Warm pools definition"
default = []
}

### description
variable "name" {
description = "The logical name of the module instance"
Expand Down
Loading

0 comments on commit 6141964

Please sign in to comment.