How do I configure service discovery with AWS Cloud Map through the AWS CLI?

4 minute read
0

I want to use AWS Cloud Map with the AWS Command Line Interface (AWS CLI) to create a hosted zone.

Resolution

Note: If you receive errors when you run AWS Command Line Interface (AWS CLI) commands, see Troubleshoot AWS CLI errors. Also, make sure that you're using the most recent AWS CLI version.

Create your DNS namespace

  1. Create the namespace using the AWS CLI, replacing example.com with the domain name that you want to use.

  2. You must choose to create a public or a private namespace. Public namespaces are visible on the internet when the domain name is registered. Private namespaces are visible only within the virtual private cloud (VPC). When you create a private namespace, you must specify the VPC ID.

    To create a public namespace, run this command:

    $ aws servicediscovery create-public-dns-namespace --name example.com

    To create a private namespace, run this command:

    $ aws servicediscovery create-private-dns-namespace --name example.com --vpc vpc-0c92f38bf7db24a05

    Note: Because the name is publicly available in DNS queries, don't include sensitive information in the name.


  3. Note the value of OperationId in the output, such as in this example:

    {
        "OperationId": "igbkufld72o4vbsbwejfi6eyinfprhc3-jkwmz00b"
    }
  4. To find more details about the operation, run the get-operation command. Replace OPERATION_VALUE with the OperationId value from the previous step:

    aws servicediscovery get-operation --operation-id OPERATION_VALUE
  5. In the output, verify that the Status value is SUCCESS. Also, note the NAMESPACE value. This is the namespace ID that creates the service and registers the instance:

    {
      "Operation": {
        "Status": "SUCCESS",
        "CreateDate": 1534428266.699,
        "Id": "igbkufld72o4vbsbwejfi6eyinfprhc3-jkwmz00b",
        "UpdateDate": 1534428267.113,
        "Type": "CREATE_NAMESPACE",
        "Targets": {
          "NAMESPACE": "ns-f2wjnv2p7pqtz5f2"
        }
      }
    }

    Note: When you create the namespace, Amazon Route 53 automatically creates a hosted zone for the domain. The hosted zone's Domain name value is the same domain name as your namespace. The Comment value is Created by Route 53 Auto Naming.

To verify the hosted zone, follow these steps:

  1. Open the Route 53 console.
  2. On the navigation pane, choose Hosted zones.
  3. Find your hosted zone in the list of hosted zones in the content pane.

Create your service

To create your service, follow these steps:

  1. Run the servicediscovery create-service command in shorthand syntax, as shown in this example. Replace WORKERS with your preferred service name. Route 53 uses this service name when it creates records:

    $aws servicediscovery create-service --name WORKERS--dns-config 'NamespaceId="ns-f2wjnv2p7pqtz5f2",RoutingPolicy="WEIGHTED",DnsRecords=[{Type="A",TTL="300"}]'

    Note: If the namespace is discoverable by public DNS queries, then don't include sensitive information in it. For services that DNS queries can access, you can't create multiple services with names that differ only by case (such as EXAMPLE and example). However, if you use a namespace that's accessible only by API calls, then you can create services with names that differ only by case.

    The default routing policy is MULTIVALUE. Supported routing policies are MULTIVALUE and WEIGHTED.

  2. Note the output. The Id value is the ID of the service you just created:
    Note: The CreatorRequestId is the ID of the request. If the API call fails, then use the CreatorRequestId to repeat the operation.

    {
      "Service": {
        "DnsConfig": {
          "NamespaceId": "ns-f2wjnv2p7pqtz5f2",
          "DnsRecords": [
            {
              "Type": "A",
              "TTL": 300
            }
          ]
        },
        "CreatorRequestId": "93e0a17a-230b-4c58-b986-f03f3217869f",
        "Id": "srv-iy3d7hhlf5cjciph",
        "Arn": "arn:aws:servicediscovery:eu-west-1:356906700443:service/srv-iy3d7hhlf5cjciph",
        "Name": "workers"
      }
    }

Register your instance

To register your instance, follow these steps:

  1. Run the servicediscovery register-instance command. Replace the value placeholders for service-id, instance-id, and attributes with your corresponding values. Without these parameters, the API call fails. Note that you can run only one request to register an instance with the same service-id and instance-id options at a time.

    $ aws servicediscovery register-instance --service-id srv-iy3d7hhlf5cjciph --instance-id i-039444aa1e2932ca3 --attributes=AWS_INSTANCE_IPV4=172.1.1.1

    Warning: You must wait for one register-instance request to complete before you can submit another request with the same service ID and instance ID.

  2. Review the output. This includes the OperationId, such as in this example:

    {
        "OperationId": "z7dfqgeadkvwwid2wa2n5ckicrxs255x-jkwr1x9f"
    }
  3. Open the Route 53 console.

  4. On the navigation pane, choose Hosted zones.

  5. Select the hosted zone that you created earlier.

  6. Choose Go to Record Sets, and then verify that the record sets created successfully for the hosted zone.

    Note: When you register the instance, Route 53 automatically creates a record with the service name and domain name.

    For Amazon Elastic Container Service (Amazon ECS) and Route 53 service discovery, you can use the Route 53 namespace and service name to configure services. Route 53 automatically creates, deletes, or updates records in your hosted zone according to your Amazon ECS container settings.