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

feat: add API ECS resources #65

Merged
merged 5 commits into from
Apr 18, 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
52 changes: 52 additions & 0 deletions infra/terraform/environments/dev/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
locals {
service_names = ["api", "selfserve", "internal"]

legacy_service_names = ["API", "IUWEB", "SSWEB"]
}

data "aws_ecr_repository" "this" {
for_each = toset(local.service_names)

name = "vol-app/${each.key}"
}

data "aws_security_group" "this" {
for_each = toset(local.legacy_service_names)

name = "DEV/APP/DA-OLCS-PRI-${each.key}-SG"
}

data "aws_subnets" "this" {
for_each = toset(local.legacy_service_names)

filter {
name = "tag:Name"
values = [
"DEV/APP/DA-OLCS-PRI-${each.key}-1A",
"DEV/APP/DA-OLCS-PRI-${each.key}-1B",
"DEV/APP/DA-OLCS-PRI-${each.key}-1C"
]
}
}

module "service" {
source = "../../modules/service"

environment = "dev"

services = {
"api" = {
cpu = 1024
memory = 4096

image = "${data.aws_ecr_repository.this["api"].repository_url}:${var.api_image_tag}"
rahul-dvsa marked this conversation as resolved.
Show resolved Hide resolved

subnet_ids = data.aws_subnets.this["API"].ids

security_group_ids = [
data.aws_security_group.this["API"].id
]
}
}
}

5 changes: 5 additions & 0 deletions infra/terraform/environments/dev/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
variable "api_image_tag" {
type = string
description = "The tag of the API image to deploy"
default = "latest"
}
10 changes: 8 additions & 2 deletions infra/terraform/modules/service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ No providers.

## Modules

No modules.
| Name | Source | Version |
|------|--------|---------|
| <a name="module_ecs_cluster"></a> [ecs\_cluster](#module\_ecs\_cluster) | terraform-aws-modules/ecs/aws//modules/cluster | ~> 5.10 |
| <a name="module_ecs_service"></a> [ecs\_service](#module\_ecs\_service) | terraform-aws-modules/ecs/aws//modules/service | ~> 5.10 |

## Resources

No resources.

## Inputs

No inputs.
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_environment"></a> [environment](#input\_environment) | The environment to deploy to | `string` | n/a | yes |
| <a name="input_services"></a> [services](#input\_services) | The services to deploy | <pre>map(object({<br> image = string<br> cpu = number<br> memory = number<br> security_group_ids = list(string)<br> subnet_ids = list(string)<br> }))</pre> | `{}` | no |

## Outputs

Expand Down
63 changes: 63 additions & 0 deletions infra/terraform/modules/service/ecs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module "ecs_cluster" {
for_each = var.services

source = "terraform-aws-modules/ecs/aws//modules/cluster"
version = "~> 5.10"

cluster_name = "vol-app-${var.environment}-${each.key}-cluster"

create_task_exec_iam_role = true

cluster_settings = [
{
"name" : "containerInsights",
"value" : "enabled"
}
]
}

module "ecs_service" {
for_each = var.services

source = "terraform-aws-modules/ecs/aws//modules/service"
version = "~> 5.10"

name = "vol-app-${var.environment}-${each.key}-service"
cluster_arn = module.ecs_cluster[each.key].arn

task_exec_iam_role_arn = module.ecs_cluster[each.key].task_exec_iam_role_arn

cpu = var.services[each.key].cpu
memory = var.services[each.key].memory

container_definitions = {
(each.key) = {
cpu = try(var.services[each.key].task_cpu_limit, var.services[each.key].cpu / 2)
rahul-dvsa marked this conversation as resolved.
Show resolved Hide resolved
memory = try(var.services[each.key].task_memory_limit, var.services[each.key].memory / 4)
essential = true
image = var.services[each.key].image
port_mappings = [
{
name = "http"
containerPort = 80
protocol = "tcp"
}
]

environment = [
{
name = "ENVIRONMENT_NAME"
value = var.environment
}
]

readonly_root_filesystem = false

memory_reservation = 100
}
}

create_security_group = false
security_group_ids = var.services[each.key].security_group_ids
subnet_ids = var.services[each.key].subnet_ids
}
16 changes: 16 additions & 0 deletions infra/terraform/modules/service/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
variable "environment" {
type = string
description = "The environment to deploy to"
}

variable "services" {
type = map(object({
image = string
cpu = number
memory = number
security_group_ids = list(string)
subnet_ids = list(string)
}))
description = "The services to deploy"
default = {}
}
Loading