Skip navigation links

Package software.amazon.awscdk.services.servicediscovery

Amazon ECS Service Discovery Construct Library

See: Description

Package software.amazon.awscdk.services.servicediscovery Description

Amazon ECS Service Discovery Construct Library

---

cfn-resources: Stable

cdk-constructs: Stable


This module is part of the AWS Cloud Development Kit project.

This package contains constructs for working with AWS Cloud Map

AWS Cloud Map is a fully managed service that you can use to create and maintain a map of the backend services and resources that your applications depend on.

For further information on AWS Cloud Map, see the AWS Cloud Map documentation

HTTP Namespace Example

The following example creates an AWS Cloud Map namespace that supports API calls, creates a service in that namespace, and registers an instance to it:

 import software.amazon.awscdk.core.*;
 import software.amazon.awscdk.services.servicediscovery.*;
 
 App app = new App();
 Stack stack = new Stack(app, "aws-servicediscovery-integ");
 
 HttpNamespace namespace = HttpNamespace.Builder.create(stack, "MyNamespace")
         .name("covfefe")
         .build();
 
 Service service1 = namespace.createService("NonIpService", BaseServiceProps.builder()
         .description("service registering non-ip instances")
         .build());
 
 service1.registerNonIpInstance("NonIpInstance", NonIpInstanceBaseProps.builder()
         .customAttributes(Map.of("arn", "arn:aws:s3:::mybucket"))
         .build());
 
 Service service2 = namespace.createService("IpService", BaseServiceProps.builder()
         .description("service registering ip instances")
         .healthCheck(HealthCheckConfig.builder()
                 .type(HealthCheckType.HTTP)
                 .resourcePath("/check")
                 .build())
         .build());
 
 service2.registerIpInstance("IpInstance", IpInstanceBaseProps.builder()
         .ipv4("54.239.25.192")
         .build());
 
 app.synth();
 

Private DNS Namespace Example

The following example creates an AWS Cloud Map namespace that supports both API calls and DNS queries within a vpc, creates a service in that namespace, and registers a loadbalancer as an instance:

 import software.amazon.awscdk.services.ec2.*;
 import software.amazon.awscdk.services.elasticloadbalancingv2.*;
 import software.amazon.awscdk.core.*;
 import software.amazon.awscdk.services.servicediscovery.*;
 
 App app = new App();
 Stack stack = new Stack(app, "aws-servicediscovery-integ");
 
 Vpc vpc = Vpc.Builder.create(stack, "Vpc").maxAzs(2).build();
 
 PrivateDnsNamespace namespace = PrivateDnsNamespace.Builder.create(stack, "Namespace")
         .name("boobar.com")
         .vpc(vpc)
         .build();
 
 Service service = namespace.createService("Service", DnsServiceProps.builder()
         .dnsRecordType(DnsRecordType.A_AAAA)
         .dnsTtl(Duration.seconds(30))
         .loadBalancer(true)
         .build());
 
 ApplicationLoadBalancer loadbalancer = ApplicationLoadBalancer.Builder.create(stack, "LB").vpc(vpc).internetFacing(true).build();
 
 service.registerLoadBalancer("Loadbalancer", loadbalancer);
 
 app.synth();
 

Public DNS Namespace Example

The following example creates an AWS Cloud Map namespace that supports both API calls and public DNS queries, creates a service in that namespace, and registers an IP instance:

 import software.amazon.awscdk.core.*;
 import software.amazon.awscdk.services.servicediscovery.*;
 
 App app = new App();
 Stack stack = new Stack(app, "aws-servicediscovery-integ");
 
 PublicDnsNamespace namespace = PublicDnsNamespace.Builder.create(stack, "Namespace")
         .name("foobar.com")
         .build();
 
 Service service = namespace.createService("Service", DnsServiceProps.builder()
         .name("foo")
         .dnsRecordType(DnsRecordType.A)
         .dnsTtl(Duration.seconds(30))
         .healthCheck(HealthCheckConfig.builder()
                 .type(HealthCheckType.HTTPS)
                 .resourcePath("/healthcheck")
                 .failureThreshold(2)
                 .build())
         .build());
 
 service.registerIpInstance("IpInstance", IpInstanceBaseProps.builder()
         .ipv4("54.239.25.192")
         .port(443)
         .build());
 
 app.synth();
 

For DNS namespaces, you can also register instances to services with CNAME records:

 import software.amazon.awscdk.core.*;
 import software.amazon.awscdk.services.servicediscovery.*;
 
 App app = new App();
 Stack stack = new Stack(app, "aws-servicediscovery-integ");
 
 PublicDnsNamespace namespace = PublicDnsNamespace.Builder.create(stack, "Namespace")
         .name("foobar.com")
         .build();
 
 Service service = namespace.createService("Service", DnsServiceProps.builder()
         .name("foo")
         .dnsRecordType(DnsRecordType.CNAME)
         .dnsTtl(Duration.seconds(30))
         .build());
 
 service.registerCnameInstance("CnameInstance", CnameInstanceBaseProps.builder()
         .instanceCname("service.pizza")
         .build());
 
 app.synth();
 
Skip navigation links