-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add terraform infra -- ECS, networking, logs (#34)
- Loading branch information
1 parent
8b7b9c5
commit 70af050
Showing
10 changed files
with
220 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import express, {Request, Response} from "express"; | ||
import {ErrorResponse} from "@types"; | ||
import {z} from "zod"; | ||
|
||
export const HealthResponse = z.object({ | ||
status: z.string(), | ||
}); | ||
|
||
export type HealthResponse = z.infer< | ||
typeof HealthResponse | ||
>; | ||
|
||
const router = express.Router(); | ||
|
||
router.get( | ||
"/status", | ||
async ( | ||
req: Request<{}, {}, null>, | ||
res: Response<HealthResponse | ErrorResponse> | ||
) => { | ||
return res.status(200).json({ | ||
status: "live" | ||
}); | ||
} | ||
); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
module "cloudwatch_logs" { | ||
source = "cloudposse/cloudwatch-logs/aws" | ||
version = "0.6.6" | ||
|
||
namespace = var.namespace | ||
# stage = var.stage | ||
name = var.name | ||
|
||
retention_in_days = 7 | ||
} | ||
|
||
module "container_definition" { | ||
source = "cloudposse/ecs-container-definition/aws" | ||
version = "0.58.1" | ||
|
||
# container_name = "${var.namespace}-${var.stage}-${var.name}" | ||
container_name = "${var.namespace}-${var.name}" | ||
container_image = "${module.ecr.repository_url}:${var.image_tag}" | ||
container_memory = 512 # optional for FARGATE launch type | ||
container_cpu = 256 # optional for FARGATE launch type | ||
essential = true | ||
port_mappings = var.container_port_mappings | ||
|
||
# The environment variables to pass to the container. | ||
#environment = [ | ||
# { | ||
# name = "ENV_NAME" | ||
# value = "ENV_VALUE" | ||
# }, | ||
#] | ||
|
||
# Pull secrets from AWS Parameter Store. | ||
# "name" is the name of the env var. | ||
# "valueFrom" is the name of the secret in PS. | ||
secrets = [ | ||
# { | ||
# name = "SECRET_ENV_NAME" | ||
# valueFrom = "SECRET_ENV_NAME" | ||
# }, | ||
] | ||
|
||
log_configuration = { | ||
logDriver = "awslogs" | ||
options = { | ||
"awslogs-region" = var.region | ||
"awslogs-group" = module.cloudwatch_logs.log_group_name | ||
"awslogs-stream-prefix" = var.name | ||
} | ||
secretOptions = null | ||
} | ||
} | ||
|
||
resource "aws_ecs_cluster" "ecs_cluster" { | ||
# name = "${var.namespace}-${var.stage}-${var.name}" | ||
name = "${var.namespace}-${var.name}" | ||
tags = { | ||
Namespace = var.namespace | ||
# Stage = var.stage | ||
Name = var.name | ||
} | ||
} | ||
|
||
module "ecs_alb_service_task" { | ||
source = "cloudposse/ecs-alb-service-task/aws" | ||
version = "0.66.4" | ||
|
||
namespace = var.namespace | ||
# stage = var.stage | ||
name = var.name | ||
|
||
use_alb_security_group = true | ||
alb_security_group = module.alb.security_group_id | ||
container_definition_json = module.container_definition.json_map_encoded_list | ||
ecs_cluster_arn = aws_ecs_cluster.ecs_cluster.arn | ||
launch_type = "FARGATE" | ||
vpc_id = module.vpc.vpc_id | ||
security_group_ids = [module.vpc.vpc_default_security_group_id] | ||
subnet_ids = module.subnets.private_subnet_ids # change to "module.subnets.public_subnet_ids" if "nat_gateway_enabled" is false | ||
ignore_changes_task_definition = false | ||
network_mode = "awsvpc" | ||
assign_public_ip = false # change to true if "nat_gateway_enabled" is false | ||
propagate_tags = "TASK_DEFINITION" | ||
desired_count = var.desired_count | ||
task_memory = 512 | ||
task_cpu = 256 | ||
force_new_deployment = true | ||
container_port = var.container_port_mappings[0].containerPort | ||
|
||
ecs_load_balancers = [{ | ||
# container_name = "${var.namespace}-${var.stage}-${var.name}" | ||
container_name = "${var.namespace}-${var.name}" | ||
container_port = var.container_port_mappings[0].containerPort | ||
elb_name = "" | ||
target_group_arn = module.alb.default_target_group_arn | ||
}] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
module "vpc" { | ||
source = "cloudposse/vpc/aws" | ||
version = "2.0.0" | ||
|
||
namespace = var.namespace | ||
# stage = var.stage | ||
name = var.name | ||
|
||
ipv4_primary_cidr_block = "10.0.0.0/16" | ||
} | ||
|
||
module "subnets" { | ||
source = "cloudposse/dynamic-subnets/aws" | ||
version = "2.0.4" | ||
|
||
namespace = var.namespace | ||
# stage = var.stage | ||
name = var.name | ||
|
||
availability_zones = ["ap-southeast-1a", "ap-southeast-1b", "ap-southeast-1c"] # change to your AZs | ||
vpc_id = module.vpc.vpc_id | ||
igw_id = [module.vpc.igw_id] | ||
ipv4_cidr_block = [module.vpc.vpc_cidr_block] | ||
nat_gateway_enabled = true | ||
max_nats = 1 | ||
} | ||
|
||
module "alb" { | ||
source = "cloudposse/alb/aws" | ||
version = "1.7.0" | ||
|
||
namespace = var.namespace | ||
# stage = var.stage | ||
name = var.name | ||
|
||
access_logs_enabled = false | ||
vpc_id = module.vpc.vpc_id | ||
ip_address_type = "ipv4" | ||
subnet_ids = module.subnets.public_subnet_ids | ||
security_group_ids = [module.vpc.vpc_default_security_group_id] | ||
# https_enabled = true | ||
# certificate_arn = aws_acm_certificate.cert.arn | ||
# http_redirect = true | ||
health_check_interval = 60 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,14 @@ | ||
output "github_actions_role_arn" { | ||
description = "The ARN of the role to be assumed by the GitHub Actions" | ||
value = aws_iam_role.github_actions_role.arn | ||
} | ||
|
||
output "alb_dns_name" { | ||
description = "DNS name of ALB" | ||
value = module.alb.alb_dns_name | ||
} | ||
|
||
output "ecr_repository_name" { | ||
description = "The name of the ECR Repository" | ||
value = module.ecr.repository_name | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters