https://docs.aws.amazon.com/ko_kr/awscloudtrail/latest/userguide/cloudtrail-user-guide.html
CloudTrail์ AWS์์ ์ ๊ณตํ๋ ์๋น์ค๋ก, AWS ๊ณ์ ์ ๊ด๋ฆฌ, ๊ท์ ์ค์, ์ด์ ๋ฐ ์ํ ๊ฐ์ฌ๋ฅผ ์ง์ํฉ๋๋ค. ์ด ์๋น์ค๋ AWS์์ ๋ฐ์ํ ๋ชจ๋ ์์ ์ ๊ธฐ๋กํ๊ณ ์ถ์ ํ์ฌ ๊ด๋ฆฌ์๋ค์ด ์ด๋ฅผ ๊ฒํ ํ ์ ์๊ฒ ํฉ๋๋ค.
์ฌ์ฉ์, ์ญํ ๋ฐ ์๋น์ค๊ฐ AWS Management Console, AWS Command Line Interface (CLI), AWS SDK, API ๋ฑ์ ํตํด ์ํํ ๋ชจ๋ ์์ ์ ํฌํจํ์ฌ ์ด๋ฒคํธ๋ฅผ ๊ธฐ๋กํฉ๋๋ค. ์ด๋ฅผ ํตํด AWS์์ ์๋น์ค๋๋ ๋ชจ๋ ํ๋์ ๋ชจ์ผ๊ณ ๋ถ์ํ ์ ์์ผ๋ฉฐ, ์ด๋ฅผ ํตํด ๋ฆฌ์์ค ๋ณ๊ฒฝ ์ถ์ ๋ฑ์ ์ํํ ์ ์์ต๋๋ค.
<CloudTrail์ ์ธ ๊ฐ์ง ์ ํ์ ์ด๋ฒคํธ>
๊ด๋ฆฌ ์ด๋ฒคํธ :
๊ณ์ ๋ฆฌ์์ค์์ ์ํ๋๋ ๊ด๋ฆฌ ์์
์ ๋ํ ์ ๋ณด๋ฅผ ์ ๊ณตํฉ๋๋ค. ์๋ฅผ ๋ค์ด, EC2 ์ธ์คํด์ค ์์ฑ, S3 ๋ฒํท ์ญ์ ์ ๊ฐ์ ๊ด๋ฆฌ์ ์์
์ ์ถ์ ํฉ๋๋ค.
๋ฐ์ดํฐ ์ด๋ฒคํธ :
๋ฆฌ์์ค์์ ์ํ๋๋ ๋ฐ์ดํฐ ์์
์ ๋ํ ์ ๋ณด๋ฅผ ์ ๊ณตํฉ๋๋ค. ์๋ฅผ ๋ค์ด, S3 ๋ฒํท์์ ํ์ผ ์
๋ก๋, ๋ค์ด๋ก๋์ ๊ฐ์ ๋ฐ์ดํฐ ์์
์ ์ถ์ ํฉ๋๋ค.
Insights ์ด๋ฒคํธ :
๊ณ์ ์์ ๋ฐ์ํ ๋น์ ์์ ์ธ ํ๋์ ๋ํ ๊ธฐ๋ก์ ์ ๊ณตํฉ๋๋ค. ์๋ฅผ ๋ค์ด, ๊ณ์ ์ ๋ณด์ ์ค์ ๋ณ๊ฒฝ ๋๋ ๋น์ ์์ ์ธ ์ก์ธ์ค ์๋์ ๊ฐ์ ํ๋์ ์๋ณํฉ๋๋ค.
CloudWatch๋ AWS์์ ์ ๊ณตํ๋ ๊ด๋ฆฌ ์๋น์ค๋ก, AWS์ ์์๊ณผ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ชจ๋ํฐ๋งํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. ์ด ์๋น์ค๋ ๋ชจ๋ํฐ๋งํ๋ ๋ชจ๋ ์์์ ๋ก๊ทธ์ ์งํ ์ ๋ณด๋ฅผ ์์งํ๊ณ ์๊ฐํํ์ฌ ์ ๊ณตํฉ๋๋ค.
์งํ(Metrics) :
์ธ์ ์ด๋ค ํญ๋ชฉ์ ๊ฐ์ด ๋ฌด์์ด์๋์ง๋ฅผ ๊ธฐ๋กํ ๊ฐ์ผ๋ก, ์๋ฅผ ๋ค์ด, EC2 ์ธ์คํด์ค์ CPU ์ฌ์ฉ๋, ๋คํธ์ํฌ ์ ์ก๋, Auto Scaling ๊ทธ๋ฃน์ด ๊ด๋ฆฌํ๋ ์ธ์คํด์ค ์ ๋ฑ์ด ์์ต๋๋ค.
CloudWatch Logs :
๋ก๊ทธ ์ด๋ฒคํธ, ๋ก๊ทธ ์คํธ๋ฆผ, ๋ก๊ทธ ๊ทธ๋ฃน๊ณผ ๊ฐ์ ๊ฐ๋
๋ค๋ก ๊ตฌ์ฑ๋ฉ๋๋ค.
๋ก๊ทธ ์ด๋ฒคํธ(Log Events) :
๋ก๊ทธ๋ฅผ ๊ธฐ๋กํ๋ ์ ํ๋ฆฌ์ผ์ด์
์ด๋ ์์์์ ๊ธฐ๋ก๋ ๋ก๊ทธ์ ํ ์ค์ ์๋ฏธํฉ๋๋ค.
๋ก๊ทธ ์คํธ๋ฆผ(Log Streams) :
๋์ผํ ์์ค์์ ๊ธฐ๋ก๋ ๋ก๊ทธ ์ด๋ฒคํธ๋ค์ ์๊ฐ์์ผ๋ก ๋ชจ์๋ ์คํธ๋ฆผ์ ๋ํ๋
๋๋ค.
๋ก๊ทธ ๊ทธ๋ฃน(Log Groups) :
๋์ผํ ๋ณด์กด ๊ธฐ๊ฐ, ๋ชจ๋ํฐ๋ง ์ค์ , ์ก์ธ์ค ์ ์ด ์ค์ ๋ฑ์ ๊ณต์ ํ๋ ๋ก๊ทธ ์คํธ๋ฆผ์ ๊ทธ๋ฃน์ ์ ์ํฉ๋๋ค.
๋ํ, CloudWatch๋ฅผ ์ฌ์ฉํ๋ฉด ํน์ ๊ธฐ์ค์ด ์ถฉ์กฑ๋๋ฉด Amazon EC2 ์ธ์คํด์ค๋ฅผ ์ค์ง, ์์ ๋๋ ์ข ๋ฃํ๋๋ก ๊ฒฝ๋ณด ์์ ์ ๊ตฌ์ฑํ ์ ์์ต๋๋ค.
๋ชฉ์ : AWS ๊ณ์ ์ ๊ด๋ฆฌ, ๊ท์ ์ค์, ์ด์ ๋ฐ ์ํ ๊ฐ์ฌ๋ฅผ ์ง์ํ๋ ์๋น์ค์ ๋๋ค.
์ฃผ์ ๊ธฐ๋ฅ:
AWS ๊ณ์ ์์ ๋ฐ์ํ ๋ชจ๋ ์์ ์ ๊ธฐ๋กํ๊ณ ์ถ์ ํฉ๋๋ค.
๊ด๋ฆฌ ์ด๋ฒคํธ, ๋ฐ์ดํฐ ์ด๋ฒคํธ, Insights ์ด๋ฒคํธ์ ๊ฐ์ด ์ธ ๊ฐ์ง ์ ํ์ ์ด๋ฒคํธ๋ฅผ ์ ๊ณตํฉ๋๋ค.
๊ด๋ฆฌ์ ์์ , ๋ฐ์ดํฐ ์์ , ๋น์ ์์ ์ธ ํ๋ ๋ฑ์ ์ด๋ฒคํธ๋ฅผ ๊ธฐ๋กํ๊ณ ๋ถ์ํ์ฌ ๋ณด์ ๋ฐ ์ด์์ ๊ฐํํฉ๋๋ค.
๋ชฉ์ : AWS์ ์์๊ณผ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ชจ๋ํฐ๋งํ๊ณ , ๋ก๊ทธ ๋ฐ ์งํ ์ ๋ณด๋ฅผ ์์งํ์ฌ ์ ๊ณตํ๋ ๊ด๋ฆฌ ์๋น์ค์ ๋๋ค.
์ฃผ์ ๊ธฐ๋ฅ:
๋ชจ๋ํฐ๋งํ๋ ์์์ ๋ก๊ทธ ๋ฐ ์งํ ์ ๋ณด๋ฅผ ์์งํ๊ณ ์๊ฐํํ์ฌ ์ ๊ณตํฉ๋๋ค.
์งํ๋ ์ด๋ค ํญ๋ชฉ์ ๊ฐ์ด ์ธ์ ์ด๋ค ๊ฒ์ด์๋์ง๋ฅผ ๊ธฐ๋กํ๋ฉฐ, ์๋ฅผ ๋ค์ด, CPU ์ฌ์ฉ๋, ๋คํธ์ํฌ ์ ์ก๋๊ณผ ๊ฐ์ ์งํ๋ฅผ ์ ๊ณตํฉ๋๋ค.
๋ก๊ทธ๋ ๋ก๊ทธ ์ด๋ฒคํธ, ๋ก๊ทธ ์คํธ๋ฆผ, ๋ก๊ทธ ๊ทธ๋ฃน์ผ๋ก ๊ตฌ์ฑ๋๋ฉฐ, ๋ก๊ทธ ์ด๋ฒคํธ๋ ๋ก๊ทธ๋ฅผ ๊ธฐ๋กํ ์ ํ๋ฆฌ์ผ์ด์ ๋๋ ์์์ ํ ์ค์ ๋ํ๋ ๋๋ค.
CloudWatch๋ ํน์ ๊ธฐ์ค์ด ์ถฉ์กฑ๋๋ฉด Amazon EC2 ์ธ์คํด์ค๋ฅผ ์ค์ง, ์์ ๋๋ ์ข ๋ฃํ๋๋ก ๊ฒฝ๋ณด ์์ ์ ์ค์ ํ ์ ์์ต๋๋ค.
CloudTrail์ ์ฃผ๋ก AWS ๊ณ์ ์ ํ๋์ ์ถ์ ํ๊ณ ๊ฐ์ฌํ๋ ๋ฐ ์ฌ์ฉ๋๋ฉฐ, ๋ณด์ ๋ฐ ๊ท์ ์ค์๋ฅผ ๊ฐํํ๋ ๋ฐ ์ค์ ์ ๋ก๋๋ค.
CloudWatch๋ AWS์ ์์๊ณผ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ชจ๋ํฐ๋งํ๊ณ , ์ด์ ๋ฐ ์ฑ๋ฅ์ ์ถ์ ํ๋ฉฐ, ๋ก๊ทธ ๋ฐ ์งํ ์ ๋ณด๋ฅผ ์ ๊ณตํ์ฌ ์์คํ ์ ์ํ๋ฅผ ํ์ ํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
์ค์ ํ ๋ด์ฉ์ ํ์ธํ๊ณ [์ถ์ ์์ฑ] ๋ฒํผ์ ํด๋ฆญ
๋ด์ฉ ํ์ธ ํ [์ญํ ์์ฑ] ๋ฒํผ์ ํด๋ฆญ
Service ํญ๋ชฉ์ vpc-flow-logs.amazonaws.com ๋ก ๋ณ๊ฒฝํ ํ [์ ์ฑ ์ ๋ฐ์ดํธ] ๋ฒํผ์ ํด๋ฆญ
CloudWatch์์ ๋ก๊ทธ ์คํธ๋ฆผ์ ๋ก๊ทธ ๋ฐ์ดํฐ๊ฐ ์ ์ฅ๋๋ ๊ฐ๋ณ์ ์ธ ๋จ์์ ๋๋ค. CloudTrail๊ณผ ๊ฐ์ AWS ์๋น์ค์ ๋ก๊ทธ ๋ฐ์ดํฐ๋ฅผ ์์ ํ๋ฉด ์ด๋ฌํ ๋ฐ์ดํฐ๋ ๋ก๊ทธ ์คํธ๋ฆผ์ผ๋ก ์์ฑ๋ฉ๋๋ค.
๋๋จธ์ง ์ค์ ์ ๊ทธ๋๋ก ์ ์งํ ์ํ์์ [์ธ์คํด์ค ์์] ๋ฒํผ์ ํด๋ฆญ
๋๋จธ์ง ์ค์ ์ ํ์ธํ๊ณ [์ญํ ์์ฑ] ๋ฒํผ์ ํด๋ฆญ
์ธ์คํด์ค ์ฐ๊ฒฐํ ํ
ubuntu@ip-172-31-9-148:~$ wget https://amazoncloudwatch-agent.s3.amazonaws.com/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb
ubuntu@ip-172-31-9-148:~$ sudo dpkg -i -E ./amazon-cloudwatch-agent.deb
ubuntu@ip-172-31-9-148:~$ cd /opt/aws/amazon-cloudwatch-agent/bin/
ubuntu@ip-172-31-9-148:/opt/aws/amazon-cloudwatch-agent/bin$ sudo ./amazon-cloudwatch-agent-config-wizard
================================================================
= 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]:
2
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]:
2
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]:
2
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]:
2
Current config as follows:
{
"agent": {
"metrics_collection_interval": 60,
"run_as_user": "root"
},
"metrics": {
"append_dimensions": {
"AutoScalingGroupName": "${aws:AutoScalingGroupName}",
"ImageId": "${aws:ImageId}",
"InstanceId": "${aws:InstanceId}",
"InstanceType": "${aws:InstanceType}"
},
"metrics_collected": {
"cpu": {
"measurement": [
"cpu_usage_idle",
"cpu_usage_iowait",
"cpu_usage_user",
"cpu_usage_system"
],
"metrics_collection_interval": 60,
"totalcpu": false
},
"disk": {
"measurement": [
"used_percent",
"inodes_free"
],
"metrics_collection_interval": 60,
"resources": [
"*"
]
},
"diskio": {
"measurement": [
"io_time"
],
"metrics_collection_interval": 60,
"resources": [
"*"
]
},
"mem": {
"measurement": [
"mem_used_percent"
],
"metrics_collection_interval": 60
},
"swap": {
"measurement": [
"swap_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": "root"
},
"metrics": {
"append_dimensions": {
"AutoScalingGroupName": "${aws:AutoScalingGroupName}",
"ImageId": "${aws:ImageId}",
"InstanceId": "${aws:InstanceId}",
"InstanceType": "${aws:InstanceType}"
},
"metrics_collected": {
"cpu": {
"measurement": [
"cpu_usage_idle",
"cpu_usage_iowait",
"cpu_usage_user",
"cpu_usage_system"
],
"metrics_collection_interval": 60,
"totalcpu": false
},
"disk": {
"measurement": [
"used_percent",
"inodes_free"
],
"metrics_collection_interval": 60,
"resources": [
"*"
]
},
"diskio": {
"measurement": [
"io_time"
],
"metrics_collection_interval": 60,
"resources": [
"*"
]
},
"mem": {
"measurement": [
"mem_used_percent"
],
"metrics_collection_interval": 60
},
"swap": {
"measurement": [
"swap_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.
ubuntu@ip-172-31-9-148:/opt/aws/amazon-cloudwatch-agent/bin$ sudo mkdir -p /usr/share/collectd/
ubuntu@ip-172-31-9-148:/opt/aws/amazon-cloudwatch-agent/bin$ sudo touch /usr/share/collectd/types.db
ubuntu@ip-172-31-9-148:/opt/aws/amazon-cloudwatch-agent/bin$ sudo ./amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json -s
ubuntu@ip-172-31-9-148:/opt/aws/amazon-cloudwatch-agent/bin sudo apt-get install -y apache2
ubuntu@ip-172-31-9-148:/opt/aws/amazon-cloudwatch-agent/bin$ sudo vi config.json
{
"agent": {
"metrics_collection_interval": 60,
"run_as_user": "root"
},
๐ "logs": {
"logs_collected": {
"files": {
"collect_list": [
{
"file_path": "/var/log/apache2/access.log",
"log_group_name": "rookies_inst_ec2_web_logs",
"log_stream_name": "access_logs"
}
]
}
}
}, ๐ ์ด๊ฑฐ ์ถ๊ฐํ๊ธฐ
"metrics": {
... (์๋ต) ...
ubuntu@ip-172-31-9-148:/opt/aws/amazon-cloudwatch-agent/bin$ sudo ./amazon-cloudwatch-agent-ctl -m ec2 -a stop
** processing amazon-cloudwatch-agent **
ubuntu@ip-172-31-9-148:/opt/aws/amazon-cloudwatch-agent/bin$ sudo ./amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file://opt/aws/amazon-cloudwatch-agent/bin/config.json -s
** processing amazon-cloudwatch-agent **
I! Trying to detect region from ec2 D! [EC2] Found active network interface I! imds retry client will retry 1 timesSuccessfully fetched the config and saved in /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d/file_config.json.tmp
Start configuration validation...
2024/04/29 06:06:44 Reading json config file path: /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d/file_config.json.tmp ...
2024/04/29 06:06:44 I! Valid Json input schema.
2024/04/29 06:06:44 D! ec2tagger processor required because append_dimensions is set
2024/04/29 06:06:44 D! delta processor required because metrics with diskio or net are set
2024/04/29 06:06:44 D! ec2tagger processor required because append_dimensions is set
2024/04/29 06:06:44 Configuration validation first phase succeeded
I! Detecting run_as_user...
I! Trying to detect region from ec2
D! [EC2] Found active network interface
I! imds retry client will retry 1 times
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent -schematest -config /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.toml
Configuration validation second phase succeeded
Configuration validation succeeded
amazon-cloudwatch-agent has already been stopped
์ค์ ๋ด์ฉ ํ์ธ ํ [๊ฒฝ๋ณด ์์ฑ] ๋ฒํผ์ ํด๋ฆญ
๊ตฌ๋ ์น์ธ ์๋ด ๋ฉ์ผ ์์ ์ฌ๋ถ ํ์ธ ํ ๊ตฌ๋ ์น์ธ์ ์ฒ๋ฆฌ
๊ฒฝ๋ณด ํ์ธ
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AWSCloudTrailAclCheck20150319-0882ce1a-2d45-4557-a10a-f7a7a316ebb3",
"Effect": "Allow",
"Principal": {
"Service": "cloudtrail.amazonaws.com"
},
"Action": "s3:GetBucketAcl",
"Resource": "arn:aws:s3:::rookies-inst-cloudtrail-logs",
"Condition": {
"StringEquals": {
"AWS:SourceArn": "arn:aws:cloudtrail:ap-northeast-2:471112797705:trail/rookies_inst_cloudtrail"
}
}
},
{
"Sid": "AWSCloudTrailWrite20150319-69bc4745-723c-456f-bdc4-6bbafbc3e7fd",
"Effect": "Allow",
"Principal": {
"Service": "cloudtrail.amazonaws.com"
},
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::rookies-inst-cloudtrail-logs/AWSLogs/471112797705/*",
"Condition": {
"StringEquals": {
"s3:x-amz-acl": "bucket-owner-full-control",
"AWS:SourceArn": "arn:aws:cloudtrail:ap-northeast-2:471112797705:trail/rookies_inst_cloudtrail"
}
}
},
{
"Effect": "Allow",
"Principal": {
"Service": "logs.ap-northeast-2.amazonaws.com"
},
"Action": "s3:GetBucketAcl",
"Resource": "arn:aws:s3:::rookies-inst-cloudtrail-logs"
},
๐{
"Effect": "Allow",
"Principal": {
"Service": "logs.ap-northeast-2.amazonaws.com"
},
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::rookies-inst-cloudtrail-logs/vpc-flow-logs/*",
"Condition": {
"StringEquals": {
"s3:x-amz-acl": "bucket-owner-full-control"
}
}
}๐
]
}