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 ECS discovery #14

Merged
merged 2 commits into from
Aug 27, 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
89 changes: 89 additions & 0 deletions examples/ecs-service-discovery/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added examples/ecs-service-discovery/README.cover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 48 additions & 0 deletions examples/ecs-service-discovery/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Service Discovery with ECS

![Cover](README.cover.png)

Service name, port name and port together form a unique discovery endpoint:
```
service_name-port_name:port
```

<!-- BEGIN_TF_DOCS -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | ~> 1.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | ~> 4.0 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | 4.67.0 |
| <a name="provider_random"></a> [random](#provider\_random) | 3.6.2 |

## Modules

| Name | Source | Version |
|------|--------|---------|
| <a name="module_cluster"></a> [cluster](#module\_cluster) | ../../modules/ecs-cluster | n/a |
| <a name="module_lb"></a> [lb](#module\_lb) | ../../modules/lb/alb | n/a |
| <a name="module_public_subnets"></a> [public\_subnets](#module\_public\_subnets) | ../../modules/vpc-public-subnet | n/a |
| <a name="module_service"></a> [service](#module\_service) | ../../modules/ecs-service | n/a |
| <a name="module_vpc"></a> [vpc](#module\_vpc) | ../../modules/vpc | n/a |

## Resources

| Name | Type |
|------|------|
| [aws_alb_listener.http](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/alb_listener) | resource |
| [aws_service_discovery_http_namespace.local](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/service_discovery_http_namespace) | resource |
| [random_id.example](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/id) | resource |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_lb_dns"></a> [lb\_dns](#output\_lb\_dns) | n/a |
<!-- END_TF_DOCS -->
133 changes: 133 additions & 0 deletions examples/ecs-service-discovery/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
locals {
context = {
namespace = "selleo"
stage = "dev"
name = "example"
}
}

resource "random_id" "example" {
byte_length = 2

prefix = "discovery-"
}

# network

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

name = random_id.example.hex
cidr = "10.0.0.0/16"
}

module "public_subnets" {
source = "../../modules/vpc-public-subnet"

vpc_id = module.vpc.id
internet_gateway_id = module.vpc.internet_gateway_id
config = {
"a" = {
az = "eu-central-1a"
cidr = "10.0.1.0/24"
nat = false
}
"b" = {
az = "eu-central-1b"
cidr = "10.0.2.0/24"
nat = false
}
}
}

module "lb" {
source = "../../modules/lb/alb"

name = random_id.example.hex
vpc_id = module.vpc.id
subnet_ids = module.public_subnets.ids
force_https = false

context = local.context
}

resource "aws_service_discovery_http_namespace" "local" {
name = "local"
description = "Service discovery namespace for ECS services"

tags = {
"context.namespace" = "selleo"
"context.stage" = "dev"
"context.name" = "app"
}
}

# cluster

module "cluster" {
source = "../../modules/ecs-cluster"

context = {
namespace = "selleo"
stage = "dev"
name = "example"
}

name_prefix = random_id.example.hex
vpc_id = module.vpc.id
subnet_ids = module.public_subnets.ids
instance_type = "t3.nano"
lb_security_group_id = module.lb.security_group_id

autoscaling_group = {
min_size = 1
max_size = 5
desired_capacity = 1
}
}

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

context = {
namespace = "selleo"
stage = "dev"
name = "example"
}

name = random_id.example.hex
vpc_id = module.vpc.id
subnet_ids = module.public_subnets.ids
cluster_id = module.cluster.id
desired_count = 1

tcp_ports = [
{
name = "http"
host = 0
container = 3000
}
]

service_discovery = {
arn = aws_service_discovery_http_namespace.local.arn
name = "app"
}
}

resource "aws_alb_listener" "http" {
load_balancer_arn = module.lb.id
port = 80
protocol = "HTTP"

default_action {
target_group_arn = module.service.lb_target_group_id
type = "forward"
}
}

# outputs

output "lb_dns" {
value = "http://${module.lb.dns_name}"
}
15 changes: 15 additions & 0 deletions examples/ecs-service-discovery/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
terraform {
required_version = "~> 1.0"

required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
}

provider "aws" {
region = "eu-central-1"
}

3 changes: 2 additions & 1 deletion modules/ecs-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@
| <a name="input_log_retention_in_days"></a> [log\_retention\_in\_days](#input\_log\_retention\_in\_days) | Log retention in days for Cloudwatch. | `string` | `60` | no |
| <a name="input_name"></a> [name](#input\_name) | ECS Service name. | `string` | n/a | yes |
| <a name="input_one_off_commands"></a> [one\_off\_commands](#input\_one\_off\_commands) | Set of commands that the tasks are created for. | `set(string)` | `[]` | no |
| <a name="input_port"></a> [port](#input\_port) | Container port | `number` | `null` | no |
| <a name="input_secrets"></a> [secrets](#input\_secrets) | Paths to secret. All secrets are read under the path. | `set(string)` | `[]` | no |
| <a name="input_service_discovery"></a> [service\_discovery](#input\_service\_discovery) | Service discovery configuration. | <pre>object({<br> arn = string<br> name = string<br> })</pre> | `null` | no |
| <a name="input_subnet_ids"></a> [subnet\_ids](#input\_subnet\_ids) | List of AWS subent IDs for service. | `list(string)` | n/a | yes |
| <a name="input_tags"></a> [tags](#input\_tags) | Additional tags attached to resources. | `map(string)` | `{}` | no |
| <a name="input_tcp_ports"></a> [tcp\_ports](#input\_tcp\_ports) | Port mapping. Use 0 for dynamic host mapping. Fargate requires ports to be the same. | <pre>list(<br> object({<br> name = string<br> host = number<br> container = number<br> })<br> )</pre> | `[]` | no |
| <a name="input_vpc_id"></a> [vpc\_id](#input\_vpc\_id) | VPC id. | `string` | n/a | yes |

## Outputs
Expand Down
Loading