There are more AWS SDK examples available in the AWS Doc SDK Examples
CloudWatch Network Monitoring 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 CloudWatch Network Monitoring.
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-monitor.
- AWS CLI
-
Example 1: To create a network monitor with an aggregation period
The following
create-monitorexample creates a monitor namedExample_NetworkMonitorwith anaggregationPeriodset to30seconds. The initialstateof the monitor will beINACTIVEbecause there are no probes associated with it. The state changes toACTIVEonly when probes are added. You can use the update-monitor or create-probe commands to add probes to this monitor.aws networkmonitor create-monitor \ --monitor-nameExample_NetworkMonitor\ --aggregation-period30Output:
{ "monitorArn": "arn:aws:networkmonitor:region:111122223333:monitor/Example_NetworkMonitor", "monitorName": "Example_NetworkMonitor", "state": "INACTIVE", "aggregationPeriod": 30, "tags": {} }For more information, see How Amazon CloudWatch Network Monitor Works in the Amazon CloudWatch User Guide.
Example 2: To create a network monitor with a probe using TCP and also includes tags
The following
create-monitorexample creates a monitor namedExample_NetworkMonitor. The command also creates one probe that uses theICMPprotocol and includes tags. Since noaggregationPeriodis passed in the request,60seconds is set as the default. Thestateof the monitor with the probe will bePENDINGuntil the monitor isACTIVE. This might take several minutes, at which point thestatewill change toACTIVE, and you can start viewing CloudWatch metrics.aws networkmonitor create-monitor \ --monitor-nameExample_NetworkMonitor\ --probessourceArn=arn:aws:ec2:region:111122223333:subnet/subnet-id,destination=10.0.0.100,destinationPort=80,protocol=TCP,packetSize=56,probeTags={Name=Probe1}\ --tagsMonitor=Monitor1Output:
{ "monitorArn": "arn:aws:networkmonitor:region111122223333:monitor/Example_NetworkMonitor", "monitorName": "Example_NetworkMonitor", "state": "PENDING", "aggregationPeriod": 60, "tags": { "Monitor": "Monitor1" } }For more information, see How Amazon CloudWatch Network Monitor Works in the Amazon CloudWatch User Guide.
Example 3: To create a network monitor with a probe using ICMP and also includes tags
The following
create-monitorexample creates a monitor namedExample_NetworkMonitorwith anaggregationPeriodof30seconds. The command also creates one probe that uses theICMPprotocol and includes tags. Since noaggregationPeriodis passed in the request,60seconds is set as the default. Thestateof the monitor with the probe will bePENDINGuntil the monitor isACTIVE. This might take several minutes, at which point thestatewill change toACTIVE, and you can start viewing CloudWatch metrics.aws networkmonitor create-monitor \ --monitor-nameExample_NetworkMonitor\ --aggregation-period30\ --probessourceArn=arn:aws:ec2:region111122223333:subnet/subnet-id,destination=10.0.0.100,protocol=ICMP,packetSize=56,probeTags={Name=Probe1}\ --tagsMonitor=Monitor1Output:
{ "monitorArn": "arn:aws:networkmonitor:region:111122223333:monitor/Example_NetworkMonitor", "monitorName": "Example_NetworkMonitor", "state": "PENDING", "aggregationPeriod": 30, "tags": { "Monitor": "Monitor1" } }For more information, see How Amazon CloudWatch Network Monitor Works in the Amazon CloudWatch User Guide.
-
For API details, see CreateMonitor
in AWS CLI Command Reference.
-
The following code example shows how to use create-probe.
- AWS CLI
-
Example 1: To create a probe that uses TCP and add it to a network monitor
The following
create-probeexample creates a probe that uses theTCPprotocoland adds the probe to a monitor namedExample_NetworkMonitor. Once created, thestateof the monitor with the probe will bePENDINGuntil the monitor isACTIVE. This might take several minutes, at which point the state will change toACTIVE, and you can start viewing CloudWatch metrics.aws networkmonitor create-probe \ --monitor-nameExample_NetworkMonitor\ --probesourceArn=arn:aws:ec2:region:111122223333:subnet/subnet-id,destination=10.0.0.100,destinationPort=80,protocol=TCP,packetSize=56,tags={Name=Probe1}Output:
{ "probeId": "probe-12345", "probeArn": "arn:aws:networkmonitor:region:111122223333:probe/probe-12345", "destination": "10.0.0.100", "destinationPort": 80, "packetSize": 56, "addressFamily": "IPV4", "vpcId": "vpc-12345", "state": "PENDING", "createdAt": "2024-03-29T12:41:57.314000-04:00", "modifiedAt": "2024-03-29T12:41:57.314000-04:00", "tags": { "Name": "Probe1" } }Example 2: To create a probe that uses probe using ICMP and add it to a network monitor
The following
create-probeexample creates a probe that uses theICMPprotocoland adds the probe to a monitor namedExample_NetworkMonitor. Once created, thestateof the monitor with the probe will bePENDINGuntil the monitor isACTIVE. This might take several minutes, at which point the state will change toACTIVE, and you can start viewing CloudWatch metrics.aws networkmonitor create-probe \ --monitor-nameExample_NetworkMonitor\ --probesourceArn=arn:aws:ec2:region:012345678910:subnet/subnet-id,destination=10.0.0.100,protocol=ICMP,packetSize=56,tags={Name=Probe1}Output:
{ "probeId": "probe-12345", "probeArn": "arn:aws:networkmonitor:region:111122223333:probe/probe-12345", "destination": "10.0.0.100", "packetSize": 56, "addressFamily": "IPV4", "vpcId": "vpc-12345", "state": "PENDING", "createdAt": "2024-03-29T12:44:02.452000-04:00", "modifiedAt": "2024-03-29T12:44:02.452000-04:00", "tags": { "Name": "Probe1" } }For more information, see How Amazon CloudWatch Network Monitor Works in the Amazon CloudWatch User Guide.
-
For API details, see CreateProbe
in AWS CLI Command Reference.
-
The following code example shows how to use delete-monitor.
- AWS CLI
-
To delete a monitor
The following
delete-monitorexample deletes a monitor namedExample_NetworkMonitor.aws networkmonitor delete-monitor \ --monitor-nameExample_NetworkMonitorThis command produces no output.
For more information, see How Amazon CloudWatch Network Monitor Works in the Amazon CloudWatch User Guide.
-
For API details, see DeleteMonitor
in AWS CLI Command Reference.
-
The following code example shows how to use delete-probe.
- AWS CLI
-
To delete a probe
The following
delete-probeexample deletes a probe with the IDprobe-12345from a network monitor namedExample_NetworkMonitor.aws networkmonitor delete-probe \ --monitor-nameExample_NetworkMonitor\ --probe-idprobe-12345This command produces no output.
For more information, see How Amazon CloudWatch Network Monitor Works in the Amazon CloudWatch User Guide.
-
For API details, see DeleteProbe
in AWS CLI Command Reference.
-
The following code example shows how to use get-monitor.
- AWS CLI
-
To get monitor information
The following
get-monitorexample gets information about a monitor namedExample_NetworkMonitor.aws networkmonitor get-monitor \ --monitor-nameExample_NetworkMonitorOutput:
{ "monitorArn": "arn:aws:networkmonitor:region:012345678910:monitor/Example_NetworkMonitor", "monitorName": "Example_NetworkMonitor", "state": "ACTIVE", "aggregationPeriod": 60, "tags": {}, "probes": [], "createdAt": "2024-04-01T17:58:07.211000-04:00", "modifiedAt": "2024-04-01T17:58:07.211000-04:00" }For more information, see How Amazon CloudWatch Network Monitor Works in the Amazon CloudWatch User Guide.
-
For API details, see GetMonitor
in AWS CLI Command Reference.
-
The following code example shows how to use get-probe.
- AWS CLI
-
To view probe details
The following
get-probeexample returns details about a probe with theprobeIDprobe-12345that's associated with a monitor namedExample_NetworkMonitor.aws networkmonitor get-probe \ --monitor-nameExample_NetworkMonitor\ --probe-idprobe-12345Output:
{ "probeId": "probe-12345", "probeArn": "arn:aws:networkmonitor:region:012345678910:probe/probe-12345", "sourceArn": "arn:aws:ec2:region:012345678910:subnet/subnet-12345", "destination": "10.0.0.100", "destinationPort": 80, "protocol": "TCP", "packetSize": 56, "addressFamily": "IPV4", "vpcId": "vpc-12345", "state": "ACTIVE", "createdAt": "2024-03-29T12:41:57.314000-04:00", "modifiedAt": "2024-03-29T12:42:28.610000-04:00", "tags": { "Name": "Probe1" } }For more information, see How Amazon CloudWatch Network Monitor Works in the Amazon CloudWatch User Guide.
-
For API details, see GetProbe
in AWS CLI Command Reference.
-
The following code example shows how to use list-monitors.
- AWS CLI
-
Example 1: To list all monitors (single monitor)
The following
list-monitorsexample returns a list of only a single monitor. The monitor'sstateisACTIVEand it has anaggregationPeriodof 60 seconds.aws networkmonitor list-monitorsOutput:
{ "monitors": [{ "monitorArn": "arn:aws:networkmonitor:region:012345678910:monitor/Example_NetworkMonitor", "monitorName": "Example_NetworkMonitor", "state": "ACTIVE", "aggregationPeriod": 60, "tags": { "Monitor": "Monitor1" } } ] }For more information, see How Amazon CloudWatch Network Monitor Works in the Amazon CloudWatch User Guide.
Example 2: To list all monitors (multiple monitors)
The following
list-monitorsexample returns a list of three monitors. Thestateof one monitor isACTIVEand generating CloudWatch metrics. The states of the other two monitors areINACTIVEand not generating CloudWatch metrics. All three monitors use anaggregationPeriodof 60 seconds.aws networkmonitor list-monitorsOutput:
{ "monitors": [ { "monitorArn": "arn:aws:networkmonitor:us-east-1:111122223333:monitor/Example_NetworkMonitor", "monitorName": "Example_NetworkMonitor", "state": "INACTIVE", "aggregationPeriod": 60, "tags": {} }, { "monitorArn": "arn:aws:networkmonitor:us-east-1:111122223333:monitor/Example_NetworkMonitor2", "monitorName": "Example_NetworkMonitor2", "state": "ACTIVE", "aggregationPeriod": 60, "tags": { "Monitor": "Monitor1" } }, { "monitorArn": "arn:aws:networkmonitor:us-east-1:111122223333:monitor/TestNetworkMonitor_CLI", "monitorName": "TestNetworkMonitor_CLI", "state": "INACTIVE", "aggregationPeriod": 60, "tags": {} } ] }For more information, see How Amazon CloudWatch Network Monitor Works in the Amazon CloudWatch User Guide.
-
For API details, see ListMonitors
in AWS CLI Command Reference.
-
The following code example shows how to use list-tags-for-resource.
- AWS CLI
-
To list tags for a resource
The following
list-tags-for-resourceexample returns a list of the tags for a monitor namedExample_NetworkMonitor.aws networkmonitor list-tags-for-resource \ --resource-arnarn:aws:networkmonitor:region:012345678910:monitor/Example_NetworkMonitorOutput:
{ "tags": { "Environment": "Dev", "Application": "PetStore" } }For more information, see How Amazon CloudWatch Network Monitor Works in the Amazon CloudWatch User Guide.
-
For API details, see ListTagsForResource
in AWS CLI Command Reference.
-
The following code example shows how to use tag-resource.
- AWS CLI
-
To tag a resource
The following
tag-resourceexample tags a monitor namedExample_NetworkMonitorwithEnvironment=DevandApplication=PetStoretags.aws networkmonitor tag-resource \ --resource-arnarn:aws:networkmonitor:region:012345678910:monitor/Example_NetworkMonitor\ --tagsEnvironment=Dev,Application=PetStoreThis command produces no output.
For more information, see How Amazon CloudWatch Network Monitor Works in the Amazon CloudWatch User Guide.
-
For API details, see TagResource
in AWS CLI Command Reference.
-
The following code example shows how to use untag-resource.
- AWS CLI
-
To untag a resource
The following
untag-resourceexample removes atag-keysparameter with the key-value pair ofEnvironment Applicationfrom its association with a monitor namedExample_NetworkMonitor.aws networkmonitor untag-resource \ --resource-arnarn:aws:networkmonitor:region:012345678910:monitor/Example_NetworkMonitor\ --tag-keysEnvironmentApplicationThis command produces no output.
For more information, see How Amazon CloudWatch Network Monitor Works in the Amazon CloudWatch User Guide.
-
For API details, see UntagResource
in AWS CLI Command Reference.
-
The following code example shows how to use update-monitor.
- AWS CLI
-
To update a monitor
The following
update-monitorexample changes a monitor'saggregationPeriodfrom60seconds to30seconds.aws networkmonitor update-monitor \ --monitor-nameExample_NetworkMonitor\ --aggregation-period30Output:
{ "monitorArn": "arn:aws:networkmonitor:region:012345678910:monitor/Example_NetworkMonitor", "monitorName": "Example_NetworkMonitor", "state": "PENDING", "aggregationPeriod": 30, "tags": { "Monitor": "Monitor1" } }For more information, see How Amazon CloudWatch Network Monitor Works in the Amazon CloudWatch User Guide.
-
For API details, see UpdateMonitor
in AWS CLI Command Reference.
-
The following code example shows how to use update-probe.
- AWS CLI
-
To update a probe
The following
update-probeexample updates a probe's originaldestinationIP address and also updates thepacketSizeto60.aws networkmonitor update-probe \ --monitor-nameExample_NetworkMonitor\ --probe-idprobe-12345\ --destination10.0.0.150\ --packet-size60Output:
{ "probeId": "probe-12345", "probeArn": "arn:aws:networkmonitor:region:012345678910:probe/probe-12345", "sourceArn": "arn:aws:ec2:region:012345678910:subnet/subnet-12345", "destination": "10.0.0.150", "destinationPort": 80, "protocol": "TCP", "packetSize": 60, "addressFamily": "IPV4", "vpcId": "vpc-12345", "state": "PENDING", "createdAt": "2024-03-29T12:41:57.314000-04:00", "modifiedAt": "2024-03-29T13:52:23.115000-04:00", "tags": { "Name": "Probe1" } }For more information, see How Amazon CloudWatch Network Monitor Works in the Amazon CloudWatch User Guide.
-
For API details, see UpdateProbe
in AWS CLI Command Reference.
-