full working example!
This commit is contained in:
parent
7e525170a1
commit
64c4ec9ef3
5 changed files with 7585 additions and 16 deletions
|
@ -19,3 +19,20 @@ provider "registry.opentofu.org/hashicorp/aws" {
|
||||||
"zh:e7b54a0faecd34a9c73729d1d1f0cfc1b8f56bae789f95987002616f1265ce72",
|
"zh:e7b54a0faecd34a9c73729d1d1f0cfc1b8f56bae789f95987002616f1265ce72",
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
provider "registry.opentofu.org/hashicorp/null" {
|
||||||
|
version = "3.2.2"
|
||||||
|
hashes = [
|
||||||
|
"h1:xN1tSeF/rUBfaddk/AVqk4i65z/MMM9uVZWd2cWCCH0=",
|
||||||
|
"zh:00e5877d19fb1c1d8c4b3536334a46a5c86f57146fd115c7b7b4b5d2bf2de86d",
|
||||||
|
"zh:1755c2999e73e4d73f9de670c145c9a0dc5a373802799dff06a0e9c161354163",
|
||||||
|
"zh:2b29d706353bc9c4edda6a2946af3322abe94372ffb421d81fa176f1e57e33be",
|
||||||
|
"zh:34f65259c6d2bd51582b6da536e782b181b23725782b181193b965f519fbbacd",
|
||||||
|
"zh:370f6eb744475926a1fa7464d82d46ad83c2e1148b4b21681b4cec4d75b97969",
|
||||||
|
"zh:5950bdb23b4fcc6431562d7eba3dea37844aa4220c4da2eb898ae3e4d1b64ec4",
|
||||||
|
"zh:8f3d5c8d4b9d497fec36953a227f80c76d37fc8431b683a23fb1c42b9cccbf8a",
|
||||||
|
"zh:8f6eb5e65c047bf490ad3891efecefc488503b65898d4ee106f474697ba257d7",
|
||||||
|
"zh:a7040eed688316fe00379574c72bb8c47dbe2638b038bb705647cbf224de8f72",
|
||||||
|
"zh:e561f28df04d9e51b75f33004b7767a53c45ad96e3375d86181ba1363bffbc77",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
|
@ -34,7 +34,7 @@ module "ecs_service" {
|
||||||
|
|
||||||
name = local.name
|
name = local.name
|
||||||
cluster_arn = module.ecs_cluster.arn
|
cluster_arn = module.ecs_cluster.arn
|
||||||
|
desired_count = 3
|
||||||
cpu = 1024
|
cpu = 1024
|
||||||
memory = 4096
|
memory = 4096
|
||||||
|
|
||||||
|
@ -44,20 +44,6 @@ module "ecs_service" {
|
||||||
# Container definition(s)
|
# Container definition(s)
|
||||||
container_definitions = {
|
container_definitions = {
|
||||||
|
|
||||||
valkey = {
|
|
||||||
cpu = 512
|
|
||||||
memory = 1024
|
|
||||||
image = "valkey/valkey:7.2.4-rc1-alpine"
|
|
||||||
port_mappings = [
|
|
||||||
{
|
|
||||||
name = "valkey"
|
|
||||||
containerPort = 6379
|
|
||||||
hostPort = 6379
|
|
||||||
protocol = "tcp"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
(local.container_name) = {
|
(local.container_name) = {
|
||||||
cpu = 512
|
cpu = 512
|
||||||
memory = 1024
|
memory = 1024
|
||||||
|
@ -74,7 +60,7 @@ module "ecs_service" {
|
||||||
environment = [
|
environment = [
|
||||||
{
|
{
|
||||||
name = "REDIS_ENDPOINT"
|
name = "REDIS_ENDPOINT"
|
||||||
value = "127.0.0.1"
|
value = "valkey"
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -82,6 +68,18 @@ module "ecs_service" {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
service_connect_configuration = {
|
||||||
|
namespace = aws_service_discovery_http_namespace.this.arn
|
||||||
|
service = {
|
||||||
|
client_alias = {
|
||||||
|
port = local.container_port
|
||||||
|
dns_name = local.container_name
|
||||||
|
}
|
||||||
|
port_name = local.container_name
|
||||||
|
discovery_name = local.container_name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
load_balancer = {
|
load_balancer = {
|
||||||
service = {
|
service = {
|
||||||
target_group_arn = module.alb.target_groups["ex_ecs"].arn
|
target_group_arn = module.alb.target_groups["ex_ecs"].arn
|
||||||
|
@ -116,6 +114,112 @@ module "ecs_service" {
|
||||||
tags = local.tags
|
tags = local.tags
|
||||||
}
|
}
|
||||||
|
|
||||||
|
resource "aws_ecs_task_definition" "task" {
|
||||||
|
family = "service"
|
||||||
|
network_mode = "awsvpc"
|
||||||
|
requires_compatibilities = ["FARGATE", "EC2"]
|
||||||
|
cpu = 512
|
||||||
|
memory = 1024
|
||||||
|
container_definitions = jsonencode([
|
||||||
|
{
|
||||||
|
name = "valkey"
|
||||||
|
image = "valkey/valkey:7.2.4-rc1-alpine"
|
||||||
|
cpu = 512
|
||||||
|
memory = 1024
|
||||||
|
essential = true # if true and if fails, all other containers fail. Must have at least one essential
|
||||||
|
portMappings = [
|
||||||
|
{
|
||||||
|
name = "valkey"
|
||||||
|
containerPort = 6379
|
||||||
|
hostPort = 6379
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_ecs_service" "service" {
|
||||||
|
name = "valkey"
|
||||||
|
cluster = module.ecs_cluster.id
|
||||||
|
task_definition = aws_ecs_task_definition.task.id
|
||||||
|
desired_count = 1
|
||||||
|
launch_type = "FARGATE"
|
||||||
|
platform_version = "LATEST"
|
||||||
|
|
||||||
|
network_configuration {
|
||||||
|
assign_public_ip = false
|
||||||
|
security_groups = [aws_security_group.sg.id]
|
||||||
|
subnets = module.vpc.private_subnets
|
||||||
|
}
|
||||||
|
lifecycle {
|
||||||
|
ignore_changes = [task_definition]
|
||||||
|
}
|
||||||
|
|
||||||
|
service_connect_configuration {
|
||||||
|
enabled = true
|
||||||
|
namespace = "chat-app-demo"
|
||||||
|
service {
|
||||||
|
discovery_name = "valkey"
|
||||||
|
port_name = "valkey"
|
||||||
|
client_alias {
|
||||||
|
dns_name = "valkey"
|
||||||
|
port = 6379
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
resource "aws_security_group" "sg" {
|
||||||
|
name = "ecs"
|
||||||
|
vpc_id = module.vpc.vpc_id
|
||||||
|
|
||||||
|
egress {
|
||||||
|
from_port = 0
|
||||||
|
to_port = 0
|
||||||
|
protocol = "-1"
|
||||||
|
cidr_blocks = ["0.0.0.0/0"]
|
||||||
|
}
|
||||||
|
|
||||||
|
ingress {
|
||||||
|
protocol = -1
|
||||||
|
self = true
|
||||||
|
from_port = 0
|
||||||
|
to_port = 0
|
||||||
|
description = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
ingress {
|
||||||
|
from_port = 6379
|
||||||
|
to_port = 6379
|
||||||
|
protocol = "tcp"
|
||||||
|
self = "false"
|
||||||
|
cidr_blocks = ["0.0.0.0/0"]
|
||||||
|
description = "Port 80"
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "null_resource" "update_desired_count" {
|
||||||
|
triggers = {
|
||||||
|
# Changes to this value will trigger the API call execution below
|
||||||
|
desired_count = 3
|
||||||
|
}
|
||||||
|
|
||||||
|
provisioner "local-exec" {
|
||||||
|
interpreter = ["/bin/bash", "-c"]
|
||||||
|
|
||||||
|
# Note: this requires the awscli to be installed locally where Terraform is executed
|
||||||
|
command = <<-EOT
|
||||||
|
aws ecs update-service \
|
||||||
|
--cluster ${module.ecs_cluster.name} \
|
||||||
|
--service ${module.ecs_service.name} \
|
||||||
|
--desired-count ${null_resource.update_desired_count.triggers.desired_count}
|
||||||
|
EOT
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
# Supporting Resources
|
# Supporting Resources
|
||||||
################################################################################
|
################################################################################
|
||||||
|
|
11
2-simple-example/iac/route53.tf
Normal file
11
2-simple-example/iac/route53.tf
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
resource "aws_route53_record" "chat" {
|
||||||
|
zone_id = "Z2TWGHEC8YQMWW"
|
||||||
|
name = "chat.ngd.io"
|
||||||
|
type = "A"
|
||||||
|
|
||||||
|
alias {
|
||||||
|
name = module.alb.dns_name
|
||||||
|
zone_id = module.alb.zone_id
|
||||||
|
evaluate_target_health = true
|
||||||
|
}
|
||||||
|
}
|
3698
2-simple-example/iac/terraform.tfstate
Normal file
3698
2-simple-example/iac/terraform.tfstate
Normal file
File diff suppressed because it is too large
Load diff
3739
2-simple-example/iac/terraform.tfstate.backup
Normal file
3739
2-simple-example/iac/terraform.tfstate.backup
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue