Skip to content

Instantly share code, notes, and snippets.

@gnumilanix
Last active February 23, 2023 03:32
Show Gist options
  • Save gnumilanix/21bd0febfce367b16c2c8e6c998e7a5a to your computer and use it in GitHub Desktop.
Save gnumilanix/21bd0febfce367b16c2c8e6c998e7a5a to your computer and use it in GitHub Desktop.
Service discovery with AWS Cloud Map

This gist provides a sample terraform configuration to use service discovery for ecs service. It is useful when configuring a service that is only accessible internally without having to configure ELB and Route 53.

  1. Create service discovery namespace
  2. Create service discovery configuration A for service B
  3. Create ECS service B to resiger with discovery configuration A
  4. Access service B using dns name

This will automatically register new ECS tasks with Cloud Map when scaling in/out

spring:
...
jpa:
properties:
hibernate:
search:
backend:
uris: http://elasticsearch.prod:9200
resource "aws_service_discovery_private_dns_namespace" "service_discovery" {
name = "prod"
vpc = aws_vpc.default.id
}
resource "aws_service_discovery_service" "elasticsearch" {
name = "elasticsearch"
dns_config {
dns_records {
ttl = 60
type = "A"
}
namespace_id = aws_service_discovery_private_dns_namespace.service_discovery.id
routing_policy = "MULTIVALUE"
}
health_check_custom_config {
failure_threshold = 1
}
}
resource "aws_ecs_service" "elasticsearch" {
name = "elasticsearch"
..
service_registries {
registry_arn = aws_service_discovery_service.elasticsearch.arn
}
..
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment