Using aws ecs with servicediscovery
TypeScriptWhen integrating AWS ECS with Service Discovery, you're essentially enabling your ECS services to discover and interact with each other automatically using AWS Cloud Map. AWS Cloud Map is a fully managed service that you can use to create and manage custom namespaces, which then allows your services to discover each other throughout AWS.
Here's how you can go about it with Pulumi:
- First, you will create a Service Discovery namespace, which acts as a container for service instances.
- Then, you create a Service Discovery service, which defines how instances of the service can be discovered.
- After that, when you define an ECS service, you include the Service Discovery service in its configuration. This way, when the ECS service runs tasks, they are automatically registered with the discovery service.
Below is a basic TypeScript program using Pulumi to set up an ECS cluster, define a task definition for your container, create a Service Discovery namespace and service, and finally deploy an ECS service that utilizes the Service Discovery service.
import * as pulumi from "@pulumi/pulumi"; import * as aws from "@pulumi/aws"; // Create an ECS Cluster. const cluster = new aws.ecs.Cluster("my-cluster", {}); // Define an ECS Task Definition. const taskDef = new aws.ecs.TaskDefinition("my-taskdef", { family: "service", cpu: "256", memory: "512", networkMode: "awsvpc", requiresCompatibilities: ["FARGATE"], executionRoleArn: aws.iam.Role.get("ecsTaskExecutionRole", "ecsTaskExecutionRole").arn, containerDefinitions: JSON.stringify([{ name: "my-container", image: "my-repo/my-image:my-tag", portMappings: [{ containerPort: 80, hostPort: 80, protocol: "tcp", }], }]), }); // Create a Service Discovery HTTP Namespace. const httpNamespace = new aws.servicediscovery.HttpNamespace("my-http-namespace", { description: "My HTTP Namespace for service discovery", name: "myservicediscovery", }); // Once namespace is created, create a Service Discovery Service. const serviceDiscoveryService = new aws.servicediscovery.Service("my-sds", { name: "MyServiceDiscoveryService", dnsConfig: { namespaceId: httpNamespace.id, dnsRecords: [{ ttl: 10, type: "A", }], }, healthCheckCustomConfig: { failureThreshold: 1 }, httpNamespaceId: httpNamespace.id, }); // Create an ECS Service with the Service Discovery Service integration. const ecsService = new aws.ecs.Service("my-ecs-service", { cluster: cluster.id, launchType: "FARGATE", taskDefinition: taskDef.arn, desiredCount: 1, networkConfiguration: { awsvpcConfiguration: { subnets: [aws.ec2.Subnet.get("subnet", "subnet-0bb1c79de3EXAMPLE").id], assignPublicIp: "ENABLED", securityGroups: [aws.ec2.SecurityGroup.get("sg", "sg-0e123456789EXAMPLE").id], }, }, // Attach the Service Discovery Service to the ECS Service. serviceRegistries: { registryArn: serviceDiscoveryService.arn, port: 80, containerName: "my-container", containerPort: 80, }, }); // Export the DNS name of the Service Discovery service. export const serviceDiscoveryDnsName = serviceDiscoveryService.name.apply(name => `${name}.myservicediscovery`);
What this Pulumi program does:
- An ECS cluster
my-cluster
is created to run your container services. - A
taskDef
defines the specification for how your container will run, including CPU and memory allocation, container image location, and port mappings. httpNamespace
creates a new HTTP namespace with the Service Discovery service. You can also create a private DNS namespace if your service only needs to be discoverable within the AWS Cloud.serviceDiscoveryService
is the Service Discovery service that utilizes the created namespace. It allows the tasks within your ECS service to be discoverable by other services within the namespace.ecsService
is your ECS service definition, including the network configuration that specifies VPC settings.- Under
serviceRegistries
, you connect the ECS service with the Service Discovery service, so that tasks are automatically registered with the discovery service when they start. - The DNS name of the Service Discovery service is exported. You may use DNS queries within your network to resolve this name to running instances of your service.
Using Service Discovery simplifies service-to-service communication and enables you to scale and update your backend services without worrying about keeping a static record of your service endpoints.