Skip to content
This repository was archived by the owner on Oct 28, 2025. It is now read-only.

Commit c8f4473

Browse files
committed
Use service module and leverage IMDSv2 (closes #12)
1 parent 86ef71c commit c8f4473

File tree

6 files changed

+82
-104
lines changed

6 files changed

+82
-104
lines changed

README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,43 +168,40 @@ module "pipeline_rds" {
168168
| Name | Version |
169169
|------|---------|
170170
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0.0 |
171-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.45.0 |
171+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.72.0 |
172172

173173
## Providers
174174

175175
| Name | Version |
176176
|------|---------|
177-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.45.0 |
177+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.72.0 |
178178

179179
## Modules
180180

181181
| Name | Source | Version |
182182
|------|--------|---------|
183183
| <a name="module_instance_type_metrics"></a> [instance\_type\_metrics](#module\_instance\_type\_metrics) | snowplow-devops/ec2-instance-type-metrics/aws | 0.1.2 |
184184
| <a name="module_kcl_autoscaling"></a> [kcl\_autoscaling](#module\_kcl\_autoscaling) | snowplow-devops/dynamodb-autoscaling/aws | 0.2.0 |
185-
| <a name="module_tags"></a> [tags](#module\_tags) | snowplow-devops/tags/aws | 0.2.0 |
186-
| <a name="module_telemetry"></a> [telemetry](#module\_telemetry) | snowplow-devops/telemetry/snowplow | 0.3.0 |
185+
| <a name="module_service"></a> [service](#module\_service) | snowplow-devops/service-ec2/aws | 0.1.0 |
186+
| <a name="module_telemetry"></a> [telemetry](#module\_telemetry) | snowplow-devops/telemetry/snowplow | 0.4.0 |
187187

188188
## Resources
189189

190190
| Name | Type |
191191
|------|------|
192-
| [aws_autoscaling_group.asg](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/autoscaling_group) | resource |
193192
| [aws_cloudwatch_log_group.log_group](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource |
194193
| [aws_dynamodb_table.kcl](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/dynamodb_table) | resource |
195194
| [aws_iam_instance_profile.instance_profile](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_instance_profile) | resource |
196195
| [aws_iam_policy.iam_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
197196
| [aws_iam_role.iam_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
198197
| [aws_iam_role_policy_attachment.policy_attachment](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
199-
| [aws_launch_configuration.lc](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/launch_configuration) | resource |
200198
| [aws_security_group.sg](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource |
201199
| [aws_security_group_rule.egress_tcp_443](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule) | resource |
202200
| [aws_security_group_rule.egress_tcp_80](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule) | resource |
203201
| [aws_security_group_rule.egress_tcp_server_rds](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule) | resource |
204202
| [aws_security_group_rule.egress_udp_123](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule) | resource |
205203
| [aws_security_group_rule.ingress_tcp_22](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule) | resource |
206204
| [aws_security_group_rule.rds_egress_tcp_webserver](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule) | resource |
207-
| [aws_ami.amazon_linux_2](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami) | data source |
208205
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
209206
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
210207

@@ -232,6 +229,7 @@ module "pipeline_rds" {
232229
| <a name="input_custom_iglu_resolvers"></a> [custom\_iglu\_resolvers](#input\_custom\_iglu\_resolvers) | The custom Iglu Resolvers that will be used by Enrichment to resolve and validate events | <pre>list(object({<br> name = string<br> priority = number<br> uri = string<br> api_key = string<br> vendor_prefixes = list(string)<br> }))</pre> | `[]` | no |
233230
| <a name="input_db_max_connections"></a> [db\_max\_connections](#input\_db\_max\_connections) | The maximum number of connections to the backing database | `number` | `10` | no |
234231
| <a name="input_default_iglu_resolvers"></a> [default\_iglu\_resolvers](#input\_default\_iglu\_resolvers) | The default Iglu Resolvers that will be used by Enrichment to resolve and validate events | <pre>list(object({<br> name = string<br> priority = number<br> uri = string<br> api_key = string<br> vendor_prefixes = list(string)<br> }))</pre> | <pre>[<br> {<br> "api_key": "",<br> "name": "Iglu Central",<br> "priority": 10,<br> "uri": "http://iglucentral.com",<br> "vendor_prefixes": []<br> },<br> {<br> "api_key": "",<br> "name": "Iglu Central - Mirror 01",<br> "priority": 20,<br> "uri": "http://mirror01.iglucentral.com",<br> "vendor_prefixes": []<br> }<br>]</pre> | no |
232+
| <a name="input_enable_auto_scaling"></a> [enable\_auto\_scaling](#input\_enable\_auto\_scaling) | Whether to enable auto-scaling policies for the service (WARN: ensure you have sufficient db\_connections available for the max number of nodes in the ASG) | `bool` | `true` | no |
235233
| <a name="input_iam_permissions_boundary"></a> [iam\_permissions\_boundary](#input\_iam\_permissions\_boundary) | The permissions boundary ARN to set on IAM roles created | `string` | `""` | no |
236234
| <a name="input_in_max_batch_size_checkpoint"></a> [in\_max\_batch\_size\_checkpoint](#input\_in\_max\_batch\_size\_checkpoint) | The maximum number events to process before checkpointing progress on the stream | `number` | `1000` | no |
237235
| <a name="input_in_max_batch_wait_checkpoint"></a> [in\_max\_batch\_wait\_checkpoint](#input\_in\_max\_batch\_wait\_checkpoint) | The maximum amount of time to wait before checkpointing progress on the stream | `string` | `"10 seconds"` | no |
@@ -244,6 +242,12 @@ module "pipeline_rds" {
244242
| <a name="input_kcl_write_min_capacity"></a> [kcl\_write\_min\_capacity](#input\_kcl\_write\_min\_capacity) | The minimum WRITE capacity for the KCL DynamoDB table | `number` | `1` | no |
245243
| <a name="input_max_size"></a> [max\_size](#input\_max\_size) | The maximum number of servers in this server-group | `number` | `2` | no |
246244
| <a name="input_min_size"></a> [min\_size](#input\_min\_size) | The minimum number of servers in this server-group | `number` | `1` | no |
245+
| <a name="input_scale_down_cooldown_sec"></a> [scale\_down\_cooldown\_sec](#input\_scale\_down\_cooldown\_sec) | Time (in seconds) until another scale-down action can occur | `number` | `600` | no |
246+
| <a name="input_scale_down_cpu_threshold_percentage"></a> [scale\_down\_cpu\_threshold\_percentage](#input\_scale\_down\_cpu\_threshold\_percentage) | The average CPU percentage that we must be below to scale-down | `number` | `20` | no |
247+
| <a name="input_scale_down_eval_minutes"></a> [scale\_down\_eval\_minutes](#input\_scale\_down\_eval\_minutes) | The number of consecutive minutes that we must be below the threshold to scale-down | `number` | `60` | no |
248+
| <a name="input_scale_up_cooldown_sec"></a> [scale\_up\_cooldown\_sec](#input\_scale\_up\_cooldown\_sec) | Time (in seconds) until another scale-up action can occur | `number` | `180` | no |
249+
| <a name="input_scale_up_cpu_threshold_percentage"></a> [scale\_up\_cpu\_threshold\_percentage](#input\_scale\_up\_cpu\_threshold\_percentage) | The average CPU percentage that must be exceeded to scale-up | `number` | `60` | no |
250+
| <a name="input_scale_up_eval_minutes"></a> [scale\_up\_eval\_minutes](#input\_scale\_up\_eval\_minutes) | The number of consecutive minutes that the threshold must be breached to scale-up | `number` | `5` | no |
247251
| <a name="input_ssh_ip_allowlist"></a> [ssh\_ip\_allowlist](#input\_ssh\_ip\_allowlist) | The list of CIDR ranges to allow SSH traffic from | `list(any)` | <pre>[<br> "0.0.0.0/0"<br>]</pre> | no |
248252
| <a name="input_tags"></a> [tags](#input\_tags) | The tags to append to this resource | `map(string)` | `{}` | no |
249253
| <a name="input_telemetry_enabled"></a> [telemetry\_enabled](#input\_telemetry\_enabled) | Whether or not to send telemetry information back to Snowplow Analytics Ltd | `bool` | `true` | no |

main.tf

Lines changed: 24 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ data "aws_caller_identity" "current" {}
2626

2727
module "telemetry" {
2828
source = "snowplow-devops/telemetry/snowplow"
29-
version = "0.3.0"
29+
version = "0.4.0"
3030

3131
count = var.telemetry_enabled ? 1 : 0
3232

@@ -39,27 +39,6 @@ module "telemetry" {
3939
module_version = local.module_version
4040
}
4141

42-
data "aws_ami" "amazon_linux_2" {
43-
most_recent = true
44-
45-
filter {
46-
name = "name"
47-
values = ["amzn2-ami-hvm-*-x86_64-ebs"]
48-
}
49-
50-
filter {
51-
name = "root-device-type"
52-
values = ["ebs"]
53-
}
54-
55-
filter {
56-
name = "virtualization-type"
57-
values = ["hvm"]
58-
}
59-
60-
owners = ["amazon"]
61-
}
62-
6342
# --- DynamoDB: KCL Table
6443

6544
resource "aws_dynamodb_table" "kcl" {
@@ -345,58 +324,30 @@ locals {
345324
})
346325
}
347326

348-
resource "aws_launch_configuration" "lc" {
349-
name_prefix = "${var.name}-"
327+
module "service" {
328+
source = "snowplow-devops/service-ec2/aws"
329+
version = "0.1.0"
350330

351-
image_id = var.amazon_linux_2_ami_id == "" ? data.aws_ami.amazon_linux_2.id : var.amazon_linux_2_ami_id
352-
instance_type = var.instance_type
353-
key_name = var.ssh_key_name
354-
iam_instance_profile = aws_iam_instance_profile.instance_profile.name
355-
security_groups = [aws_security_group.sg.id]
356-
user_data = local.user_data
331+
user_supplied_script = local.user_data
332+
name = var.name
333+
tags = local.tags
357334

358-
# Note: Required if deployed in a public subnet
335+
amazon_linux_2_ami_id = var.amazon_linux_2_ami_id
336+
instance_type = var.instance_type
337+
ssh_key_name = var.ssh_key_name
338+
iam_instance_profile_name = aws_iam_instance_profile.instance_profile.name
359339
associate_public_ip_address = var.associate_public_ip_address
360-
361-
root_block_device {
362-
volume_type = "gp2"
363-
volume_size = "10"
364-
delete_on_termination = true
365-
encrypted = true
366-
}
367-
368-
lifecycle {
369-
create_before_destroy = true
370-
}
371-
}
372-
373-
module "tags" {
374-
source = "snowplow-devops/tags/aws"
375-
version = "0.2.0"
376-
377-
tags = local.tags
378-
}
379-
380-
resource "aws_autoscaling_group" "asg" {
381-
name = var.name
382-
383-
max_size = var.max_size
384-
min_size = var.min_size
385-
386-
launch_configuration = aws_launch_configuration.lc.name
387-
388-
health_check_grace_period = 300
389-
health_check_type = "EC2"
390-
391-
vpc_zone_identifier = var.subnet_ids
392-
393-
instance_refresh {
394-
strategy = "Rolling"
395-
preferences {
396-
min_healthy_percentage = 90
397-
}
398-
triggers = ["tag"]
399-
}
400-
401-
tags = module.tags.asg_tags
340+
security_groups = [aws_security_group.sg.id]
341+
342+
min_size = var.min_size
343+
max_size = var.max_size
344+
subnet_ids = var.subnet_ids
345+
346+
enable_auto_scaling = var.enable_auto_scaling
347+
scale_up_cooldown_sec = var.scale_up_cooldown_sec
348+
scale_up_cpu_threshold_percentage = var.scale_up_cpu_threshold_percentage
349+
scale_up_eval_minutes = var.scale_up_eval_minutes
350+
scale_down_cooldown_sec = var.scale_down_cooldown_sec
351+
scale_down_cpu_threshold_percentage = var.scale_down_cpu_threshold_percentage
352+
scale_down_eval_minutes = var.scale_down_eval_minutes
402353
}

outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
output "asg_id" {
2-
value = aws_autoscaling_group.asg.id
2+
value = module.service.asg_id
33
description = "ID of the ASG"
44
}
55

66
output "asg_name" {
7-
value = aws_autoscaling_group.asg.name
7+
value = module.service.asg_name
88
description = "Name of the ASG"
99
}
1010

templates/user-data.sh.tmpl

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,5 @@
1-
#!/bin/bash
2-
set -e -x
3-
exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1
4-
5-
# -----------------------------------------------------------------------------
6-
# BASE INSTALL
7-
# -----------------------------------------------------------------------------
8-
91
readonly CONFIG_DIR=/opt/snowplow/config
102

11-
function install_docker_ce() {
12-
sudo yum install -y docker-20.10.4-1.amzn2
13-
sudo systemctl enable docker
14-
sudo systemctl start docker
15-
sudo usermod -a -G docker ec2-user
16-
}
17-
18-
function get_instance_id() {
19-
curl --silent --location "http://169.254.169.254/latest/meta-data/instance-id/"
20-
}
21-
22-
install_docker_ce
23-
243
sudo mkdir -p $${CONFIG_DIR}
254
sudo cat << EOF > $${CONFIG_DIR}/postgres_loader.json
265
${config}

variables.tf

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,50 @@ variable "java_opts" {
108108
type = string
109109
}
110110

111+
# --- Auto-scaling options
112+
113+
variable "enable_auto_scaling" {
114+
description = "Whether to enable auto-scaling policies for the service (WARN: ensure you have sufficient db_connections available for the max number of nodes in the ASG)"
115+
default = true
116+
type = bool
117+
}
118+
119+
variable "scale_up_cooldown_sec" {
120+
description = "Time (in seconds) until another scale-up action can occur"
121+
default = 180
122+
type = number
123+
}
124+
125+
variable "scale_up_cpu_threshold_percentage" {
126+
description = "The average CPU percentage that must be exceeded to scale-up"
127+
default = 60
128+
type = number
129+
}
130+
131+
variable "scale_up_eval_minutes" {
132+
description = "The number of consecutive minutes that the threshold must be breached to scale-up"
133+
default = 5
134+
type = number
135+
}
136+
137+
variable "scale_down_cooldown_sec" {
138+
description = "Time (in seconds) until another scale-down action can occur"
139+
default = 600
140+
type = number
141+
}
142+
143+
variable "scale_down_cpu_threshold_percentage" {
144+
description = "The average CPU percentage that we must be below to scale-down"
145+
default = 20
146+
type = number
147+
}
148+
149+
variable "scale_down_eval_minutes" {
150+
description = "The number of consecutive minutes that we must be below the threshold to scale-down"
151+
default = 60
152+
type = number
153+
}
154+
111155
# --- Configuration options
112156

113157
variable "in_stream_name" {

versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ terraform {
44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = ">= 3.45.0"
7+
version = ">= 3.72.0"
88
}
99
}
1010
}

0 commit comments

Comments
 (0)