본문 바로가기
AWS

Terraform을 이용한 AWS Cloud Watch 모니터링 세팅

by DarrenH 2025. 10. 12.
반응형

AWS Profile 지정

aws configure --profile <프로필 이름>

AWS Access Key ID [None]: <Access-Key>
AWS Secret Access Key [None]: <Secrey-Key>
Default region name [None]: ap-northeast-2
Default output format [None]: json

각 EC2에 CloudWatch Agent 설치

## CloudWatch Agent 설치
sudo yum install amazon-cloudwatch-agent
## CloudWatch Agent 설정 마법사 실행
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard
  • 설정 파일 위치 /opt/aws/amazon-cloudwatch-agent/bin/config.json

설정 마법사 실행 로그

더보기

================================================================
= Welcome to the Amazon CloudWatch Agent Configuration Manager =
=                                                              =
= CloudWatch Agent allows you to collect metrics and logs from =
= your host and send them to CloudWatch. Additional CloudWatch =
= charges may apply.                                           =
================================================================
On which OS are you planning to use the agent?
1. linux
2. windows
3. darwin
default choice: [1]:
1
Trying to fetch the default region based on ec2 metadata...
I! imds retry client will retry 1 timesAre you using EC2 or On-Premises hosts?
1. EC2
2. On-Premises
default choice: [1]:
1
Which user are you planning to run the agent?
1. cwagent
2. root
3. others
default choice: [1]:
1
Do you want to turn on StatsD daemon?
1. yes
2. no
default choice: [1]:
2
Do you want to monitor metrics from CollectD? WARNING: CollectD must be installed or the Agent will fail to start
1. yes
2. no
default choice: [1]:
2
Do you want to monitor any host metrics? e.g. CPU, memory, etc.
1. yes
2. no
default choice: [1]:
1
Do you want to monitor cpu metrics per core?
1. yes
2. no
default choice: [1]:
1
Do you want to add ec2 dimensions (ImageId, InstanceId, InstanceType, AutoScalingGroupName) into all of your metrics if the info is available?
1. yes
2. no
default choice: [1]:
1
Do you want to aggregate ec2 dimensions (InstanceId)?
1. yes
2. no
default choice: [1]:
1
Would you like to collect your metrics at high resolution (sub-minute resolution)? This enables sub-minute resolution for all metrics, but you can customize for specific metrics in the output json file.
1. 1s
2. 10s
3. 30s
4. 60s
default choice: [4]:
4
Which default metrics config do you want?
1. Basic
2. Standard
3. Advanced
4. None
default choice: [1]:
1
Current config as follows:
{
    "agent": {
        "metrics_collection_interval": 60,
        "run_as_user": "cwagent"
    },
    "metrics": {
        "aggregation_dimensions": [
            [
                "InstanceId"
            ]
        ],
        "append_dimensions": {
            "AutoScalingGroupName": "${aws:AutoScalingGroupName}",
            "ImageId": "${aws:ImageId}",
            "InstanceId": "${aws:InstanceId}",
            "InstanceType": "${aws:InstanceType}"
        },
        "metrics_collected": {
            "disk": {
                "measurement": [
                    "used_percent"
                ],
                "metrics_collection_interval": 60,
                "resources": [
                    "*"
                ]
            },
            "mem": {
                "measurement": [
                    "mem_used_percent"
                ],
                "metrics_collection_interval": 60
            }
        }
    }
}
Are you satisfied with the above config? Note: it can be manually customized after the wizard completes to add additional items.
1. yes
2. no
default choice: [1]:
1
Do you have any existing CloudWatch Log Agent (http://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AgentReference.html) configuration file to import for migration?
1. yes
2. no
default choice: [2]:
2
Do you want to monitor any log files?
1. yes
2. no
default choice: [1]:
2
Do you want the CloudWatch agent to also retrieve X-ray traces?
1. yes
2. no
default choice: [1]:
2
Existing config JSON identified and copied to:  /opt/aws/amazon-cloudwatch-agent/etc/backup-configs
Saved config file to /opt/aws/amazon-cloudwatch-agent/bin/config.json successfully.
Current config as follows:
{
    "agent": {
        "metrics_collection_interval": 60,
        "run_as_user": "cwagent"
    },
    "metrics": {
        "aggregation_dimensions": [
            [
                "InstanceId"
            ]
        ],
        "append_dimensions": {
            "AutoScalingGroupName": "${aws:AutoScalingGroupName}",
            "ImageId": "${aws:ImageId}",
            "InstanceId": "${aws:InstanceId}",
            "InstanceType": "${aws:InstanceType}"
        },
        "metrics_collected": {
            "disk": {
                "measurement": [
                    "used_percent"
                ],
                "metrics_collection_interval": 60,
                "resources": [
                    "*"
                ]
            },
            "mem": {
                "measurement": [
                    "mem_used_percent"
                ],
                "metrics_collection_interval": 60
            }
        }
    }
}
Please check the above content of the config.
The config file is also located at /opt/aws/amazon-cloudwatch-agent/bin/config.json.
Edit it manually if needed.
Do you want to store the config in the SSM parameter store?
1. yes
2. no
default choice: [1]:
2
Program exits now.

terraform.tfvars 파일 생성 (각 프로젝트에 맞게)

cd ./terraform-cloudwatch

cat > terraform.tfvars << 'EOF'
# 프로젝트 설정
aws_region = "<리전>"
aws_profile = "<프로필 이름>"
project_name   = "<프로젝트 이름>"
dashboard_name = "<대시보드 이름>"

# EC2 인스턴스 정보
ec2_instances = [
  {
    instance_id = "<인스턴스 ID>"
    label       = "<LABEL>"
  },
  {
    instance_id = "<인스턴스 ID>"
    label       = "<LABEL>"
  }
]

# RDS Cluster 정보
rds_cluster = {
  cluster_id = "<클러스터 ID>"
  label       = "<LABEL>"
}


# RDS Writer 정보
rds_writer_instance = {
  instance_id = "<인스턴스 ID>"
  label       = "<LABEL>"
}

# RDS Reader 정보
rds_reader_instances = [
  {
    instance_id = "<인스턴스 ID>"
    label       = "<LABEL>"
  }
]


# CloudFront 배포 정보
cloudfront_distributions = [
  {
    distribution_id = "<distribution_id>"
    label           = "<LABEL>"
  },
  {
    distribution_id = "<distribution_id>"
    label           = "<LABEL>"
  }
]

# ElastiCache 설정
elasticache_cluster_id = "<elasticache_cluster_id>"

# ALB 설정
alb_name = "<app/~~>"
alb_target_groups = [
  {
    arn = "<arn>"
    label       = "<LABEL>"
  },
  {
    arn = "<arn>"
    label       = "<LABEL>"
  }
]


# 임계값 설정
unhealthy_host_threshold = 1
memory_usage_threshold = 80
cpu_usage_threshold = 50
disk_usage_threshold = 80
database_connections_threshold = 60
database_memory_threshold = 500000000
free_storage_threshold = 3000000000

# 태그
tags = {
  Project     = "<프로젝트명>"
  ManagedBy   = "terraform"
}
EOF

Terraform 실행

## 초기화
terraform init

## 계획 확인 (실제 반영 X)
terraform plan

## 적용 (현재 AWS Profile에 반영)
terraform apply

## 대시보드 URL 확인
terraform output dashboard_url

최종 대시보드

 

 

Github Repository

https://github.com/Darren4641/Terraform-AWS-CloudWatch/tree/main

 

GitHub - Darren4641/Terraform-AWS-CloudWatch: Terraform을 이용한 AWS Cloud Watch 모니터링 세팅

Terraform을 이용한 AWS Cloud Watch 모니터링 세팅. Contribute to Darren4641/Terraform-AWS-CloudWatch development by creating an account on GitHub.

github.com

 

반응형

'AWS' 카테고리의 다른 글

AWS Load Balancer Target Failover  (0) 2025.03.27
AWS Load Balancer Draining 상태  (0) 2025.03.27
Terraform을 이용한 VPC 생성  (0) 2025.03.25
ECR 스프링 부트 GitHub Action 설정하기  (0) 2023.08.20
AWS VPC 설정  (0) 2023.07.10