Skip to content

Commit 0af486b

Browse files
committed
cusotm metrics from cwagent
1 parent 0cab46d commit 0af486b

3 files changed

Lines changed: 59 additions & 24 deletions

File tree

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
files:
2-
"/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json":
3-
mode: "000644"
2+
"/opt/aws/amazon-cloudwatch-agent/bin/config.json":
3+
mode: "000600"
44
owner: root
55
group: root
66
content: |
@@ -11,6 +11,10 @@ files:
1111
},
1212
"metrics": {
1313
"namespace": "CWAgent",
14+
"append_dimensions": {
15+
"AutoScalingGroupName": "${aws:AutoScalingGroupName}",
16+
"InstanceId": "${aws:InstanceId}"
17+
},
1418
"metrics_collected": {
1519
"mem": {
1620
"measurement": [
@@ -35,29 +39,10 @@ files:
3539
"*"
3640
]
3741
}
38-
},
39-
"append_dimensions": {
40-
"AutoScalingGroupName": "${aws:AutoScalingGroupName}",
41-
"InstanceId": "${aws:InstanceId}"
4242
}
4343
}
4444
}
4545

46-
commands:
47-
01_install_cloudwatch_agent:
48-
command: |
49-
if ! command -v amazon-cloudwatch-agent-ctl &> /dev/null; then
50-
wget -q https://s3.amazonaws.com/amazoncloudwatch-agent/amazon_linux/amd64/latest/amazon-cloudwatch-agent.rpm
51-
rpm -U ./amazon-cloudwatch-agent.rpm
52-
rm -f ./amazon-cloudwatch-agent.rpm
53-
fi
54-
test: "[ ! -f /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl ]"
55-
56-
02_stop_cloudwatch_agent:
57-
command: |
58-
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl \
59-
-a fetch-config \
60-
-m ec2 \
61-
-c file:/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json \
62-
-s
63-
ignoreErrors: true
46+
container_commands:
47+
start_cloudwatch_agent:
48+
command: /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a append-config -m ec2 -s -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json

infrastructure/modules/iam/main.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,18 @@ resource "aws_iam_role_policy" "eb_ecr_access" {
163163
})
164164
}
165165

166+
# Attach AWS managed policy for CloudWatch Agent
167+
# This policy includes permissions for:
168+
# - cloudwatch:PutMetricData
169+
# - ec2:DescribeVolumes, ec2:DescribeTags
170+
# - logs:* (CreateLogGroup, CreateLogStream, PutLogEvents, etc.)
171+
# - xray:* (for traces)
172+
# - ssm:GetParameter (for configs in Parameter Store)
173+
resource "aws_iam_role_policy_attachment" "eb_cloudwatch_agent" {
174+
role = aws_iam_role.eb_ec2_role.name
175+
policy_arn = "arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy"
176+
}
177+
166178
# EC2 Instance Profile
167179
resource "aws_iam_instance_profile" "eb_ec2_profile" {
168180
name = "${var.project_name}-${var.environment}-eb-ec2-profile"

infrastructure/modules/monitoring/main.tf

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,44 @@ resource "aws_cloudwatch_dashboard" "main" {
2727
}
2828
}
2929
},
30+
# EC2 Memory Utilization (Custom Metric)
31+
{
32+
type = "metric"
33+
properties = {
34+
metrics = [
35+
["CWAgent", "MemoryUtilization", "AutoScalingGroupName", var.eb_autoscaling_group_name, { stat = "Average" }]
36+
]
37+
period = 300
38+
stat = "Average"
39+
region = var.aws_region
40+
title = "EC2 Memory Utilization (%)"
41+
yAxis = {
42+
left = {
43+
min = 0
44+
max = 100
45+
}
46+
}
47+
}
48+
},
49+
# EC2 Disk Utilization (Custom Metric)
50+
{
51+
type = "metric"
52+
properties = {
53+
metrics = [
54+
["CWAgent", "DiskUtilization", "AutoScalingGroupName", var.eb_autoscaling_group_name, { stat = "Average" }]
55+
]
56+
period = 300
57+
stat = "Average"
58+
region = var.aws_region
59+
title = "EC2 Disk Utilization (%)"
60+
yAxis = {
61+
left = {
62+
min = 0
63+
max = 100
64+
}
65+
}
66+
}
67+
},
3068
{
3169
type = "metric"
3270
properties = {

0 commit comments

Comments
 (0)