Skip to content

Commit 334aa59

Browse files
authored
Merge pull request #114 from spacelift-io/revert-autoscaler-tf
fix: revert autoscaler changes
2 parents 74d402b + 6e5f9a4 commit 334aa59

File tree

15 files changed

+233
-114
lines changed

15 files changed

+233
-114
lines changed

.spacelift/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
version: 1
2-
module_version: 2.8.0
2+
module_version: 2.9.0
33
tests:
44
- name: AMD64-based workerpool
55
project_root: examples/amd64

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,23 +74,35 @@ $ make docs
7474

7575
| Name | Version |
7676
|------|---------|
77+
| <a name="provider_archive"></a> [archive](#provider\_archive) | n/a |
7778
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.55.0 |
79+
| <a name="provider_null"></a> [null](#provider\_null) | n/a |
7880

7981
## Modules
8082

8183
| Name | Source | Version |
8284
|------|--------|---------|
8385
| <a name="module_asg"></a> [asg](#module\_asg) | terraform-aws-modules/autoscaling/aws | ~> 8.0 |
84-
| <a name="module_autoscaler"></a> [autoscaler](#module\_autoscaler) | github.com/spacelift-io/ec2-workerpool-autoscaler//iac | n/a |
8586

8687
## Resources
8788

8889
| Name | Type |
8990
|------|------|
91+
| [aws_cloudwatch_event_rule.scheduling](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_event_rule) | resource |
92+
| [aws_cloudwatch_event_target.scheduling](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_event_target) | resource |
93+
| [aws_cloudwatch_log_group.log_group](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource |
9094
| [aws_iam_instance_profile.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_instance_profile) | resource |
95+
| [aws_iam_role.autoscaler](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
9196
| [aws_iam_role.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
97+
| [aws_iam_role_policy.autoscaler](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
9298
| [aws_iam_role_policy_attachment.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
99+
| [aws_lambda_function.autoscaler](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function) | resource |
100+
| [aws_lambda_permission.allow_cloudwatch_to_call_lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_permission) | resource |
101+
| [aws_ssm_parameter.spacelift_api_key_secret](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ssm_parameter) | resource |
102+
| [null_resource.download](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
103+
| [archive_file.binary](https://registry.terraform.io/providers/hashicorp/archive/latest/docs/data-sources/file) | data source |
93104
| [aws_ami.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami) | data source |
105+
| [aws_iam_policy_document.autoscaler](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
94106
| [aws_region.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
95107

96108
## Inputs
@@ -101,8 +113,7 @@ $ make docs
101113
| <a name="input_ami_id"></a> [ami\_id](#input\_ami\_id) | ID of the Spacelift AMI. If left empty, the latest Spacelift AMI will be used. | `string` | `""` | no |
102114
| <a name="input_autoscaler_architecture"></a> [autoscaler\_architecture](#input\_autoscaler\_architecture) | Instruction set architecture of the autoscaler to use | `string` | `"amd64"` | no |
103115
| <a name="input_autoscaler_s3_package"></a> [autoscaler\_s3\_package](#input\_autoscaler\_s3\_package) | Configuration to retrieve autoscaler lambda package from s3 bucket | <pre>object({<br> bucket = string<br> key = string<br> object_version = optional(string)<br> })</pre> | `null` | no |
104-
| <a name="input_autoscaler_version"></a> [autoscaler\_version](#input\_autoscaler\_version) | Version of the autoscaler to deploy | `string` | `"latest"` | no |
105-
| <a name="input_autoscaling_group_arn"></a> [autoscaling\_group\_arn](#input\_autoscaling\_group\_arn) | autoscaling group ARN. Required for autoscaler | `string` | n/a | yes |
116+
| <a name="input_autoscaler_version"></a> [autoscaler\_version](#input\_autoscaler\_version) | Version of the autoscaler to deploy | `string` | `"v0.3.0"` | no |
106117
| <a name="input_autoscaling_max_create"></a> [autoscaling\_max\_create](#input\_autoscaling\_max\_create) | The maximum number of instances the utility is allowed to create in a single run | `number` | `1` | no |
107118
| <a name="input_autoscaling_max_terminate"></a> [autoscaling\_max\_terminate](#input\_autoscaling\_max\_terminate) | The maximum number of instances the utility is allowed to terminate in a single run | `number` | `1` | no |
108119
| <a name="input_autoscaling_timeout"></a> [autoscaling\_timeout](#input\_autoscaling\_timeout) | Timeout (in seconds) for a single autoscaling run. The more instances you have, the higher this should be. | `number` | `30` | no |
@@ -120,7 +131,6 @@ $ make docs
120131
| <a name="input_max_size"></a> [max\_size](#input\_max\_size) | Maximum number of workers to spin up | `number` | `10` | no |
121132
| <a name="input_min_size"></a> [min\_size](#input\_min\_size) | Minimum numbers of workers to spin up | `number` | `0` | no |
122133
| <a name="input_poweroff_delay"></a> [poweroff\_delay](#input\_poweroff\_delay) | Number of seconds to wait before powering the EC2 instance off after the Spacelift launcher stopped | `number` | `15` | no |
123-
| <a name="input_region"></a> [region](#input\_region) | AWS region to deploy to | `string` | n/a | yes |
124134
| <a name="input_schedule_expression"></a> [schedule\_expression](#input\_schedule\_expression) | Autoscaler scheduling expression | `string` | `"rate(1 minute)"` | no |
125135
| <a name="input_security_groups"></a> [security\_groups](#input\_security\_groups) | List of security groups to use | `list(string)` | n/a | yes |
126136
| <a name="input_spacelift_api_key_endpoint"></a> [spacelift\_api\_key\_endpoint](#input\_spacelift\_api\_key\_endpoint) | Full URL of the Spacelift API endpoint to use, eg. https://demo.app.spacelift.io | `string` | `null` | no |

autoscaler.tf

Lines changed: 80 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,97 @@
1-
module "autoscaler" {
2-
source = "github.com/spacelift-io/ec2-workerpool-autoscaler//iac"
3-
4-
for_each = var.enable_autoscaling ? toset(["ENABLED"]) : toset([])
5-
6-
autoscaling_group_arn = var.autoscaling_group_arn
7-
autoscaler_version = var.autoscaler_version
8-
spacelift_api_key_id = var.spacelift_api_key_id
9-
spacelift_api_key_secret = var.spacelift_api_key_secret
10-
spacelift_api_key_endpoint = var.spacelift_api_key_endpoint
11-
worker_pool_id = var.worker_pool_id
12-
autoscaler_architecture = var.autoscaler_architecture
13-
autoscaling_timeout = var.autoscaling_timeout
14-
autoscaling_max_create = var.autoscaling_max_create
15-
autoscaling_max_terminate = var.autoscaling_max_terminate
16-
schedule_expression = var.schedule_expression
17-
base_name = var.base_name
18-
region = var.region
19-
autoscaler_s3_package = var.autoscaler_s3_package
20-
subnet_ids = var.vpc_subnets
21-
security_group_ids = var.security_groups
22-
23-
depends_on = [module.asg]
1+
locals {
2+
function_name = "${local.base_name}-ec2-autoscaler"
3+
use_s3_package = var.autoscaler_s3_package != null
244
}
255

26-
moved {
27-
from = aws_ssm_parameter.spacelift_api_key_secret[0]
28-
to = module.autoscaler["ENABLED"].aws_ssm_parameter.spacelift_api_key_secret
6+
resource "aws_ssm_parameter" "spacelift_api_key_secret" {
7+
count = var.enable_autoscaling ? 1 : 0
8+
name = "/${local.function_name}/spacelift-api-secret-${var.worker_pool_id}"
9+
type = "SecureString"
10+
value = var.spacelift_api_key_secret
11+
tags = var.additional_tags
2912
}
3013

31-
moved {
32-
from = null_resource.download[0]
33-
to = module.autoscaler["ENABLED"].null_resource.download
14+
resource "null_resource" "download" {
15+
count = var.enable_autoscaling && !local.use_s3_package ? 1 : 0
16+
triggers = {
17+
# Always re-download the archive file
18+
now = timestamp()
19+
}
20+
provisioner "local-exec" {
21+
command = "${path.module}/download.sh ${var.autoscaler_version} ${var.autoscaler_architecture}"
22+
}
3423
}
3524

36-
moved {
37-
from = aws_lambda_function.autoscaler[0]
38-
to = module.autoscaler["ENABLED"].aws_lambda_function.autoscaler
25+
data "archive_file" "binary" {
26+
count = var.enable_autoscaling && !local.use_s3_package ? 1 : 0
27+
type = "zip"
28+
source_file = "lambda/bootstrap"
29+
output_path = "ec2-workerpool-autoscaler_${var.autoscaler_version}.zip"
30+
depends_on = [null_resource.download]
3931
}
4032

41-
moved {
42-
from = aws_cloudwatch_event_rule.scheduling[0]
43-
to = module.autoscaler["ENABLED"].aws_cloudwatch_event_rule.scheduling
33+
resource "aws_lambda_function" "autoscaler" {
34+
count = var.enable_autoscaling ? 1 : 0
35+
36+
filename = !local.use_s3_package ? data.archive_file.binary[count.index].output_path : null
37+
source_code_hash = !local.use_s3_package ? data.archive_file.binary[count.index].output_base64sha256 : null
38+
39+
s3_bucket = local.use_s3_package ? var.autoscaler_s3_package.bucket : null
40+
s3_key = local.use_s3_package ? var.autoscaler_s3_package.key : null
41+
s3_object_version = local.use_s3_package ? var.autoscaler_s3_package.object_version : null
42+
43+
function_name = local.function_name
44+
role = aws_iam_role.autoscaler[count.index].arn
45+
handler = "bootstrap"
46+
runtime = "provided.al2"
47+
architectures = [var.autoscaler_architecture == "amd64" ? "x86_64" : var.autoscaler_architecture]
48+
timeout = var.autoscaling_timeout
49+
50+
environment {
51+
variables = {
52+
AUTOSCALING_GROUP_ARN = module.asg.autoscaling_group_arn
53+
AUTOSCALING_REGION = data.aws_region.this.name
54+
SPACELIFT_API_KEY_ID = var.spacelift_api_key_id
55+
SPACELIFT_API_KEY_SECRET_NAME = aws_ssm_parameter.spacelift_api_key_secret[count.index].name
56+
SPACELIFT_API_KEY_ENDPOINT = var.spacelift_api_key_endpoint
57+
SPACELIFT_WORKER_POOL_ID = var.worker_pool_id
58+
AUTOSCALING_MAX_CREATE = var.autoscaling_max_create
59+
AUTOSCALING_MAX_KILL = var.autoscaling_max_terminate
60+
}
61+
}
62+
63+
tracing_config {
64+
mode = "Active"
65+
}
66+
tags = var.additional_tags
4467
}
4568

46-
moved {
47-
from = aws_cloudwatch_event_target.scheduling[0]
48-
to = module.autoscaler["ENABLED"].aws_cloudwatch_event_target.scheduling
69+
resource "aws_cloudwatch_event_rule" "scheduling" {
70+
count = var.enable_autoscaling ? 1 : 0
71+
name = local.function_name
72+
description = "Spacelift autoscaler scheduling for worker pool ${var.worker_pool_id}"
73+
schedule_expression = var.schedule_expression
74+
tags = var.additional_tags
4975
}
5076

51-
moved {
52-
from = aws_lambda_permission.allow_cloudwatch_to_call_lambda[0]
53-
to = module.autoscaler["ENABLED"].aws_lambda_permission.allow_cloudwatch_to_call_lambda
77+
resource "aws_cloudwatch_event_target" "scheduling" {
78+
count = var.enable_autoscaling ? 1 : 0
79+
rule = aws_cloudwatch_event_rule.scheduling[count.index].name
80+
arn = aws_lambda_function.autoscaler[count.index].arn
5481
}
5582

56-
moved {
57-
from = aws_cloudwatch_log_group.log_group[0]
58-
to = module.autoscaler["ENABLED"].aws_cloudwatch_log_group.log_group
83+
resource "aws_lambda_permission" "allow_cloudwatch_to_call_lambda" {
84+
count = var.enable_autoscaling ? 1 : 0
85+
statement_id = "AllowExecutionFromCloudWatch"
86+
action = "lambda:InvokeFunction"
87+
function_name = aws_lambda_function.autoscaler[count.index].function_name
88+
principal = "events.amazonaws.com"
89+
source_arn = aws_cloudwatch_event_rule.scheduling[count.index].arn
5990
}
6091

61-
moved {
62-
from = aws_iam_role.autoscaler[0]
63-
to = module.autoscaler["ENABLED"].aws_iam_role.autoscaler
92+
resource "aws_cloudwatch_log_group" "log_group" {
93+
count = var.enable_autoscaling ? 1 : 0
94+
name = "/aws/lambda/${local.function_name}"
95+
retention_in_days = 7
96+
tags = var.additional_tags
6497
}

download.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env sh
2+
set -ex
3+
4+
# Download the data.
5+
code_version=$1
6+
code_architecture=$2
7+
8+
curl -L -o lambda.zip "https://github.com/spacelift-io/ec2-workerpool-autoscaler/releases/download/${code_version}/ec2-workerpool-autoscaler_linux_${code_architecture}.zip"
9+
10+
mkdir -p lambda
11+
cd lambda
12+
unzip -o ../lambda.zip
13+
rm ../lambda.zip

examples/amd64/main.tf

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ module "this" {
3838
export SPACELIFT_TOKEN="<token-here>"
3939
export SPACELIFT_POOL_PRIVATE_KEY="<private-key-here>"
4040
EOT
41-
autoscaling_group_arn = var.autoscaling_group_arn
42-
region = var.region
4341
security_groups = [data.aws_security_group.this.id]
4442
spacelift_api_key_endpoint = var.spacelift_api_key_endpoint
4543
spacelift_api_key_id = var.spacelift_api_key_id

examples/amd64/variables.tf

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,4 @@ variable "spacelift_api_key_endpoint" {
1717
variable "worker_pool_id" {
1818
type = string
1919
description = "ID (ULID) of the the worker pool."
20-
}
21-
22-
variable "autoscaling_group_arn" {
23-
type = string
24-
description = "autoscaling group ARN. Required for autoscaler"
25-
}
26-
27-
variable "region" {
28-
type = string
29-
description = "AWS region to deploy to"
30-
}
20+
}

examples/arm64/main.tf

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ module "this" {
5959
export SPACELIFT_TOKEN="<token-here>"
6060
export SPACELIFT_POOL_PRIVATE_KEY="<private-key-here>"
6161
EOT
62-
autoscaling_group_arn = var.autoscaling_group_arn
63-
region = var.region
6462
ami_id = data.aws_ami.this.id
6563
ec2_instance_type = "t4g.micro"
6664
security_groups = [data.aws_security_group.this.id]

examples/arm64/variables.tf

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,4 @@ variable "spacelift_api_key_endpoint" {
1717
variable "worker_pool_id" {
1818
type = string
1919
description = "ID (ULID) of the the worker pool."
20-
}
21-
22-
variable "autoscaling_group_arn" {
23-
type = string
24-
description = "autoscaling group ARN. Required for autoscaler"
25-
}
26-
27-
variable "region" {
28-
type = string
29-
description = "AWS region to deploy to"
30-
}
20+
}

examples/autoscaler-s3-package/main.tf

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ module "this" {
2323
export SPACELIFT_TOKEN="<token-here>"
2424
export SPACELIFT_POOL_PRIVATE_KEY="<private-key-here>"
2525
EOT
26-
autoscaling_group_arn = var.autoscaling_group_arn
27-
region = var.region
2826
security_groups = [data.aws_security_group.this.id]
2927
spacelift_api_key_endpoint = var.spacelift_api_key_endpoint
3028
spacelift_api_key_id = var.spacelift_api_key_id

examples/autoscaler-s3-package/variables.tf

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,3 @@ variable "autoscaler_architecture" {
3131
description = "Instruction set architecture of the autoscaler to use"
3232
default = "amd64"
3333
}
34-
35-
variable "autoscaling_group_arn" {
36-
type = string
37-
description = "autoscaling group ARN. Required for autoscaler"
38-
}
39-
40-
variable "region" {
41-
type = string
42-
description = "AWS region to deploy to"
43-
}

examples/custom-iam-role/main.tf

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ module "this" {
6161
export SPACELIFT_TOKEN="<token-here>"
6262
export SPACELIFT_POOL_PRIVATE_KEY="<private-key-here>"
6363
EOT
64-
autoscaling_group_arn = var.autoscaling_group_arn
65-
region = var.region
6664
create_iam_role = false
6765
custom_iam_role_name = aws_iam_role.this.name
6866
security_groups = [data.aws_security_group.this.id]

examples/custom-iam-role/variables.tf

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,4 @@ variable "spacelift_api_key_endpoint" {
1717
variable "worker_pool_id" {
1818
type = string
1919
description = "ID (ULID) of the the worker pool."
20-
}
21-
22-
variable "autoscaling_group_arn" {
23-
type = string
24-
description = "autoscaling group ARN. Required for autoscaler"
25-
}
26-
27-
variable "region" {
28-
type = string
29-
description = "AWS region to deploy to"
30-
}
20+
}

0 commit comments

Comments
 (0)