[AWS Cloud9]EC2 자동 정지를 감지하고 다른 처리를 시작
7511 단어 람다파이썬AWSCloud9CloudWatchAWS
소개
AWS Cloud9는 IDE 브라우저를 닫을 때 자동으로 EC2 인스턴스를 중지할 수 있습니다.
몇 분 후에 자동 정지 여부는 설정에서 변경이 가능합니다.

AWS Cloud9를 1주일 사용하면 EC2 인스턴스가 자동 중지하는 것이 "편리한 → 당연"이라는 느낌이 들었습니다. 그리고 생각했습니다. 자동 정지를 감지하여 더 즐겁게 할 수 없을까.
하고 싶은 일
AWS Cloud9 기능으로
AWS Cloud9 기능으로
자동 정지되었음을 감지
CloudWatch 규칙을 만듭니다.

{
"source": [
"aws.ec2"
],
"detail-type": [
"EC2 Instance State-change Notification"
],
"detail": {
"state": [
"running",
"stopped"
],
"instance-id": [
"AWS Cloud9のインスタンスID"
]
}
}
감지한 정보를 트리거로 다른 처리를 동작
타겟에 이번은 2개(SNS(메일 송신)와 Lambda(rds정지)를 설정했습니다.)
SNS는 용도를 생각하지 못했기 때문에 이메일을 설정했을 뿐. json이 그대로 메일로 날아온다.

Lambda는 드디어 IDE 기동했을 때의 동작도 기술.
IAM 역할
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
"rds:StartDBInstance",
"rds:StopDBInstance"
],
"Effect": "Allow",
"Resource": "*"
}
]
}
lambda_function.py
import boto3
def lambda_handler(event, context):
dbinstance = 'RDSインスタンス名'
rds = boto3.client('rds')
instanceState = event['detail']['state']
if instanceState == 'stopped':
result = rds.stop_db_instance(DBInstanceIdentifier = dbinstance)
else:
result = rds.start_db_instance(DBInstanceIdentifier = dbinstance)
print(result)



요약
관련
Reference
이 문제에 관하여([AWS Cloud9]EC2 자동 정지를 감지하고 다른 처리를 시작), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/icck/items/c841fa99af2c88a6d956텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)