# Service

Define a CloudMap Service.

Extends: Construct › Resource › Service
Implements: IResource, IService, IEnvironmentAware, IServiceRef, IConstruct, IDependable

- CloudFormation: [`AWS::ServiceDiscovery::Service`](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-servicediscovery-service.html)
- L1 type: [`CfnService`](/cdk/api/v2/docs/aws-cdk-lib.aws_servicediscovery.CfnService.html)
- Source: [`aws-servicediscovery/lib/service.ts`](https://github.com/aws/aws-cdk/blob/v2.274.0/packages/aws-cdk-lib/aws-servicediscovery/lib/service.ts#L184)
- Type last changed: v2.234.0 · 8 Jan 2026

## Example

```ts
import * as cdk from '../../core';
import * as servicediscovery from '../lib';

const app = new cdk.App();
const stack = new cdk.Stack(app, 'aws-servicediscovery-integ');

const namespace = new servicediscovery.PublicDnsNamespace(stack, 'Namespace', {
  name: 'foobar.com',
});

const service = namespace.createService('Service', {
  name: 'foo',
  dnsRecordType: servicediscovery.DnsRecordType.A,
  dnsTtl: cdk.Duration.seconds(30),
  healthCheck: {
    type: servicediscovery.HealthCheckType.HTTPS,
    resourcePath: '/healthcheck',
    failureThreshold: 2,
  },
});

service.registerIpInstance('IpInstance', {
  ipv4: '54.239.25.192',
  port: 443,
});

app.synth();
```

## Initializer

```ts
new Service(scope: Construct, id: string, props: ServiceProps)
```

## Construct Props

- `customHealthCheck`: [HealthCheckCustomConfig](/cdk/api/v2/docs/aws-cdk-lib.aws_servicediscovery.HealthCheckCustomConfig.html) = none — Structure containing failure threshold for a custom health checker.

  Only one of healthCheckConfig or healthCheckCustomConfig can be specified.
  See: https://docs.aws.amazon.com/cloud-map/latest/api/API_HealthCheckCustomConfig.html

- `description`: string = none — A description of the service.

- `discoveryType`: [DiscoveryType](/cdk/api/v2/docs/aws-cdk-lib.aws_servicediscovery.DiscoveryType.html) = DNS_AND_API — Controls how instances within this service can be discovered.

- `dnsRecordType`: [DnsRecordType](/cdk/api/v2/docs/aws-cdk-lib.aws_servicediscovery.DnsRecordType.html) = A — The DNS type of the record that you want AWS Cloud Map to create.

  Supported record types
  include A, AAAA, A and AAAA (A_AAAA), CNAME, and SRV.

- `dnsTtl`: [Duration](/cdk/api/v2/docs/aws-cdk-lib.Duration.html) = Duration.minutes(1) — The amount of time, in seconds, that you want DNS resolvers to cache the settings for this record.

- `healthCheck`: [HealthCheckConfig](/cdk/api/v2/docs/aws-cdk-lib.aws_servicediscovery.HealthCheckConfig.html) = none — Settings for an optional health check.

  If you specify health check settings, AWS Cloud Map associates the health
  check with the records that you specify in DnsConfig. Only one of healthCheckConfig or healthCheckCustomConfig can
  be specified. Not valid for PrivateDnsNamespaces. If you use healthCheck, you can only register IP instances to
  this service.

- `loadBalancer`: boolean = false — Whether or not this service will have an Elastic LoadBalancer registered to it as an AliasTargetInstance.

  Setting this to `true` correctly configures the `routingPolicy`
  and performs some additional validation.

- `name`: string — A name for the Service.

  **Default:** CloudFormation-generated name

- `namespace`: [INamespace](/cdk/api/v2/docs/aws-cdk-lib.aws_servicediscovery.INamespace.html) (required) — The namespace that you want to use for DNS configuration.

- `routingPolicy`: [RoutingPolicy](/cdk/api/v2/docs/aws-cdk-lib.aws_servicediscovery.RoutingPolicy.html) — The routing policy that you want to apply to all DNS records that AWS Cloud Map creates when you register an instance and specify this service.

  **Default:** WEIGHTED for CNAME records and when loadBalancer is true, MULTIVALUE otherwise

## Properties

- `discoveryType`: [DiscoveryType](/cdk/api/v2/docs/aws-cdk-lib.aws_servicediscovery.DiscoveryType.html) (required) — The discovery type used by this service.

- `dnsRecordType`: [DnsRecordType](/cdk/api/v2/docs/aws-cdk-lib.aws_servicediscovery.DnsRecordType.html) (required) — The DnsRecordType used by the service.

- `env`: [ResourceEnvironment](/cdk/api/v2/docs/aws-cdk-lib.interfaces.ResourceEnvironment.html) (required) — The environment this resource belongs to.

  For resources that are created and managed in a Stack (those created by
  creating new class instances like `new Role()`, `new Bucket()`, etc.), this
  is always the same as the environment of the stack they belong to.

  For referenced resources (those obtained from referencing methods like
  `Role.fromRoleArn()`, `Bucket.fromBucketName()`, etc.), they might be
  different than the stack they were imported into.

- `namespace`: [INamespace](/cdk/api/v2/docs/aws-cdk-lib.aws_servicediscovery.INamespace.html) (required) — The namespace for the Cloudmap Service.

- `node`: [Node](/cdk/api/v2/docs/constructs.Node.html) (required) — The tree node.

- `PROPERTY_INJECTION_ID`: string (required) (static) — Uniquely identifies this class.

- `routingPolicy`: [RoutingPolicy](/cdk/api/v2/docs/aws-cdk-lib.aws_servicediscovery.RoutingPolicy.html) (required) — The Routing Policy used by the service.

- `serviceArn`: string (required) — The Arn of the namespace that you want to use for DNS configuration.

- `serviceId`: string (required) — The ID of the namespace that you want to use for DNS configuration.

- `serviceName`: string (required) — A name for the Cloudmap Service.

- `serviceRef`: [ServiceReference](/cdk/api/v2/docs/aws-cdk-lib.interfaces.aws_servicediscovery.ServiceReference.html) (required) — A reference to a Service resource.

- `stack`: [Stack](/cdk/api/v2/docs/aws-cdk-lib.Stack.html) (required) — The stack in which this resource is defined.

## Methods

- `applyCrossStackReferenceStrength(strength: ReferenceStrength): void` — Override the cross-stack reference strength for this resource.

  When set, any cross-stack reference to this resource will use the specified
  mechanism instead of the global default determined by the
  `@aws-cdk/core:defaultCrossStackReferences` context key. This is useful for
  selectively weakening specific references to avoid the "deadly embrace" problem
  without changing the app-wide default.

  **Parameters**

  - `strength` — - The reference strength to use for this resource.

- `applyRemovalPolicy(policy: RemovalPolicy): void` — Apply the given removal policy to this resource.

  The Removal Policy controls what happens to this resource when it stops
  being managed by CloudFormation, either because you've removed it from the
  CDK application or because you've made a change that requires the resource
  to be replaced.

  The resource can be deleted (`RemovalPolicy.DESTROY`), or left in your AWS
  account for data recovery and cleanup later (`RemovalPolicy.RETAIN`).

- `fromServiceAttributes(scope: Construct, id: string, attrs: ServiceAttributes): IService` (static)

- `isConstruct(x: any): boolean` (static) — Checks if `x` is a construct.

  Use this method instead of `instanceof` to properly detect `Construct`
  instances, even when the construct library is symlinked.

  Explanation: in JavaScript, multiple copies of the `constructs` library on
  disk are seen as independent, completely different libraries. As a
  consequence, the class `Construct` in each copy of the `constructs` library
  is seen as a different class, and an instance of one class will not test as
  `instanceof` the other class. `npm install` will not create installations
  like this, but users may manually symlink construct libraries together or
  use a monorepo tool: in those cases, multiple copies of the `constructs`
  library can be accidentally installed, and `instanceof` will behave
  unpredictably. It is safest to avoid using `instanceof`, and using
  this type-testing method instead.

  **Parameters**

  - `x` — Any object.

  **Returns:** true if `x` is an object created from a class which extends `Construct`.

- `isOwnedResource(construct: IConstruct): boolean` (static) — Returns true if the construct was created by CDK, and false otherwise.

- `isResource(construct: IConstruct): boolean` (static) — Check whether the given construct is a Resource.

- `registerCnameInstance(id: string, props: CnameInstanceBaseProps): IInstance` — Registers a resource that is accessible using a CNAME.

- `registerIpInstance(id: string, props: IpInstanceBaseProps): IInstance` — Registers a resource that is accessible using an IP address.

- `registerLoadBalancer(id: string, loadBalancer: ILoadBalancerV2, customAttributes?: { [key: string]: string }): IInstance` — Registers an ELB as a new instance with unique name instanceId in this service.

- `registerNonIpInstance(id: string, props: NonIpInstanceBaseProps): IInstance` — Registers a resource that is accessible using values other than an IP address or a domain name (CNAME).

- `toString(): string` — Returns a string representation of this construct.

- `with(...mixins: IMixin[]): IConstruct` — Applies one or more mixins to this construct.

  Mixins are applied in order. The list of constructs is captured at the
  start of the call, so constructs added by a mixin will not be visited.
  Use multiple `with()` calls if subsequent mixins should apply to added
  constructs.
