💡 AWS Lambda와 CloudWatch를 이용해서, 정해진 시간에 EC2를 stop , start 할 수 있도록
람다에서 요구 기능을 수행하기 위해서는 2가지 Role을 가진 역할 필요함
AmazonEC2ReadOnlyAccess
AWSLambdaBasicExecutionRole
람다를 사용할 IAM 유저도 3가지 Role을 가지고 있어야함.
AWSLambdaFullAccessAWSLambdaReadOnlyAccessAWSLambdaRole1) 새로 작성 (Author from scratch)

2) 기본 정보

3) 권한

import boto3
region = 'us-west-1'
instances = ['i-12345cb6de4f78g9h', 'i-08ce9b2d7eccf6d26']
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
ec2.stop_instances(InstanceIds=instances)
print('stopped your instances: ' + str(instances))
import boto3
region = 'us-west-1'
instances = ['i-12345cb6de4f78g9h', 'i-08ce9b2d7eccf6d26']
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
ec2.start_instances(InstanceIds=instances)
print('started your instances: ' + str(instances))

이미 지정된 hello-world 템플릿에서 이벤트 이름 작성, 내부 json값을 변경하지 않아도 됨.

작성 후 , 하단에 생성을 클릭
이 후에 다시 테스트 버튼을 클릭해서 테스트 실행 혹은 함수코드에 Test 버튼으로도 실행 가능함.

이벤트 —> 규칙으로 들어감


일정에서 cron 으로 설정
.png)
(참고) UTC 시간 계산 사이트