AWS CDK Service Discovery (Cloud Map)

1.204.0 · maintenance · verified Fri Apr 17

The `aws-cdk-aws-servicediscovery` package provides AWS Cloud Development Kit (CDK) constructs for AWS Cloud Map (Service Discovery). It allows you to define and manage DNS-based service discovery for your applications within AWS infrastructure as code. This package is part of the CDK v1 ecosystem, and its functionality is integrated into `aws-cdk-lib` for CDK v2. The current version is 1.204.0, following the release cadence of AWS CDK v1.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a Public DNS Namespace and register an IP-based service instance using AWS CDK v1. This sets up a discoverable DNS record (e.g., `web-api.example.com`) that can be resolved by clients.

import os
from aws_cdk import ( 
    core, 
    aws_servicediscovery as servicediscovery
)

class MyServiceDiscoveryStack(core.Stack):
    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        # Create a Public DNS Namespace
        # This will create a Route 53 Hosted Zone for your services.
        public_dns_namespace = servicediscovery.PublicDnsNamespace(
            self, "MyPublicNamespace",
            name="example.com"
        )

        # Create a service within the namespace
        # This service will have an A record by default for IP instances.
        service = public_dns_namespace.create_service(
            "MyPublicWebService",
            name="web-api"
        )

        # Register an IP instance with the service
        # Applications can now discover 'web-api.example.com'
        service.register_ip_instance(
            "WebInstance1",
            ip="192.0.2.100", # Example IP address, replace with actual target
            port=80
        )

# Instantiate and synthesize the CDK app
app = core.App()
MyServiceDiscoveryStack(app, os.environ.get('CDK_STACK_NAME', 'ServiceDiscoveryQuickstartStack'))
app.synth()

view raw JSON →