Question to ChatGPT: write terraform code to install cloudwatch agent on ubuntu machines and add custom metrics such as RAM utilization and disk space remaining and publish to a cloudwatch dashboard
# Install the CloudWatch Agent on the Ubuntu machine
resource "aws_ssm_document" "cloudwatch_agent_install" {
name = "cloudwatch-agent-install"
document_type = "Command"
content = <<DOC
#! /bin/bash
cd /tmp
curl https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb -O
sudo dpkg -i -E ./amazon-cloudwatch-agent.deb
sudo systemctl start amazon-cloudwatch-agent
DOC
}
resource "aws_ssm_document" "cloudwatch_agent_install_document" {
name = "cloudwatch-agent-install"
document_type = "Automation"
content = <<DOC
{
"schemaVersion": "1.2",
"description": "Install the CloudWatch Agent",
"parameters": {},
"mainSteps": [
{
"name": "install",
"action": "aws:runShellScript",
"inputs": {
"runCommand": [
"${aws_ssm_document.cloudwatch_agent_install.content}"
]
}
}
]
}
DOC
}
resource "aws_ssm_maintenance_window_task" "cloudwatch_agent_install_task" {
window_id = "${aws_ssm_maintenance_window.my_window.id}"
task_type = "AUTOMATION"
task_arn = "${aws_ssm_document.cloudwatch_agent_install_document.arn}"
priority = 1
service_role_arn = "${aws_iam_role.ssm_role.arn}"
max_concurrency = "1"
max_errors = "1"
target_locations = ["LOCAL"]
}
# Configure the CloudWatch Agent to send custom metrics for RAM utilization and disk space remaining
resource "aws_cloudwatch_log_group" "my_log_group" {
name = "cloudwatch-agent-log-group"
}
resource "aws_cloudwatch_log_stream" "my_log_stream" {
name = "cloudwatch-agent-log-stream"
log_group_name = "${aws_cloudwatch_log_group.my_log_group.name}"
}
resource "aws_cloudwatch_agent_policy" "my_policy" {
name = "cloudwatch-agent-policy"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"cloudwatch:PutMetricData",
"cloudwatch:GetMetricData",
"cloudwatch:ListMetrics"
],
"Resource": "*"
}
]
}
EOF
}
resource "aws_iam