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-monitor
example creates a monitor namedExample_NetworkMonitor
with anaggregationPeriod
set to30
seconds. The initialstate
of the monitor will beINACTIVE
because there are no probes associated with it. The state changes toACTIVE
only 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-name
Example_NetworkMonitor
\ --aggregation-period30
Output:
{ "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-monitor
example creates a monitor namedExample_NetworkMonitor
. The command also creates one probe that uses theICMP
protocol and includes tags. Since noaggregationPeriod
is passed in the request,60
seconds is set as the default. Thestate
of the monitor with the probe will bePENDING
until the monitor isACTIVE
. This might take several minutes, at which point thestate
will change toACTIVE
, and you can start viewing CloudWatch metrics.aws networkmonitor create-monitor \ --monitor-name
Example_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=Monitor1
Output:
{ "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-monitor
example creates a monitor namedExample_NetworkMonitor
with anaggregationPeriod
of30
seconds. The command also creates one probe that uses theICMP
protocol and includes tags. Since noaggregationPeriod
is passed in the request,60
seconds is set as the default. Thestate
of the monitor with the probe will bePENDING
until the monitor isACTIVE
. This might take several minutes, at which point thestate
will change toACTIVE
, and you can start viewing CloudWatch metrics.aws networkmonitor create-monitor \ --monitor-name
Example_NetworkMonitor
\ --aggregation-period30
\ --probessourceArn=arn:aws:ec2:region111122223333:subnet/subnet-id,destination=10.0.0.100,protocol=ICMP,packetSize=56,probeTags={Name=Probe1}
\ --tagsMonitor=Monitor1
Output:
{ "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-probe
example creates a probe that uses theTCP
protocol
and adds the probe to a monitor namedExample_NetworkMonitor
. Once created, thestate
of the monitor with the probe will bePENDING
until 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-name
Example_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-probe
example creates a probe that uses theICMP
protocol
and adds the probe to a monitor namedExample_NetworkMonitor
. Once created, thestate
of the monitor with the probe will bePENDING
until 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-name
Example_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-monitor
example deletes a monitor namedExample_NetworkMonitor
.aws networkmonitor delete-monitor \ --monitor-name
Example_NetworkMonitor
This 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-probe
example deletes a probe with the IDprobe-12345
from a network monitor namedExample_NetworkMonitor
.aws networkmonitor delete-probe \ --monitor-name
Example_NetworkMonitor
\ --probe-idprobe-12345
This 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-monitor
example gets information about a monitor namedExample_NetworkMonitor
.aws networkmonitor get-monitor \ --monitor-name
Example_NetworkMonitor
Output:
{ "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-probe
example returns details about a probe with theprobeID
probe-12345
that's associated with a monitor namedExample_NetworkMonitor
.aws networkmonitor get-probe \ --monitor-name
Example_NetworkMonitor
\ --probe-idprobe-12345
Output:
{ "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-monitors
example returns a list of only a single monitor. The monitor'sstate
isACTIVE
and it has anaggregationPeriod
of 60 seconds.aws networkmonitor list-monitors
Output:
{ "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-monitors
example returns a list of three monitors. Thestate
of one monitor isACTIVE
and generating CloudWatch metrics. The states of the other two monitors areINACTIVE
and not generating CloudWatch metrics. All three monitors use anaggregationPeriod
of 60 seconds.aws networkmonitor list-monitors
Output:
{ "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-resource
example returns a list of the tags for a monitor namedExample_NetworkMonitor
.aws networkmonitor list-tags-for-resource \ --resource-arn
arn:aws:networkmonitor:region:012345678910:monitor/Example_NetworkMonitor
Output:
{ "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-resource
example tags a monitor namedExample_NetworkMonitor
withEnvironment=Dev
andApplication=PetStore
tags.aws networkmonitor tag-resource \ --resource-arn
arn:aws:networkmonitor:region:012345678910:monitor/Example_NetworkMonitor
\ --tagsEnvironment=Dev,Application=PetStore
This 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-resource
example removes atag-keys
parameter with the key-value pair ofEnvironment Application
from its association with a monitor namedExample_NetworkMonitor
.aws networkmonitor untag-resource \ --resource-arn
arn:aws:networkmonitor:region:012345678910:monitor/Example_NetworkMonitor
\ --tag-keysEnvironment
Application
This 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-monitor
example changes a monitor'saggregationPeriod
from60
seconds to30
seconds.aws networkmonitor update-monitor \ --monitor-name
Example_NetworkMonitor
\ --aggregation-period30
Output:
{ "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-probe
example updates a probe's originaldestination
IP address and also updates thepacketSize
to60
.aws networkmonitor update-probe \ --monitor-name
Example_NetworkMonitor
\ --probe-idprobe-12345
\ --destination10.0.0.150
\ --packet-size60
Output:
{ "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.
-