# ServiceDiscovery

Provides the Service Discovery method a VirtualNode uses.

- Source: [`aws-appmesh/lib/service-discovery.ts`](https://github.com/aws/aws-cdk/blob/v2.275.0/packages/aws-cdk-lib/aws-appmesh/lib/service-discovery.ts#L90)
- Type last changed: v2.30.0 · 1 Jul 2022

## Example

```ts
const vpc = new ec2.Vpc(this, 'vpc');
const namespace = new cloudmap.PrivateDnsNamespace(this, 'test-namespace', {
    vpc,
    name: 'domain.local',
});
const service = namespace.createService('Svc');

declare const mesh: appmesh.Mesh;
const node = mesh.addVirtualNode('virtual-node', {
  serviceDiscovery: appmesh.ServiceDiscovery.cloudMap(service),
  listeners: [appmesh.VirtualNodeListener.http({
    port: 8081,
    healthCheck: appmesh.HealthCheck.http({
      healthyThreshold: 3,
      interval: Duration.seconds(5), // minimum
      path: '/health-check-path',
      timeout: Duration.seconds(2), // minimum
      unhealthyThreshold: 2,
    }),
  })],
  accessLog: appmesh.AccessLog.fromFilePath('/dev/stdout'),
});
```

## Initializer

```ts
new ServiceDiscovery()
```

## Methods

- `bind(scope: Construct): ServiceDiscoveryConfig` — Binds the current object when adding Service Discovery to a VirtualNode.

- `cloudMap(service: IService, instanceAttributes?: { [key: string]: string }, ipPreference?: IpPreference): ServiceDiscovery // chainable` (static) — Returns Cloud Map based service discovery.

  **Parameters**

  - `service` — The AWS Cloud Map Service to use for service discovery.
  - `instanceAttributes` — A string map that contains attributes with values that you can use to filter instances by any custom attribute that you specified when you registered the instance.
  - `ipPreference` — No IP preference is applied to the Virtual Node.

- `dns(hostname: string, responseType?: DnsResponseType, ipPreference?: IpPreference): ServiceDiscovery // chainable` (static) — Returns DNS based service discovery.

  **Parameters**

  - `responseType` — Specifies the DNS response type for the virtual node.
  - `ipPreference` — No IP preference is applied to the Virtual Node.
