Amazon Cloud Map examples using Amazon CLI - Amazon Command Line Interface
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

This documentation is for Version 1 of the Amazon CLI only. For documentation related to Version 2 of the Amazon CLI, see the Version 2 User Guide.

Amazon Cloud Map examples using Amazon CLI

The following code examples show you how to perform actions and implement common scenarios by using the Amazon Command Line Interface with Amazon Cloud Map.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.

Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to use create-http-namespace.

Amazon CLI

To create an HTTP namespace

The following create-http-namespace example creates an HTTP namespace example.com.

aws servicediscovery create-http-namespace \ --name example.com \ --creator-request-id example-request-id

Output:

{ "OperationId": "gv4g5meo7ndmeh4fqskygvk23d2fijwa-k9302yzd" }

To confirm that the operation succeeded, you can run get-operation. For more information, see get-operation .

For more information about creating a namespace, see Creating an Amazon Cloud Map namespace to group application services in the Amazon Cloud Map Developer Guide.

The following code example shows how to use create-private-dns-namespace.

Amazon CLI

To create a private DNS namespace

The following create-private-dns-namespace example creates a private DNS namespace.

aws servicediscovery create-private-dns-namespace \ --name example.com \ --vpc vpc-1c56417b

Output:

{ "OperationId": "gv4g5meo7ndmeh4fqskygvk23d2fijwa-k9302yzd" }

To confirm that the operation succeeded, you can run get-operation. For more information, see get-operation .

For more information, see Creating namespaces in the Amazon Cloud Map Developer Guide.

The following code example shows how to use create-public-dns-namespace.

Amazon CLI

To create an public DNS namespace

The following create-public-dns-namespace example creates an public DNS namespace example.com.

aws servicediscovery create-public-dns-namespace \ --name example-public-dns.com \ --creator-request-id example-public-request-id \ --properties DnsProperties={SOA={TTL=60}}

Output:

{ "OperationId": "gv4g5meo7ndmeh4fqskygvk23d2fijwa-k9302yzd" }

To confirm that the operation succeeded, you can run get-operation.

For more information about creating a namespace, see Creating an Amazon Cloud Map namespace to group application services in the Amazon Cloud Map Developer Guide.

The following code example shows how to use create-service.

Amazon CLI

To create a service

The following create-service example creates a service.

aws servicediscovery create-service \ --name myservice \ --namespace-id ns-ylexjili4cdxy3xm \ --dns-config "NamespaceId=ns-ylexjili4cdxy3xm,RoutingPolicy=MULTIVALUE,DnsRecords=[{Type=A,TTL=60}]"

Output:

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

For more information, see Creating services in the Amazon Cloud Map Developer Guide.

  • For API details, see CreateService in Amazon CLI Command Reference.

The following code example shows how to use delete-namespace.

Amazon CLI

To delete a namespace

The following delete-namespace example deletes a namespace.

aws servicediscovery delete-namespace \ --id ns-ylexjili4cdxy3xm

Output:

{ "OperationId": "gv4g5meo7ndmeh4fqskygvk23d2fijwa-k98y6drk" }

To confirm that the operation succeeded, you can run get-operation. For more information, see get-operation .

For more information, see Deleting namespaces in the Amazon Cloud Map Developer Guide.

The following code example shows how to use delete-service-attributes.

Amazon CLI

To delete a service attribute

The following delete-service-attributes example deletes a service attribute with the key Port that is associated with the specified service.

aws servicediscovery delete-service-attributes \ --service-id srv-e4anhexample0004 \ --attributes Port

This command produces no output.

For more information, see Deleting namespaces in the Amazon Cloud Map Developer Guide.

The following code example shows how to use delete-service.

Amazon CLI

To delete a service

The following delete-service example deletes a service.

aws servicediscovery delete-service \ --id srv-p5zdwlg5uvvzjita

This command produces no output.

For more information, see Deleting services in the Amazon Cloud Map Developer Guide.

  • For API details, see DeleteService in Amazon CLI Command Reference.

The following code example shows how to use deregister-instance.

Amazon CLI

To deregister a service instance

The following deregister-instance example deregisters a service instance.

aws servicediscovery deregister-instance \ --service-id srv-p5zdwlg5uvvzjita \ --instance-id myservice-53

Output:

{ "OperationId": "4yejorelbukcjzpnr6tlmrghsjwpngf4-k98rnaiq" }

To confirm that the operation succeeded, you can run get-operation. For more information, see get-operation .

For more information, see Deregistering service instances in the Amazon Cloud Map Developer Guide.

The following code example shows how to use discover-instances-revision.

Amazon CLI

To discover the revision of an instance

The following discover-instances-revision example discovers the increasing revision of an instance.

aws servicediscovery discover-instances-revision \ --namespace-name example.com \ --service-name myservice

Output:

{ "InstancesRevision": 123456 }

For more information, see Amazon Cloud Map service instances in the Amazon Cloud Map Developer Guide.

The following code example shows how to use discover-instances.

Amazon CLI

To discover registered instances

The following discover-instances example discovers registered instances.

aws servicediscovery discover-instances \ --namespace-name example.com \ --service-name myservice \ --max-results 10 \ --health-status ALL

Output:

{ "Instances": [ { "InstanceId": "myservice-53", "NamespaceName": "example.com", "ServiceName": "myservice", "HealthStatus": "UNKNOWN", "Attributes": { "AWS_INSTANCE_IPV4": "172.2.1.3", "AWS_INSTANCE_PORT": "808" } } ] }

For more information, see Amazon Cloud Map service instances in the Amazon Cloud Map Developer Guide.

The following code example shows how to use get-instance.

Amazon CLI

To get the details of an instance

The following get-instance example gets the attributes of a service.

aws servicediscovery get-instance \ --service-id srv-e4anhexample0004 --instance-id i-abcd1234

Output:

{ "Instances": { "Id": "arn:aws:servicediscovery:us-west-2:111122223333;:service/srv-e4anhexample0004", "Attributes": { "AWS_INSTANCE_IPV4": "192.0.2.44", "AWS_INSTANCE_PORT": "80", "color": "green", "region": "us-west-2", "stage": "beta" } } }

For more information, see Amazon Cloud Map service instances in the Amazon Cloud Map Developer Guide.

  • For API details, see GetInstance in Amazon CLI Command Reference.

The following code example shows how to use get-instances-health-status.

Amazon CLI

To get the health status of instances associated with a service

The following get-instances-health-status example gets the health status of instances associated with the specified service.

aws servicediscovery get-instances-health-status \ --service-id srv-e4anhexample0004

Output:

{ "Status": { "i-abcd1234": "HEALTHY", "i-abcd1235": "UNHEALTHY" } }

For more information, see Amazon Cloud Map service instances in the Amazon Cloud Map Developer Guide.

The following code example shows how to use get-namespace.

Amazon CLI

To get the details of a namespace

The following get-namespace example retrieves information about the specified namespace.

aws servicediscovery get-namespace \ --id ns-e4anhexample0004

Output:

{ "Namespaces": { "Arn": "arn:aws:servicediscovery:us-west-2:123456789012:namespace/ns-e4anhexample0004", "CreateDate": "20181118T211712Z", "CreatorRequestId": "example-creator-request-id-0001", "Description": "Example.com AWS Cloud Map HTTP Namespace", "Id": "ns-e4anhexample0004", "Name": "example-http.com", "Properties": { "DnsProperties": {}, "HttpProperties": { "HttpName": "example-http.com" } }, "Type": "HTTP" } }

For more information, see Amazon Cloud Map namespaces in the Amazon Cloud Map Developer Guide.

  • For API details, see GetNamespace in Amazon CLI Command Reference.

The following code example shows how to use get-operation.

Amazon CLI

To get the result of an operation

The following get-operation example gets the result of a namespace creation operation.

aws servicediscovery get-operation \ --operation-id gv4g5meo7ndmeh4fqskygvk23d2fijwa-k9302yzd

Output:

{ "Operation": { "Id": "gv4g5meo7ndmeh4fqskygvk23d2fijwa-k9302yzd", "Type": "CREATE_NAMESPACE", "Status": "SUCCESS", "CreateDate": 1587055860.121, "UpdateDate": 1587055900.469, "Targets": { "NAMESPACE": "ns-ylexjili4cdxy3xm" } } }

For more information, see Creating an Amazon Cloud Map namespace to group application services in the Amazon Cloud Map Developer Guide.

  • For API details, see GetOperation in Amazon CLI Command Reference.

The following code example shows how to use get-service-attributes.

Amazon CLI

To get the attributes of an service

The following get-service-attributes example gets the attributes of a service.

aws servicediscovery get-service-attributes \ --service-id srv-e4anhexample0004

Output:

{ "ServiceAttributes": { "ServiceArn": "arn:aws:servicediscovery:us-west-2:111122223333;:service/srv-e4anhexample0004", "Attributes": { "Port": "80" } } }

For more information, see Amazon Cloud Map services in the Amazon Cloud Map Developer Guide.

The following code example shows how to use get-service.

Amazon CLI

To get the settings of a service

The following get-service example gets the settings of a specified service.

aws servicediscovery get-service \ --id srv-e4anhexample0004

Output:

{ "Service": { "Id": "srv-e4anhexample0004", "Arn": "arn:aws:servicediscovery:us-west-2:111122223333:service/srv-e4anhexample0004", "Name": "test-service", "NamespaceId": "ns-e4anhexample0004", "DnsConfig": {}, "Type": "HTTP", "CreateDate": "2025-02-24T10:59:02.905000-06:00", "CreatorRequestId": "3f50f9d9-b14c-482e-a556-d2a22fe6106d" } }

For more information, see Amazon Cloud Map services in the Amazon Cloud Map Developer Guide.

  • For API details, see GetService in Amazon CLI Command Reference.

The following code example shows how to use list-instances.

Amazon CLI

To list service instances

The following list-instances example lists service instances.

aws servicediscovery list-instances \ --service-id srv-qzpwvt2tfqcegapy

Output:

{ "Instances": [ { "Id": "i-06bdabbae60f65a4e", "Attributes": { "AWS_INSTANCE_IPV4": "172.2.1.3", "AWS_INSTANCE_PORT": "808" } } ] }

For more information, see Viewing a list of service instances in the Amazon Cloud Map Developer Guide.

  • For API details, see ListInstances in Amazon CLI Command Reference.

The following code example shows how to use list-namespaces.

Amazon CLI

To list namespaces

The following list-namespaces example lists namespaces.

aws servicediscovery list-namespaces

Output:

{ "Namespaces": [ { "Arn": "arn:aws:servicediscovery:us-west-2:123456789012:namespace/ns-a3ccy2e7e3a7rile", "CreateDate": 1585354387.357, "Id": "ns-a3ccy2e7e3a7rile", "Name": "local", "Properties": { "DnsProperties": { "HostedZoneId": "Z06752353VBUDTC32S84S" }, "HttpProperties": { "HttpName": "local" } }, "Type": "DNS_PRIVATE" }, { "Arn": "arn:aws:servicediscovery:us-west-2:123456789012:namespace/ns-pocfyjtrsmwtvcxx", "CreateDate": 1586468974.698, "Description": "My second namespace", "Id": "ns-pocfyjtrsmwtvcxx", "Name": "My-second-namespace", "Properties": { "DnsProperties": {}, "HttpProperties": { "HttpName": "My-second-namespace" } }, "Type": "HTTP" }, { "Arn": "arn:aws:servicediscovery:us-west-2:123456789012:namespace/ns-ylexjili4cdxy3xm", "CreateDate": 1587055896.798, "Id": "ns-ylexjili4cdxy3xm", "Name": "example.com", "Properties": { "DnsProperties": { "HostedZoneId": "Z09983722P0QME1B3KC8I" }, "HttpProperties": { "HttpName": "example.com" } }, "Type": "DNS_PRIVATE" } ] }

For more information, see Viewing a list of namespaces in the Amazon Cloud Map Developer Guide.

  • For API details, see ListNamespaces in Amazon CLI Command Reference.

The following code example shows how to use list-operations.

Amazon CLI

To list operations that meet the specified criteria

The following list-operations example lists operations that have a status of PENDING or SUCCESS.

aws servicediscovery list-operations \ --service-id srv-e4anhexample0004 \ --filters Name=STATUS,Condition=IN,Values=PENDING,SUCCESS

Output:

{ "Operations": [ { "Id": "76yy8ovhpdz0plmjzbsnqgnrqvpv2qdt-kexample", "Status": "SUCCESS" }, { "Id": "prysnyzpji3u2ciy45nke83x2zanl7yk-dexample", "Status": "SUCCESS" }, { "Id": "ko4ekftir7kzlbechsh7xvcdgcpk66gh-7example", "Status": "PENDING" } ] }

For more information, see What is Amazon Cloud Map? in the Amazon Cloud Map Developer Guide.

  • For API details, see ListOperations in Amazon CLI Command Reference.

The following code example shows how to use list-services.

Amazon CLI

To list services

The following list-services example lists services.

aws servicediscovery list-services

Output:

{ "Services": [ { "Id": "srv-p5zdwlg5uvvzjita", "Arn": "arn:aws:servicediscovery:us-west-2:123456789012:service/srv-p5zdwlg5uvvzjita", "Name": "myservice", "DnsConfig": { "RoutingPolicy": "MULTIVALUE", "DnsRecords": [ { "Type": "A", "TTL": 60 } ] }, "CreateDate": 1587081768.334 } ] }

For more information, see Viewing a list of services in the Amazon Cloud Map Developer Guide.

  • For API details, see ListServices in Amazon CLI Command Reference.

The following code example shows how to use list-tags-for-resource.

Amazon CLI

To list tags associated with the specified resource

The following list-tags-for-resource example lists tags for the specified resource.

aws servicediscovery list-tags-for-resource \ --resource-arn arn:aws:servicediscovery:us-west-2:123456789012:namespace/ns-e4anhexample0004

Output:

{ "Tags": [ { "Key": "Project", "Value": "Zeta" }, { "Key": "Department", "Value": "Engineering" } ] }

For more information, see Tagging your Amazon Cloud Map resources in the Amazon Cloud Map Developer Guide.

The following code example shows how to use register-instance.

Amazon CLI

To register a service instance

The following register-instance example registers a service instance.

aws servicediscovery register-instance \ --service-id srv-p5zdwlg5uvvzjita \ --instance-id myservice-53 \ --attributes=AWS_INSTANCE_IPV4=172.2.1.3,AWS_INSTANCE_PORT=808

Output:

{ "OperationId": "4yejorelbukcjzpnr6tlmrghsjwpngf4-k95yg2u7" }

To confirm that the operation succeeded, you can run get-operation. For more information, see get-operation .

For more information, see Registering instances in the Amazon Cloud Map Developer Guide.

The following code example shows how to use tag-resource.

Amazon CLI

To associate tags with the specified resource

The following tag-resource example associates a Department tag with the value Engineering with the specified namespace.

aws servicediscovery tag-resource \ --resource-arn arn:aws:servicediscovery:us-west-2:123456789012:namespace/ns-e4anhexample0004 \ --tags Key=Department, Value=Engineering

This command produces no output.

For more information, see Tagging your Amazon Cloud Map resources in the Amazon Cloud Map Developer Guide.

  • For API details, see TagResource in Amazon CLI Command Reference.

The following code example shows how to use untag-resource.

Amazon CLI

To remove tags from the specified resource

The following untag-resource example removes a Department tag from the specified namespace.

aws servicediscovery untag-resource \ --resource-arn arn:aws:servicediscovery:us-west-2:123456789012:namespace/ns-e4anhexample0004 \ --tags Key=Department, Value=Engineering

This command produces no output.

For more information, see Tagging your Amazon Cloud Map resources in the Amazon Cloud Map Developer Guide.

  • For API details, see UntagResource in Amazon CLI Command Reference.

The following code example shows how to use update-http-namespace.

Amazon CLI

To update an HTTP namespace

The following update-http-namespace example updates the specified HTTP namespace's description.

aws servicediscovery update-http-namespace \ --id ns-vh4nbmEXAMPLE \ --updater-request-id example-request-id \ --namespace Description="The updated namespace description."

Output:

{ "OperationId": "gv4g5meo7ndmeh4fqskygvk23d2fijwa-k9302yzd" }

To confirm that the operation succeeded, you can run get-operation. For more information, see get-operation .

For more information, see Amazon Cloud Map namespaces in the Amazon Cloud Map Developer Guide.

The following code example shows how to use update-instance-custom-health-status.

Amazon CLI

To update a custom health check

The following update-instance-custom-health-status example updates the status of the custom health check for the specified service and example service instance to HEALTHY.

aws servicediscovery update-instance-custom-health-status \ --service-id srv-e4anhexample0004 \ --instance-id example \ --status HEALTHY

This command produces no output.

For more information, see Amazon Cloud Map service health check configuration in the Amazon Cloud Map Developer Guide.

The following code example shows how to use update-private-dns-namespace.

Amazon CLI

To update a private DNS namespace

The following update-private-dns-namespace example updates the description of a private DNS namespace.

aws servicediscovery update-private-dns-namespace \ --id ns-bk3aEXAMPLE \ --updater-request-id example-private-request-id \ --namespace Description="The updated namespace description."

Output:

{ "OperationId": "gv4g5meo7ndmeh4fqskygvk23d2fijwa-k9302yzd" }

To confirm that the operation succeeded, you can run get-operation.

For more information, see Amazon Cloud Map namespaces in the Amazon Cloud Map Developer Guide.

The following code example shows how to use update-public-dns-namespace.

Amazon CLI

To update a public DNS namespace

The following update-public-dns-namespace example updates the description of a public DNS namespace.

aws servicediscovery update-public-dns-namespace \ --id ns-bk3aEXAMPLE \ --updater-request-id example-public-request-id \ --namespace Description="The updated namespace description."

Output:

{ "OperationId": "gv4g5meo7ndmeh4fqskygvk23d2fijwa-k9302yzd" }

To confirm that the operation succeeded, you can run get-operation.

For more information, see Amazon Cloud Map namespaces in the Amazon Cloud Map Developer Guide.

The following code example shows how to use update-service-attributes.

Amazon CLI

To update a service to add an attribute

The following update-service-attributes example updates the specified service to add a service attribute with a key Port and a value 80.

aws servicediscovery update-service-attributes \ --service-id srv-e4anhexample0004 \ --attributes Port=80

This command produces no output.

For more information, see Amazon Cloud Map services in the Amazon Cloud Map Developer Guide.

The following code example shows how to use update-service.

Amazon CLI

To update a service

The following update-service example updates a service to update the DnsConfig and HealthCheckConfig settings.

aws servicediscovery update-service \ --id srv-e4anhexample0004 \ --service "DnsConfig={DnsRecords=[{"Type"="A","TTL"=60}]},HealthCheckConfig={"Type"="HTTP","ResourcePath"="/","FailureThreshold"="2"}"

Output:

{ "OperationId": "gv4g5meo7ndmeh4fqskygvk23d2fijwa-k9302yzd" }

To confirm that the operation succeeded, you can run get-operation.

For more information about updating a service, see Updating an Amazon Cloud Map service in the Amazon Cloud Map Developer Guide.

  • For API details, see UpdateService in Amazon CLI Command Reference.