Skip to content

Is there a way to confirm if multiple EC2 instances are running a CW Agent?

0

I have multiple EC2 instances, and I wonder if I can confirm which one of these are running CloudWatch Agents, even better if I could export JSON or file containing these.

I am not sure if this is possible, but if it is how can it be done?

I also want to note I looked at SSM Command but I only see it allowing me to do this by specifying tags in the example. Is there a way I can just use this command to check all my EC2 instances at once to determine which one is running CloudWatch agents?

2 Answers
0

You can confirm which EC2 instances are running the CloudWatch agent using several approaches:

  1. Check the CloudWatch agent service status directly on each instance: You can use AWS Systems Manager Run Command to execute the following command across multiple instances:
sudo systemctl status amazon-cloudwatch-agent

This will show if the agent is running, stopped, or not installed.

  1. Use AWS PowerShell to query instances: You can use PowerShell to query EC2 instances and filter them based on specific criteria. For example:
$InstanceParams = @{
Filter = @(
@{'Name' = 'instance-state-name';'Values' = @("running","stopped")}
)
}
$result = Get-EC2Instance @InstanceParams

You could then run a command against these instances to check the CloudWatch agent status.

  1. Use AWS CloudFormation to deploy and verify: If you're deploying the CloudWatch agent at scale, you can use CloudFormation to install the agent and track which instances have it installed. The CloudFormation template can target instances by:
  • Specific instance IDs (comma-delimited list)
  • Instance tags (specify TagKey and TagValue)
  • Auto Scaling groups (using the tag key aws:autoscaling:groupName)
  1. Systems Manager Run Command without tags: While you mentioned seeing examples with tags, you can also run commands against all managed instances without specifying tags. In the AWS Systems Manager console, when creating a Run Command, you can select "Choose instances manually" and then select all the instances you want to check.

For exporting the results to JSON, you can configure the Run Command to store command output in an S3 bucket, which would give you a file containing the status of the CloudWatch agent on each instance.
Sources
Logging EC2 Instances Bash Commands with CloudWatch Logs | AWS re:Post
Use DescribeInstances with an AWS SDK or CLI - AWS SDK Code Examples
CloudWatch solution: Amazon EC2 health - Amazon CloudWatch

answered 9 months ago

AWS
EXPERT

reviewed 9 months ago

  • Can you give me example of how to run without tags for run command ?

0

Hello.

Use the Tag Editor to set the same tags for all EC2 instances.
The Tag Editor allows you to set tags for EC2 instances in a region in bulk.
https://docs.aws.amazon.com/tag-editor/latest/userguide/find-resources-to-tag.html

After that, try using SSM Run Command to find the EC2 instance where the CloudWatch Agent is running.
If you select AWS-RunShellScript as the command document, you can run commands on an EC2 instance running the Linux OS.
https://docs.aws.amazon.com/systems-manager/latest/userguide/running-commands-console.html

EXPERT

answered 9 months ago

AWS
EXPERT

reviewed 9 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.