Skip to content

Commit 9299ace

Browse files
committed
[IT-4821] Integrate cloudwatch with linux docker product
This template provides system-level monitoring for general-purpose Docker hosts, allowing users to add their own container-specific logging as needed. Enhanced EC2 Docker product with CloudWatch agent integration. IAM Permissions: Added CloudWatchAgentServerPolicy to the instance role for CloudWatch agent access CloudWatch Log Groups: Created dedicated log group for: System logs (/aws/ec2/system/{StackName}) CloudWatch Agent Installation: Added automated installation and configuration of the CloudWatch agent with: System metrics collection (CPU, memory, disk) System log file monitoring (messages, secure, cloud-init) 60-second collection intervals Stack Outputs: Added helpful outputs showing log group names and CloudWatch console links Monitoring is enabled for the EC2 instance system-level metrics and logs, organized by stack name and instance ID for easy identification. Log group has a 30-day retention period to manage costs effectively.
1 parent c81c723 commit 9299ace

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

templates/ec2/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,26 @@ This reference architecture creates an AWS Service Catalog Portfolio called "Ser
44
* EC2 Linux with docker Integration: Provision an AWS Linux EC2 instance with docker and docker compose.
55
* Notebook Linux EC2 Instance with Jumpcloud Integration, which builds one EC2 instance using an Ubuntu Bionic-based Rstudio AMI.
66
* EC2 Windows with Jumpcloud Integration, which builds one EC2 instance using a Windows Server 2019 AMI.
7+
8+
## Logging
9+
These templates have been enhanced with AWS CloudWatch agent integration to monitor both system metrics and Docker container logs.
10+
11+
### CloudWatch Log Groups
12+
The template creates log groups automatically with system Metrics
13+
14+
The CloudWatch agent collects:
15+
- CPU usage (idle, iowait, user, system)
16+
- Memory usage percentage
17+
- Disk usage percentage
18+
19+
### Log Retention
20+
21+
All log groups are configured with a 30-day retention period to manage costs.
22+
23+
### Docker Container Logging
24+
25+
Each Docker container is configured with the `awslogs` logging driver to send logs directly to CloudWatch. The logs are organized by:
26+
- Instance ID as the log stream prefix
27+
- Container name as the log stream suffix
28+
29+
This provides clear separation and easy identification of logs from different containers and instances.

templates/ec2/sc-ec2-linux-docker.yaml

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ Parameters:
9292
MinValue: 30
9393
MaxValue: 5000
9494
Resources:
95+
# CloudWatch Log Groups for system monitoring
96+
SystemLogGroup:
97+
Type: AWS::Logs::LogGroup
98+
Properties:
99+
LogGroupName: !Sub '/aws/ec2/system/${AWS::StackName}'
100+
RetentionInDays: 30
101+
95102
TgwHubSecurityGroup:
96103
Type: AWS::EC2::SecurityGroup
97104
Metadata:
@@ -122,6 +129,7 @@ Resources:
122129
'Fn::Sub': '${AWS::Region}-get-role-policy-ReadAssumedRoleInformationPolicy'
123130
- "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
124131
- "arn:aws:iam::aws:policy/AWSQuickSetupPatchPolicyBaselineAccess" #For SSM patching
132+
- 'arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy' #For CloudWatch agent
125133
AssumeRolePolicyDocument:
126134
Version: '2012-10-17'
127135
Statement:
@@ -153,6 +161,8 @@ Resources:
153161
- cfn_hup_service
154162
SetEnv:
155163
- set_env_vars
164+
- install_cloudwatch_agent
165+
- configure_cloudwatch_agent
156166
cfn_hup_service:
157167
files:
158168
/etc/cfn/cfn-hup.conf:
@@ -208,6 +218,82 @@ Resources:
208218
AWS_REGION: !Ref AWS::Region
209219
STACK_NAME: !Ref AWS::StackName
210220
STACK_ID: !Ref AWS::StackId
221+
install_cloudwatch_agent:
222+
packages:
223+
yum:
224+
amazon-cloudwatch-agent: []
225+
configure_cloudwatch_agent:
226+
files:
227+
/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json:
228+
content: !Sub |
229+
{
230+
"agent": {
231+
"metrics_collection_interval": 60,
232+
"run_as_user": "cwagent"
233+
},
234+
"logs": {
235+
"logs_collected": {
236+
"files": {
237+
"collect_list": [
238+
{
239+
"file_path": "/var/log/messages",
240+
"log_group_name": "/aws/ec2/system/${AWS::StackName}",
241+
"log_stream_name": "{instance_id}/messages",
242+
"timezone": "UTC"
243+
},
244+
{
245+
"file_path": "/var/log/secure",
246+
"log_group_name": "/aws/ec2/system/${AWS::StackName}",
247+
"log_stream_name": "{instance_id}/secure",
248+
"timezone": "UTC"
249+
},
250+
{
251+
"file_path": "/var/log/cloud-init-output.log",
252+
"log_group_name": "/aws/ec2/system/${AWS::StackName}",
253+
"log_stream_name": "{instance_id}/cloud-init",
254+
"timezone": "UTC"
255+
}
256+
]
257+
}
258+
}
259+
},
260+
"metrics": {
261+
"namespace": "CWAgent",
262+
"metrics_collected": {
263+
"cpu": {
264+
"measurement": [
265+
"cpu_usage_idle",
266+
"cpu_usage_iowait",
267+
"cpu_usage_user",
268+
"cpu_usage_system"
269+
],
270+
"metrics_collection_interval": 60,
271+
"totalcpu": false
272+
},
273+
"disk": {
274+
"measurement": [
275+
"used_percent"
276+
],
277+
"metrics_collection_interval": 60,
278+
"resources": [
279+
"*"
280+
]
281+
},
282+
"mem": {
283+
"measurement": [
284+
"mem_used_percent"
285+
],
286+
"metrics_collection_interval": 60
287+
}
288+
}
289+
}
290+
}
291+
mode: "000644"
292+
owner: "root"
293+
group: "root"
294+
commands:
295+
start_cloudwatch_agent:
296+
command: "/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json"
211297
Properties:
212298
ImageId: !FindInMap [AMIs, !Ref AMI, AmiId]
213299
InstanceType: !Ref 'EC2InstanceType'
@@ -268,3 +354,10 @@ Outputs:
268354
Documentation:
269355
Description: 'Service Catalog Documentation'
270356
Value: "https://help.sc.sageit.org/sc/Service-Catalog-Provisioning.938836322.html"
357+
CloudWatchLogGroups:
358+
Description: 'CloudWatch Log Groups for monitoring'
359+
Value: !Sub |
360+
System Logs: /aws/ec2/system/${AWS::StackName}
361+
CloudWatchConsoleURI:
362+
Description: 'CloudWatch Logs Console'
363+
Value: !Sub "https://${AWS::Region}.console.aws.amazon.com/cloudwatch/home?region=${AWS::Region}#logsV2:log-groups"

0 commit comments

Comments
 (0)