AWS Cloud Map examples using AWS CLI - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

AWS Cloud Map examples using AWS CLI

The following code examples show you how to perform actions and implement common scenarios by using the AWS Command Line Interface with AWS 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-private-dns-namespace.

AWS 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 AWS Cloud Map Developer Guide.

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

AWS 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 AWS Cloud Map Developer Guide.

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

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

AWS 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 AWS Cloud Map Developer Guide.

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

AWS 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 AWS Cloud Map Developer Guide.

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

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

AWS 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 AWS Cloud Map Developer Guide.

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

AWS 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" } } ] }

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

AWS CLI

To get the result of an operation

The following get-operation example gets the result of an 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 API details, see GetOperation in AWS CLI Command Reference.

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

AWS 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 AWS Cloud Map Developer Guide.

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

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

AWS 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 AWS Cloud Map Developer Guide.

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

AWS 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 AWS Cloud Map Developer Guide.

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

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

AWS 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 AWS Cloud Map Developer Guide.