Can I create a seperate metric for each task? - amazon-web-services

I want to create a separate custom metric for each ECS task that I will run when AutoScaling automatically will be creating more tasks. Is there a way to easily implement this using Cloudformation or maybe something similar? Maybe automatically creating a separate log group with a separate metric filter for each task. Or only a metric filter but assigning it to a log stream of the created task.

If the custom metric is published by your task, then you can just start publishing the metrics directly. https://aws.amazon.com/premiumsupport/knowledge-center/cloudwatch-custom-metrics/
You would want to use the dimension of the task identifier. You would get that from the metadata. https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint.html
So you would get something like. Note I haven't tried the dimension type of taskArn which might cause a concern, but AWS already publishes cloudwatch events with this field.
aws cloudwatch put-metric-data --metric-name petro_custom_1 --dimensions taskArn=$TaskARN --namespace "Custom" --value $Value

Related

how to see powertools custom metrics in Cloudwatch from ECS?

I have an ECS container that is deployed by the Docker image and I have used aws_powertools for logging and creating the metrics in Cloudwatch. However, I can only see my loggings and printed out metrics. But in Cloudwatch my custom metrics do not show up automatically as they do normally in Lambda usage.
So, how can I solve this situation, or are there any possibilities to get metrics from ECS container and put them into CW?
this how I create metrics:
metrics = Metrics(service="servicename", namespace="namespace")
metrics.add_metric(
name="name",
unit=MetricUnit.Seconds,
value=execution_duration,
)
serialized_metric = metrics.serialize_metric_set()
metrics.clear_metrics()
print(json.dumps(serialized_metric, separators=(",", ":")))
Thank you in advance!

How to find which cloudwatch alarms uses custom metrics and which uses default metrics?

I have around 100+ cloudwatch alarms.
I want to find which alarms use custom metrics and which one uses default metrics?
I need to find such a list so that I can convert default metric alarms into custom metrics.
Note:- the alarms have been created by CDK.
Is there a way I can use AWS CLI and find out this?

Regular expressions for CloudWatch alarms

I have microservice that sends some custom metrics to AWS CloudWatch. Metric name consists of package name and some other data. For example gauge.com.example.test.time and gauge.com.example.test2.time and so on
Now I need to create some alarms based on this metrics. Is it possible to specify some reqular expression in metric name field when you create CloudWatch alarm instead of manual creation of separate alarm for each metric?
I tried such things: gauge.com.example..time gauge.com.example.*.time gauge.com.example.(\w).time and many other things but without success.
It is now possible to create aws alarms based on a Metric Math Expression.
https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Create-alarm-on-metric-math-expression.html
Not possible. From Creating Amazon CloudWatch Alarms
You can create a CloudWatch alarm that watches a single metric.

CloudWatch custom metrics not working as expected

I had already created 7 other metrics based on some log files I send to CloudWatch with no problems.
Some time ago we had a problem with MongoDB connection, and I identified that through logs, so I'd like to create a Metric, so that I can create an Alarm based on it. I did create the Metric, but (of course) there are no data being fed into that Metic, because no more "MongoError" messages exists.
But does that also mean that I can't even access the Metric to create the Alarm? Because this is what is happening right now. The Metric cannot be seen anywhere, only in the "Filters" section of the Logs, which won't allow me to create Alarms or create graphics or anything.
I have already posted this on AWS forums but that usually doesn't help.
An Amazon CloudWatch custom metric is only created when data is stored against the custom metric. Therefore, you'll need to push a data value to make appear and then you will be able to create an alarm.
You can push some data to CloudWatch with the AWS Command-Line Interface (CLI), eg:
aws cloudwatch put-metric-data --namespace MongoDB --metric-name errors --value 0

Stopping EC2 instance when custom cloudwatch metric passes limit

I'm trying to find a way to make an Amazon EC2 instance stop automatically when a certain custom metric on CloudWatch passes a limit. So far if I've understood correctly based on these articles:
Discussion Forum: Custom Metric EC2 Action
CloudWatch Documentation: Create Alarms to Stop, Terminate, Reboot, or Recover an Instance
This will only work if the metric is defined as follows:
Tied to certain instance
With type of System/Linux
However in my case I have a custom metric that is actually not instance-related but "global" and if a certain limit is passed, I would need to stop all instances, no matter from which instance the limiting log is received.
Does anybody know if there is way to make this work? What I'd need is some way to make CloudWatch work like this:
If arbitrary custom metric value passes a certain limit -> stop defined instances not tied to the metric itself.
The main problem is that the EC2 option is greyed out as the metric is not tied to certain EC2 instance and I'm not sure if there's any way to do this without actually making the metric itself certain instance related.
Have the custom CloudWatch metric post alerts to an SNS topic.
Have the SNS topic trigger a Lambda function that shuts down your EC2 instances via a call to the AWS API.