The cloudwatch metricset of aws module allows you to monitor various services on
AWS. `cloudwatch` metricset fetches metrics from given namespace periodically
by calling `GetMetricData` api.

[float]
=== AWS Permissions
Some specific AWS permissions are required for IAM user to collect AWS Cloudwatch metrics.
----
ec2:DescribeRegions
cloudwatch:GetMetricData
cloudwatch:ListMetrics
tag:getResources
sts:GetCallerIdentity
iam:ListAccountAliases
----

[float]
=== Metricset-specific configuration notes
* *namespace*: The namespace used by ListMetrics API to filter against.
For example, AWS/EC2, AWS/S3. If wildcard * is given for namespace, metrics
from all namespaces will be collected automatically.
* *name*: The name of the metric to filter against. For example, CPUUtilization for EC2 instance.
* *dimensions*: The dimensions to filter against. For example, InstanceId=i-123.
* *resource_type*: The constraints on the resources that you want returned.
The format of each resource type is service[:resourceType].
For example, specifying a resource type of ec2 returns all Amazon EC2 resources
(which includes EC2 instances). Specifying a resource type of ec2:instance returns
only EC2 instances.
* *statistic*: Statistics are metric data aggregations over specified periods of time.
By default, statistic includes Average, Sum, Count, Maximum and Minimum.
* *tags*: The tags to filter against. If tags are given in config, then only
collect metrics from resources that have tag key and tag value matches the filter.
For example, if tags parameter is given as `Organization=Engineering` under
`AWS/ELB` namespace, then only collect metrics from ELBs with tag name equals to
`Organization` and tag value equals to `Engineering`.

[float]
=== Configuration examples
To be more focused on `cloudwatch` metricset use cases, the examples below do
not include configurations on AWS credentials.
Please see <<aws-credentials-config,AWS credentials options>> for more details on setting AWS credentials
in configurations in order for this metricset to make proper AWS API calls.

[float]
==== Example 1
[source,yaml]
----
- module: aws
  period: 300s
  metricsets:
    - cloudwatch
  metrics:
    - namespace: AWS/EBS <1>
    - namespace: AWS/ELB <2>
      resource_type: elasticloadbalancing
      tags:
        - key: "Organization"
          value: "Engineering"
    - namespace: AWS/EC2 <3>
      name: CPUUtilization
      statistic: ["Average"]
      dimensions:
        - name: InstanceId
          value: i-0686946e22cf9494a
----

<1> Users can configure the `cloudwatch` metricset to collect all metrics from one
specific namespace, such as `AWS/EBS`.

<2> `cloudwatch` metricset also has the ability to collect tags from AWS resources.
If user specify `resource_type`, then tags will be collected and stored
as a part of the event. Please see https://docs.aws.amazon.com/resourcegroupstagging/latest/APIReference/API_GetResources.html[AWS API GetResources]
for more details about `resource_type`.

<3> If users knows exactly what are the cloudwatch metrics they want to collect,
this configuration format can be used. `namespace` and `metricname` need to be
specified and `dimensions` can be used to filter cloudwatch metrics. Please see
https://docs.aws.amazon.com/cli/latest/reference/cloudwatch/list-metrics.html[AWS List Metrics]
for more details.

[float]
==== Example 2
[source,yaml]
----
- module: aws
  period: 300s
  metricsets:
    - cloudwatch
  metrics:
    - namespace: "*"
----
With this config, metrics from all namespaces will be collected from Cloudwatch.
The limitation here is the collection period for all namespaces are all set to
be the same, which in this case is 300 second. This will cause extra costs for
API calls or data loss.
For example, metrics from namespace AWS/Usage are sent to Cloudwatch every 1
minute. With the collection period equals to 300 seconds, data points in between
will get lost. Metrics from namespace AWS/Billing are sent to Cloudwatch every
several hours. By querying from AWS/Billing namespace every 300 seconds,
additional costs will occur.

[float]
=== More examples
With the configuration below, users will be able to collect cloudwatch metrics
from EBS, ELB and EC2 without tag information.

[source,yaml]
----
- module: aws
  period: 300s
  metricsets:
    - cloudwatch
  metrics:
    - namespace: AWS/EBS
    - namespace: AWS/ELB
    - namespace: AWS/EC2
----

With the configuration below, users will be able to collect cloudwatch metrics
from EBS, ELB and EC2 with tags from these services.

[source,yaml]
----
- module: aws
  period: 300s
  metricsets:
    - cloudwatch
  metrics:
    - namespace: AWS/EBS
      resource_type: ebs
    - namespace: AWS/ELB
      resource_type: elasticloadbalancing
    - namespace: AWS/EC2
      resource_type: ec2:instance
----

With the configuration below, users will be able to collect specific cloudwatch
metrics. For example CPUUtilization metric(average) from EC2 instance i-123 and NetworkIn
metric(average) from EC2 instance i-456.
[source,yaml]
----
- module: aws
  period: 300s
  metricsets:
    - cloudwatch
  metrics:
    - namespace: AWS/EC2
      name: ["CPUUtilization"]
      resource_type: ec2:instance
      dimensions:
        - name: InstanceId
          value: i-123
      statistic: ["Average"]
    - namespace: AWS/EC2
      name: ["NetworkIn"]
      dimensions:
        - name: InstanceId
          value: i-456
      statistic: ["Average"]
----


With the configuration below, user can filter out only `LoadBalacer` and `TargetGroup` dimension
metircs with the metric name `UnHealthyHostCount`, `LoadBalacer` and `TargetGroup` value could
be any.

[source,yaml]
----
- module: aws
  period: 300s
  metricsets:
    - cloudwatch
  metrics:
    - namespace: AWS/ApplicationELB
      statistic: ['Maximum']
      name: ['UnHealthyHostCount']
      dimensions:
        - name: LoadBalancer
          value: "*"
        - name: TargetGroup
          value: "*"
      tags.resource_type_filter: elasticloadbalancing
----
