EC2 인스턴스를 스케줄로 시작 및 중지
10045 단어 EC2람다CloudWatchAWS
EC2 인스턴스를 예약으로 시작 및 중지
목적
함수 만들기 1
함수 이름과 역할을 설정합니다.
사용자 지정 역할 만들기를 선택하면 사용자 지정 역할 만들기의 새 화면이 열립니다.

사용자 지정 역할 만들기
정책 문서
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
},
{
"Effect": "Allow",
"Action": [
"ec2:Start*",
"ec2:Stop*"
],
"Resource": "*"
}
]
}

함수 생성 2
작성한 롤이 기존의 롤로서 읽혀지기 때문에, [함수의 작성]을 실시한다.

lambda_handler.py
import boto3
def lambda_handler(event, context):
region = event['Region']
instances = event['Instances']
ec2 = boto3.client('ec2', region_name=region)
if event['Action'] == 'start':
ec2.start_instances(InstanceIds=instances)
print 'started your instances: ' + ", ".join(instances)
elif event['Action'] == 'stop':
ec2.stop_instances(InstanceIds=instances)
print 'stopped your instances: ' + ", ".join(instances)
동작 테스트
임의의 파라미터를 설정하여 동작을 확인한다.
세 개의 인스턴스에 대해 시작을 지시합니다.

인스턴스 ID를 임의의 ID로 변경
대상을 3개 정의하고 있지만, 당연히 증감시켜도 좋다
test.json
{
"Action": "start",
"Region": "ap-northeast-1",
"Instances":
[
"i-xxxxxxxxxxxxxxxxx",
"i-yyyyyyyyyyyyyyyyyy",
"i-zzzzzzzzzzzzzzzzz"
]
}

동작 테스트
EC2 인스턴스가 시작되거나 동작을 확인합니다.
실행 결과가 성공하고 EC2가 시작되면 OK입니다.


CloudWatch
30 14 * * ? *
{"Action": "stop", "Region": "ap-northeast-1", "Instances": ["i-xxxxxxxxxxxxxxxxx","i-yyyyyyyyyyyyyyyyyy","i-zzzzzzzzzzzzzzzzz"]}


참고
Reference
이 문제에 관하여(EC2 인스턴스를 스케줄로 시작 및 중지), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/r_karotou/items/1b6dca99d4eb6980f9ae텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)