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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# 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 |