Skip to content

Instantly share code, notes, and snippets.

@ninejuan
Last active March 31, 2026 10:23
Show Gist options
  • Select an option

  • Save ninejuan/e9d67adba941255187d2cd615965d92a to your computer and use it in GitHub Desktop.

Select an option

Save ninejuan/e9d67adba941255187d2cd615965d92a to your computer and use it in GitHub Desktop.
#!/bin/bash
# ------------------------------------
# 해당 파일을 사용 시에는 여러분들 상황에 맞게
# 잘 커스텀해서
# ------------------------------------
set -euo pipefail
BUCKET_NAME="worldpay-artifact-156041424727"
APP_NAME="app.py"
REQUIREMENTS_NAME="requirements.txt"
APP_DIR="/app"
LOG_DIR="/var/log/app"
LOG_GROUP="/aws/ec2/wsi"
CW_AGENT_CONFIG="/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json"
# IAM Role이 EC2에 붙을 때까지 대기
until aws sts get-caller-identity &>/dev/null; do
sleep 2
done
# 패키지 업데이트 및 필요 패키지 설치
dnf update -y
dnf install -y python3 python3-pip amazon-cloudwatch-agent
# 앱 및 로그 디렉토리 생성
mkdir -p $APP_DIR
mkdir -p $LOG_DIR
# S3에서 앱 파일 다운로드
aws s3 cp s3://$BUCKET_NAME/$APP_NAME $APP_DIR/$APP_NAME
aws s3 cp s3://$BUCKET_NAME/$REQUIREMENTS_NAME $APP_DIR/$REQUIREMENTS_NAME
# 의존성 설치
pip3 install -r $APP_DIR/$REQUIREMENTS_NAME
# systemd 서비스 등록 — 로그를 파일로 출력
cat > /etc/systemd/system/app.service << EOF
[Unit]
Description=Skills WAS Application
After=network.target
[Service]
ExecStart=/usr/bin/python3 $APP_DIR/$APP_NAME
Restart=always
RestartSec=5
StandardOutput=append:$LOG_DIR/app.log
StandardError=append:$LOG_DIR/app.log
# Environment="AWS_REGION=ap-northeast-2"
# Environment="DB_NAME=skills"
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable app
systemctl start app
# CloudWatch Agent 설정
# log_stream_name / log_group_name 에서 사용 가능한 변수:
# {instance_id} — EC2 인스턴스 ID 예: i-0123456789abcdef0
# {hostname} — EC2 메타데이터 기반 호스트명 예: ip-10-0-1-5
# {ip_address} — EC2 프라이빗 IP 주소 예: 10.0.1.5
# {local_hostname} — OS 네트워크 설정 기반 호스트명 (비EC2/온프레미스 환경에서 유용)
#
# metrics.append_dimensions 에서 사용 가능한 변수:
# ${aws:InstanceId} — EC2 인스턴스 ID
# ${aws:ImageId} — AMI ID
# ${aws:InstanceType} — 인스턴스 타입 예: t3.micro
# ${aws:AutoScalingGroupName} — ASG 이름 (ASG 소속일 때만 값 존재)
cat > $CW_AGENT_CONFIG << EOF
{
"agent": {
"metrics_collection_interval": 60,
"run_as_user": "root"
},
"logs": {
"logs_collected": {
"files": {
"collect_list": [
{
"file_path": "$LOG_DIR/app.log",
"log_group_name": "$LOG_GROUP",
"log_stream_name": "api_{instance_id}",
"retention_in_days": 7
}
]
}
}
},
"metrics": {
"namespace": "Skills/EC2",
"append_dimensions": {
"InstanceId": "\${aws:InstanceId}",
"InstanceType": "\${aws:InstanceType}"
},
"metrics_collected": {
"mem": {
"measurement": ["mem_used_percent"],
"metrics_collection_interval": 60
},
"disk": {
"measurement": ["disk_used_percent"],
"resources": ["/"],
"metrics_collection_interval": 60
},
"cpu": {
"measurement": ["cpu_usage_idle", "cpu_usage_user", "cpu_usage_system"],
"metrics_collection_interval": 60,
"totalcpu": true
},
"net": {
"measurement": ["net_bytes_recv", "net_bytes_sent"],
"metrics_collection_interval": 60,
"resources": ["eth0"]
}
}
}
}
EOF
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl \
-a fetch-config -m ec2 -s \
-c file:$CW_AGENT_CONFIG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment