create_service

ServiceDiscovery.Client.create_service(**kwargs)

Creates a service. This action defines the configuration for the following entities:

  • For public and private DNS namespaces, one of the following combinations of DNS records in Amazon Route 53:
    • A
    • AAAA
    • A and AAAA
    • SRV
    • CNAME
  • Optionally, a health check

After you create the service, you can submit a RegisterInstance request, and Cloud Map uses the values in the configuration to create the specified entities.

For the current quota on the number of instances that you can register using the same namespace and using the same service, see Cloud Map quotas in the Cloud Map Developer Guide .

See also: AWS API Documentation

Request Syntax

response = client.create_service(
    Name='string',
    NamespaceId='string',
    CreatorRequestId='string',
    Description='string',
    DnsConfig={
        'NamespaceId': 'string',
        'RoutingPolicy': 'MULTIVALUE'|'WEIGHTED',
        'DnsRecords': [
            {
                'Type': 'SRV'|'A'|'AAAA'|'CNAME',
                'TTL': 123
            },
        ]
    },
    HealthCheckConfig={
        'Type': 'HTTP'|'HTTPS'|'TCP',
        'ResourcePath': 'string',
        'FailureThreshold': 123
    },
    HealthCheckCustomConfig={
        'FailureThreshold': 123
    },
    Tags=[
        {
            'Key': 'string',
            'Value': 'string'
        },
    ],
    Type='HTTP'
)
Parameters
  • Name (string) --

    [REQUIRED]

    The name that you want to assign to the service.

    Note

    Do not include sensitive information in the name if the namespace is discoverable by public DNS queries.

    If you want Cloud Map to create an SRV record when you register an instance and you're using a system that requires a specific SRV format, such as HAProxy, specify the following for Name :

    • Start the name with an underscore (_), such as _exampleservice .
    • End the name with ._protocol , such as ._tcp .

    When you register an instance, Cloud Map creates an SRV record and assigns a name to the record by concatenating the service name and the namespace name (for example,

    _exampleservice._tcp.example.com ).

    Note

    For services that are accessible by DNS queries, you can't create multiple services with names that differ only by case (such as EXAMPLE and example). Otherwise, these services have the same DNS name and can't be distinguished. However, if you use a namespace that's only accessible by API calls, then you can create services that with names that differ only by case.

  • NamespaceId (string) -- The ID of the namespace that you want to use to create the service. The namespace ID must be specified, but it can be specified either here or in the DnsConfig object.
  • CreatorRequestId (string) --

    A unique string that identifies the request and that allows failed CreateService requests to be retried without the risk of running the operation twice. CreatorRequestId can be any unique string (for example, a date/timestamp).

    This field is autopopulated if not provided.

  • Description (string) -- A description for the service.
  • DnsConfig (dict) --

    A complex type that contains information about the Amazon Route 53 records that you want Cloud Map to create when you register an instance.

    • NamespaceId (string) --
      Use NamespaceId in Service instead.

      The ID of the namespace to use for DNS configuration.

    • RoutingPolicy (string) --

      The routing policy that you want to apply to all Route 53 DNS records that Cloud Map creates when you register an instance and specify this service.

      Note

      If you want to use this service to register instances that create alias records, specify WEIGHTED for the routing policy.

      You can specify the following values:

      MULTIVALUE

      If you define a health check for the service and the health check is healthy, Route 53 returns the applicable value for up to eight instances.

      For example, suppose that the service includes configurations for one A record and a health check. You use the service to register 10 instances. Route 53 responds to DNS queries with IP addresses for up to eight healthy instances. If fewer than eight instances are healthy, Route 53 responds to every DNS query with the IP addresses for all of the healthy instances.

      If you don't define a health check for the service, Route 53 assumes that all instances are healthy and returns the values for up to eight instances.

      For more information about the multivalue routing policy, see Multivalue Answer Routing in the Route 53 Developer Guide .

      WEIGHTED

      Route 53 returns the applicable value from one randomly selected instance from among the instances that you registered using the same service. Currently, all records have the same weight, so you can't route more or less traffic to any instances.

      For example, suppose that the service includes configurations for one A record and a health check. You use the service to register 10 instances. Route 53 responds to DNS queries with the IP address for one randomly selected instance from among the healthy instances. If no instances are healthy, Route 53 responds to DNS queries as if all of the instances were healthy.

      If you don't define a health check for the service, Route 53 assumes that all instances are healthy and returns the applicable value for one randomly selected instance.

      For more information about the weighted routing policy, see Weighted Routing in the Route 53 Developer Guide .

    • DnsRecords (list) -- [REQUIRED]

      An array that contains one DnsRecord object for each Route 53 DNS record that you want Cloud Map to create when you register an instance.

      • (dict) --

        A complex type that contains information about the Route 53 DNS records that you want Cloud Map to create when you register an instance.

        • Type (string) -- [REQUIRED]

          The type of the resource, which indicates the type of value that Route 53 returns in response to DNS queries. You can specify values for Type in the following combinations:

          • A
          • AAAA
          • A and AAAA
          • SRV
          • CNAME

          If you want Cloud Map to create a Route 53 alias record when you register an instance, specify A or AAAA for Type .

          You specify other settings, such as the IP address for A and AAAA records, when you register an instance. For more information, see RegisterInstance.

          The following values are supported:

          A

          Route 53 returns the IP address of the resource in IPv4 format, such as 192.0.2.44.

          AAAA

          Route 53 returns the IP address of the resource in IPv6 format, such as 2001:0db8:85a3:0000:0000:abcd:0001:2345.

          CNAME

          Route 53 returns the domain name of the resource, such as www.example.com. Note the following:

          • You specify the domain name that you want to route traffic to when you register an instance. For more information, see Attributes in the topic RegisterInstance.
          • You must specify WEIGHTED for the value of RoutingPolicy .
          • You can't specify both CNAME for Type and settings for HealthCheckConfig . If you do, the request will fail with an InvalidInput error.

            SRV

          Route 53 returns the value for an SRV record. The value for an SRV record uses the following values:

          priority weight port service-hostname

          Note the following about the values:

          • The values of priority and weight are both set to 1 and can't be changed.
          • The value of port comes from the value that you specify for the AWS_INSTANCE_PORT attribute when you submit a RegisterInstance request.
          • The value of service-hostname is a concatenation of the following values:
            • The value that you specify for InstanceId when you register an instance.
            • The name of the service.
            • The name of the namespace.

          For example, if the value of InstanceId is test , the name of the service is backend , and the name of the namespace is example.com , the value of service-hostname is the following:

          test.backend.example.com

          If you specify settings for an SRV record, note the following:

          • If you specify values for AWS_INSTANCE_IPV4 , AWS_INSTANCE_IPV6 , or both in the RegisterInstance request, Cloud Map automatically creates A and/or AAAA records that have the same name as the value of service-hostname in the SRV record. You can ignore these records.
          • If you're using a system that requires a specific SRV format, such as HAProxy, see the Name element in the documentation about CreateService for information about how to specify the correct name format.
        • TTL (integer) -- [REQUIRED]

          The amount of time, in seconds, that you want DNS resolvers to cache the settings for this record.

          Note

          Alias records don't include a TTL because Route 53 uses the TTL for the Amazon Web Services resource that an alias record routes traffic to. If you include the AWS_ALIAS_DNS_NAME attribute when you submit a RegisterInstance request, the TTL value is ignored. Always specify a TTL for the service; you can use a service to register instances that create either alias or non-alias records.

  • HealthCheckConfig (dict) --
    Public DNS and HTTP namespaces only. A complex type that contains settings for an optional Route 53 health check. If you specify settings for a health check, Cloud Map associates the health check with all the Route 53 DNS records that you specify in DnsConfig .

    Warning

    If you specify a health check configuration, you can specify either HealthCheckCustomConfig or HealthCheckConfig but not both.

    For information about the charges for health checks, see Cloud Map Pricing.

    • Type (string) -- [REQUIRED]

      The type of health check that you want to create, which indicates how Route 53 determines whether an endpoint is healthy.

      Warning

      You can't change the value of Type after you create a health check.

      You can create the following types of health checks:

      • HTTP : Route 53 tries to establish a TCP connection. If successful, Route 53 submits an HTTP request and waits for an HTTP status code of 200 or greater and less than 400.
      • HTTPS : Route 53 tries to establish a TCP connection. If successful, Route 53 submits an HTTPS request and waits for an HTTP status code of 200 or greater and less than 400.

      Warning

      If you specify HTTPS for the value of Type , the endpoint must support TLS v1.0 or later.

      • TCP : Route 53 tries to establish a TCP connection. If you specify TCP for Type , don't specify a value for ResourcePath .

      For more information, see How Route 53 Determines Whether an Endpoint Is Healthy in the Route 53 Developer Guide .

    • ResourcePath (string) --

      The path that you want Route 53 to request when performing health checks. The path can be any value that your endpoint returns an HTTP status code of a 2xx or 3xx format for when the endpoint is healthy. An example file is /docs/route53-health-check.html . Route 53 automatically adds the DNS name for the service. If you don't specify a value for ResourcePath , the default value is / .

      If you specify TCP for Type , you must not specify a value for ResourcePath .

    • FailureThreshold (integer) --

      The number of consecutive health checks that an endpoint must pass or fail for Route 53 to change the current status of the endpoint from unhealthy to healthy or the other way around. For more information, see How Route 53 Determines Whether an Endpoint Is Healthy in the Route 53 Developer Guide .

  • HealthCheckCustomConfig (dict) --

    A complex type that contains information about an optional custom health check.

    Warning

    If you specify a health check configuration, you can specify either HealthCheckCustomConfig or HealthCheckConfig but not both.

    You can't add, update, or delete a HealthCheckCustomConfig configuration from an existing service.

    • FailureThreshold (integer) --

      Warning

      This parameter is no longer supported and is always set to 1. Cloud Map waits for approximately 30 seconds after receiving an UpdateInstanceCustomHealthStatus request before changing the status of the service instance.

      The number of 30-second intervals that you want Cloud Map to wait after receiving an UpdateInstanceCustomHealthStatus request before it changes the health status of a service instance.

      Sending a second or subsequent UpdateInstanceCustomHealthStatus request with the same value before 30 seconds has passed doesn't accelerate the change. Cloud Map still waits 30 seconds after the first request to make the change.

  • Tags (list) --

    The tags to add to the service. Each tag consists of a key and an optional value that you define. Tags keys can be up to 128 characters in length, and tag values can be up to 256 characters in length.

    • (dict) --

      A custom key-value pair that's associated with a resource.

      • Key (string) -- [REQUIRED]

        The key identifier, or name, of the tag.

      • Value (string) -- [REQUIRED]

        The string value that's associated with the key of the tag. You can set the value of a tag to an empty string, but you can't set the value of a tag to null.

  • Type (string) -- If present, specifies that the service instances are only discoverable using the DiscoverInstances API operation. No DNS records is registered for the service instances. The only valid value is HTTP .
Return type

dict

Returns

Response Syntax

{
    'Service': {
        'Id': 'string',
        'Arn': 'string',
        'Name': 'string',
        'NamespaceId': 'string',
        'Description': 'string',
        'InstanceCount': 123,
        'DnsConfig': {
            'NamespaceId': 'string',
            'RoutingPolicy': 'MULTIVALUE'|'WEIGHTED',
            'DnsRecords': [
                {
                    'Type': 'SRV'|'A'|'AAAA'|'CNAME',
                    'TTL': 123
                },
            ]
        },
        'Type': 'HTTP'|'DNS_HTTP'|'DNS',
        'HealthCheckConfig': {
            'Type': 'HTTP'|'HTTPS'|'TCP',
            'ResourcePath': 'string',
            'FailureThreshold': 123
        },
        'HealthCheckCustomConfig': {
            'FailureThreshold': 123
        },
        'CreateDate': datetime(2015, 1, 1),
        'CreatorRequestId': 'string'
    }
}

Response Structure

  • (dict) --

    • Service (dict) --

      A complex type that contains information about the new service.

      • Id (string) --

        The ID that Cloud Map assigned to the service when you created it.

      • Arn (string) --

        The Amazon Resource Name (ARN) that Cloud Map assigns to the service when you create it.

      • Name (string) --

        The name of the service.

      • NamespaceId (string) --

        The ID of the namespace that was used to create the service.

      • Description (string) --

        The description of the service.

      • InstanceCount (integer) --

        The number of instances that are currently associated with the service. Instances that were previously associated with the service but that are deleted aren't included in the count. The count might not reflect pending registrations and deregistrations.

      • DnsConfig (dict) --

        A complex type that contains information about the Route 53 DNS records that you want Cloud Map to create when you register an instance.

        Warning

        The record types of a service can only be changed by deleting the service and recreating it with a new Dnsconfig .

        • NamespaceId (string) --

          Use NamespaceId in Service instead.

          The ID of the namespace to use for DNS configuration.

        • RoutingPolicy (string) --

          The routing policy that you want to apply to all Route 53 DNS records that Cloud Map creates when you register an instance and specify this service.

          Note

          If you want to use this service to register instances that create alias records, specify WEIGHTED for the routing policy.

          You can specify the following values:

          MULTIVALUE

          If you define a health check for the service and the health check is healthy, Route 53 returns the applicable value for up to eight instances.

          For example, suppose that the service includes configurations for one A record and a health check. You use the service to register 10 instances. Route 53 responds to DNS queries with IP addresses for up to eight healthy instances. If fewer than eight instances are healthy, Route 53 responds to every DNS query with the IP addresses for all of the healthy instances.

          If you don't define a health check for the service, Route 53 assumes that all instances are healthy and returns the values for up to eight instances.

          For more information about the multivalue routing policy, see Multivalue Answer Routing in the Route 53 Developer Guide .

          WEIGHTED

          Route 53 returns the applicable value from one randomly selected instance from among the instances that you registered using the same service. Currently, all records have the same weight, so you can't route more or less traffic to any instances.

          For example, suppose that the service includes configurations for one A record and a health check. You use the service to register 10 instances. Route 53 responds to DNS queries with the IP address for one randomly selected instance from among the healthy instances. If no instances are healthy, Route 53 responds to DNS queries as if all of the instances were healthy.

          If you don't define a health check for the service, Route 53 assumes that all instances are healthy and returns the applicable value for one randomly selected instance.

          For more information about the weighted routing policy, see Weighted Routing in the Route 53 Developer Guide .

        • DnsRecords (list) --

          An array that contains one DnsRecord object for each Route 53 DNS record that you want Cloud Map to create when you register an instance.

          • (dict) --

            A complex type that contains information about the Route 53 DNS records that you want Cloud Map to create when you register an instance.

            • Type (string) --

              The type of the resource, which indicates the type of value that Route 53 returns in response to DNS queries. You can specify values for Type in the following combinations:

              • A
              • AAAA
              • A and AAAA
              • SRV
              • CNAME

              If you want Cloud Map to create a Route 53 alias record when you register an instance, specify A or AAAA for Type .

              You specify other settings, such as the IP address for A and AAAA records, when you register an instance. For more information, see RegisterInstance.

              The following values are supported:

              A

              Route 53 returns the IP address of the resource in IPv4 format, such as 192.0.2.44.

              AAAA

              Route 53 returns the IP address of the resource in IPv6 format, such as 2001:0db8:85a3:0000:0000:abcd:0001:2345.

              CNAME

              Route 53 returns the domain name of the resource, such as www.example.com. Note the following:

              • You specify the domain name that you want to route traffic to when you register an instance. For more information, see Attributes in the topic RegisterInstance.

              • You must specify WEIGHTED for the value of RoutingPolicy .

              • You can't specify both CNAME for Type and settings for HealthCheckConfig . If you do, the request will fail with an InvalidInput error.

                SRV

              Route 53 returns the value for an SRV record. The value for an SRV record uses the following values:

              priority weight port service-hostname

              Note the following about the values:

              • The values of priority and weight are both set to 1 and can't be changed.
              • The value of port comes from the value that you specify for the AWS_INSTANCE_PORT attribute when you submit a RegisterInstance request.
              • The value of service-hostname is a concatenation of the following values:
                • The value that you specify for InstanceId when you register an instance.
                • The name of the service.
                • The name of the namespace.

              For example, if the value of InstanceId is test , the name of the service is backend , and the name of the namespace is example.com , the value of service-hostname is the following:

              test.backend.example.com

              If you specify settings for an SRV record, note the following:

              • If you specify values for AWS_INSTANCE_IPV4 , AWS_INSTANCE_IPV6 , or both in the RegisterInstance request, Cloud Map automatically creates A and/or AAAA records that have the same name as the value of service-hostname in the SRV record. You can ignore these records.
              • If you're using a system that requires a specific SRV format, such as HAProxy, see the Name element in the documentation about CreateService for information about how to specify the correct name format.
            • TTL (integer) --

              The amount of time, in seconds, that you want DNS resolvers to cache the settings for this record.

              Note

              Alias records don't include a TTL because Route 53 uses the TTL for the Amazon Web Services resource that an alias record routes traffic to. If you include the AWS_ALIAS_DNS_NAME attribute when you submit a RegisterInstance request, the TTL value is ignored. Always specify a TTL for the service; you can use a service to register instances that create either alias or non-alias records.

      • Type (string) --

        Describes the systems that can be used to discover the service instances.

        DNS_HTTP

        The service instances can be discovered using either DNS queries or the DiscoverInstances API operation.

        HTTP

        The service instances can only be discovered using the DiscoverInstances API operation.

        DNS

        Reserved.

      • HealthCheckConfig (dict) --

        Public DNS and HTTP namespaces only. A complex type that contains settings for an optional health check. If you specify settings for a health check, Cloud Map associates the health check with the records that you specify in DnsConfig .

        For information about the charges for health checks, see Amazon Route 53 Pricing.

        • Type (string) --

          The type of health check that you want to create, which indicates how Route 53 determines whether an endpoint is healthy.

          Warning

          You can't change the value of Type after you create a health check.

          You can create the following types of health checks:

          • HTTP : Route 53 tries to establish a TCP connection. If successful, Route 53 submits an HTTP request and waits for an HTTP status code of 200 or greater and less than 400.
          • HTTPS : Route 53 tries to establish a TCP connection. If successful, Route 53 submits an HTTPS request and waits for an HTTP status code of 200 or greater and less than 400.

          Warning

          If you specify HTTPS for the value of Type , the endpoint must support TLS v1.0 or later.

          • TCP : Route 53 tries to establish a TCP connection. If you specify TCP for Type , don't specify a value for ResourcePath .

          For more information, see How Route 53 Determines Whether an Endpoint Is Healthy in the Route 53 Developer Guide .

        • ResourcePath (string) --

          The path that you want Route 53 to request when performing health checks. The path can be any value that your endpoint returns an HTTP status code of a 2xx or 3xx format for when the endpoint is healthy. An example file is /docs/route53-health-check.html . Route 53 automatically adds the DNS name for the service. If you don't specify a value for ResourcePath , the default value is / .

          If you specify TCP for Type , you must not specify a value for ResourcePath .

        • FailureThreshold (integer) --

          The number of consecutive health checks that an endpoint must pass or fail for Route 53 to change the current status of the endpoint from unhealthy to healthy or the other way around. For more information, see How Route 53 Determines Whether an Endpoint Is Healthy in the Route 53 Developer Guide .

      • HealthCheckCustomConfig (dict) --

        A complex type that contains information about an optional custom health check.

        Warning

        If you specify a health check configuration, you can specify either HealthCheckCustomConfig or HealthCheckConfig but not both.

        • FailureThreshold (integer) --

          Warning

          This parameter is no longer supported and is always set to 1. Cloud Map waits for approximately 30 seconds after receiving an UpdateInstanceCustomHealthStatus request before changing the status of the service instance.

          The number of 30-second intervals that you want Cloud Map to wait after receiving an UpdateInstanceCustomHealthStatus request before it changes the health status of a service instance.

          Sending a second or subsequent UpdateInstanceCustomHealthStatus request with the same value before 30 seconds has passed doesn't accelerate the change. Cloud Map still waits 30 seconds after the first request to make the change.

      • CreateDate (datetime) --

        The date and time that the service was created, in Unix format and Coordinated Universal Time (UTC). The value of CreateDate is accurate to milliseconds. For example, the value 1516925490.087 represents Friday, January 26, 2018 12:11:30.087 AM.

      • CreatorRequestId (string) --

        A unique string that identifies the request and that allows failed requests to be retried without the risk of running the operation twice. CreatorRequestId can be any unique string (for example, a date/timestamp).

Exceptions

  • ServiceDiscovery.Client.exceptions.InvalidInput
  • ServiceDiscovery.Client.exceptions.ResourceLimitExceeded
  • ServiceDiscovery.Client.exceptions.NamespaceNotFound
  • ServiceDiscovery.Client.exceptions.ServiceAlreadyExists
  • ServiceDiscovery.Client.exceptions.TooManyTagsException
  • ServiceDiscovery.Client.exceptions.RequestLimitExceeded

Examples

Example: Create service

response = client.create_service(
    CreatorRequestId='567c1193-6b00-4308-bd57-ad38a8822d25',
    DnsConfig={
        'DnsRecords': [
            {
                'TTL': 60,
                'Type': 'A',
            },
        ],
        'NamespaceId': 'ns-ylexjili4cdxy3xm',
        'RoutingPolicy': 'MULTIVALUE',
    },
    Name='myservice',
    NamespaceId='ns-ylexjili4cdxy3xm',
)

print(response)

Expected Output:

{
    'Service': {
        'Arn': 'arn:aws:servicediscovery:us-west-2:123456789012:service/srv-p5zdwlg5uvvzjita',
        'CreateDate': 1587081768.334,
        'CreatorRequestId': '567c1193-6b00-4308-bd57-ad38a8822d25',
        'DnsConfig': {
            'DnsRecords': [
                {
                    'TTL': 60,
                    'Type': 'A',
                },
            ],
            'NamespaceId': 'ns-ylexjili4cdxy3xm',
            'RoutingPolicy': 'MULTIVALUE',
        },
        'Id': 'srv-p5zdwlg5uvvzjita',
        'Name': 'myservice',
        'NamespaceId': 'ns-ylexjili4cdxy3xm',
    },
    'ResponseMetadata': {
        '...': '...',
    },
}