Skip to content

Commit

Permalink
feat!: refactor module (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfroembgen authored Sep 27, 2023
1 parent a86ca71 commit 33510dd
Show file tree
Hide file tree
Showing 20 changed files with 104 additions and 408 deletions.
51 changes: 16 additions & 35 deletions README.md

Large diffs are not rendered by default.

19 changes: 0 additions & 19 deletions examples/all-labels/main.tf

This file was deleted.

Empty file removed examples/all-labels/outputs.tf
Empty file.
3 changes: 0 additions & 3 deletions examples/all-labels/providers.tf

This file was deleted.

Empty file removed examples/all-labels/variables.tf
Empty file.
10 changes: 0 additions & 10 deletions examples/all-labels/versions.tf

This file was deleted.

10 changes: 0 additions & 10 deletions examples/basic/main.tf

This file was deleted.

Empty file removed examples/basic/outputs.tf
Empty file.
3 changes: 0 additions & 3 deletions examples/basic/providers.tf

This file was deleted.

Empty file removed examples/basic/variables.tf
Empty file.
10 changes: 0 additions & 10 deletions examples/basic/versions.tf

This file was deleted.

9 changes: 0 additions & 9 deletions examples/minimal/main.tf

This file was deleted.

Empty file removed examples/minimal/outputs.tf
Empty file.
3 changes: 0 additions & 3 deletions examples/minimal/providers.tf

This file was deleted.

Empty file removed examples/minimal/variables.tf
Empty file.
10 changes: 0 additions & 10 deletions examples/minimal/versions.tf

This file was deleted.

246 changes: 38 additions & 208 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,227 +1,57 @@
locals {
has_additional_attributes = length(module.this.attributes) > 1
redis_container_name = "redis"
default_read_capacity = var.ddb_billing_mode == "PROVISIONED" ? var.ddb_read_capacity == null ? 1 : var.ddb_read_capacity : null
default_write_capacity = var.ddb_billing_mode == "PROVISIONED" ? var.ddb_write_capacity == null ? 1 : var.ddb_write_capacity : null
}

# can't just pull in the context here because the attributes add up instead of being replaced by the input
module "ddb_label" {
module "kvstore_label" {
source = "justtrackio/label/null"
version = "0.26.0"

enabled = module.this.enabled
namespace = module.this.namespace
tenant = module.this.tenant
environment = module.this.environment
stage = module.this.stage
name = module.this.name
delimiter = module.this.delimiter
tags = module.this.tags
additional_tag_map = module.this.additional_tag_map
label_order = var.label_orders.ddb
regex_replace_chars = module.this.regex_replace_chars
id_length_limit = module.this.id_length_limit
label_key_case = var.label_key_case
label_value_case = var.label_value_case
descriptor_formats = var.descriptor_formats
labels_as_tags = var.labels_as_tags
}

module "redis_label" {
count = var.redis_enabled ? 1 : 0
source = "justtrackio/label/null"
version = "0.26.0"

context = module.ddb_label.context
attributes = concat(["kvstore_${try(module.this.attributes[0], "")}"], local.has_additional_attributes ? slice(module.this.attributes, 1, length(module.this.attributes)) : [])
label_order = var.label_orders.redis
}

module "ddb" {
source = "justtrackio/dynamodb-table/aws"
version = "1.0.3"

context = module.ddb_label.context
attributes = concat(["kvstore-${try(module.this.attributes[0], "")}"], local.has_additional_attributes ? slice(module.this.attributes, 1, length(module.this.attributes)) : [])

billing_mode = var.ddb_billing_mode

autoscale_read_schedule = var.ddb_autoscale_read_schedule
autoscale_write_schedule = var.ddb_autoscale_write_schedule

hash_key = "key"

dynamodb_attributes = [{
name = "key"
type = "S"
}]

ttl_enabled = false

tags = {
Model = "kvstore_${try(module.this.attributes[0], "")}"
}
}

module "task_label" {
source = "cloudposse/label/null"
version = "0.25.0"
attributes = ["task"]
label_order = var.label_orders.iam
label_value_case = "none"
label_order = var.label_orders.kvstore
tenant = "kvstore"

context = module.this.context
}

data "aws_iam_policy_document" "ecs_task" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]

principals {
type = "Service"
identifiers = ["ecs-tasks.amazonaws.com"]
}
}
}

resource "aws_iam_role" "ecs_task" {
count = var.redis_enabled ? 1 : 0

name = module.task_label.id
assume_role_policy = data.aws_iam_policy_document.ecs_task.json
tags = module.task_label.tags
}

module "exec_label" {
source = "cloudposse/label/null"
version = "0.25.0"
attributes = ["exec"]
label_order = var.label_orders.iam

context = module.this.context
}

data "aws_iam_policy_document" "ecs_task_exec" {
statement {
actions = ["sts:AssumeRole"]

principals {
type = "Service"
identifiers = ["ecs-tasks.amazonaws.com"]
}
}
}

resource "aws_iam_role" "ecs_exec" {
count = var.redis_enabled ? 1 : 0

name = module.exec_label.id
assume_role_policy = data.aws_iam_policy_document.ecs_task_exec.json
tags = module.exec_label.tags
}

data "aws_iam_policy_document" "ecs_exec" {
statement {
effect = "Allow"
resources = ["*"]

actions = [
"ssm:GetParameters",
"ecr:GetAuthorizationToken",
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage",
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
}
}

resource "aws_iam_role_policy" "ecs_exec" {
count = var.redis_enabled ? 1 : 0

name = module.exec_label.id
policy = data.aws_iam_policy_document.ecs_exec.json
role = aws_iam_role.ecs_exec[0].id
}

module "container_definition" {
count = var.redis_enabled ? 1 : 0
source = "cloudposse/ecs-container-definition/aws"
version = "0.58.1"

container_image = "${var.redis_image_repository}:${var.redis_image_tag}"
container_name = local.redis_container_name
container_cpu = var.redis_cpu_size
container_memory_reservation = var.redis_service_memory_size

port_mappings = [
module "ddb" {
count = var.ddb_enabled ? 1 : 0
source = "terraform-aws-modules/dynamodb-table/aws"
version = "3.2.0"

name = module.kvstore_label.id
hash_key = "key"
tags = module.kvstore_label.tags
autoscaling_enabled = var.ddb_autoscaling_enabled
autoscaling_read = var.ddb_autoscaling_read
autoscaling_write = var.ddb_autoscaling_write
billing_mode = var.ddb_billing_mode
read_capacity = local.default_read_capacity
write_capacity = local.default_write_capacity

attributes = [
{
containerPort = 6379
hostPort = 0
protocol = "tcp"
},
]

command = [
"--maxmemory ${var.redis_memory_size}mb",
"--maxmemory-policy allkeys-lru"
name = "key"
type = "S"
}
]
}

module "redis" {
count = var.redis_enabled ? 1 : 0
source = "cloudposse/ecs-alb-service-task/aws"
version = "0.66.4"

context = module.redis_label[0].context

container_definition_json = "[${sensitive(module.container_definition[0].json_map_encoded)}]"
source = "justtrackio/ecs-redis/aws"
version = "2.1.0"

context = module.kvstore_label.context
label_orders = var.label_orders

redis_maxmemory = var.redis_maxmemory
redis_maxmemory_policy = var.redis_maxmemory_policy
container_cpu = var.redis_cpu_size
container_memory_reservation = var.redis_memory_size
container_image_repository = var.redis_image_repository
container_image_tag = var.redis_image_tag
deployment_maximum_percent = var.redis_deployment_maximum_percent
deployment_minimum_healthy_percent = var.redis_deployment_minimum_healthy_percent
desired_count = var.redis_desired_count
ecs_cluster_arn = var.redis_ecs_cluster_arn
launch_type = var.redis_launch_type
name = "${var.name}${module.this.delimiter}redis"
network_mode = var.redis_network_mode
propagate_tags = var.redis_propagate_tags
vpc_id = var.redis_vpc_id
task_role_arn = [aws_iam_role.ecs_task[0].arn]
task_exec_role_arn = [aws_iam_role.ecs_exec[0].arn]

service_registries = [{
registry_arn = aws_service_discovery_service.this[0].arn
container_name = local.redis_container_name
container_port = 6379
}]

tags = {
Model = "kvstore_${try(module.this.attributes[0], "")}"
ApplicationType = "redis"
}

service_placement_constraints = var.redis_service_placement_constraints != null ? var.redis_service_placement_constraints : module.this.environment == "prod" ? [{
type = "memberOf"
expression = "attribute:spotinst.io/container-instance-lifecycle==od"
}] : []
}

resource "aws_service_discovery_service" "this" {
count = var.redis_enabled ? 1 : 0
name = var.redis_service_discovery_name

dns_config {
namespace_id = var.redis_service_discovery_dns_namespace_id

dns_records {
ttl = 60
type = "SRV"
}
}

health_check_custom_config {
failure_threshold = 1
}

tags = module.this.tags
service_discovery_name = "${module.kvstore_label.tenant}-${module.kvstore_label.name}.${module.kvstore_label.stage}"
}
2 changes: 1 addition & 1 deletion outputs.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
output "redis_service_discovery_service_arn" {
description = "ARN of the aws_service_discovery_service created for the redis service"
value = try(aws_service_discovery_service.this[0].arn, "")
value = try(module.redis[0].service_discovery_service_arn)
}
Loading

0 comments on commit 33510dd

Please sign in to comment.