반응형
아마 2019년도에 작업한 내역같은데 노션 정리하면서 여기다가 옮기고 삭제해야겠다..
1.Polly 정책 & 역할 생성
테스트용으로 lambda_start_stop_ec2 역할을 생성해준다.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
},
{
"Sid": "VisualEditor1",
"Effect": "Allow",
"Action": [
"ec2:DescribeInstances",
"ec2:Start*",
"ec2:Stop*"
],
"Resource": "*"
},
{
"Sid": "VisualEditor2",
"Effect": "Allow",
"Action": "logs:CreateLogGroup",
"Resource": "arn:aws:log:*:*:*"
}
]
}
아까만든 정책을 연결해서 만들면 끝
람다를 만들어준다. 2개정도 필요
ec2-start
기본설정 → 제한시간 10초로
import boto3
region = 'ap-northeast-2'
intances = ['인스턴스 고유키']
def lambda_handler(event, context):
ec2 = boto3.client('ec2', region_name=region)
ec2.start_instances(InstanceIds=intances)
print 'started your instances: ' + str(intances)
ec2-stop
import boto3
region = 'ap-northeast-2'
intances = ['인스턴스 고유항번']
def lambda_handler(event, context):
ec2 = boto3.client('ec2', region_name=region)
ec2.stop_instances(InstanceIds=intances)
print 'stoped your instances: ' + str(intances)
import boto3
import json
import os
region = os.environ['region']
intances = os.environ['ins_list'].split(",")
proc = os.environ['proc']
def lambda_handler(event, context):
# TODO implement
# print(os.environ['ins_list'])
# start
ec2 = boto3.client('ec2', region_name=region)
print region
print intances
if proc == 'start':
ec2.start_instances(InstanceIds=intances)
print 'start your instances: ' + str(intances)
else:
ec2.stop_instances(InstanceIds=intances)
print 'stopped your instances: ' + str(intances)
https://www.youtube.com/watch?v=X8DYOq_BLSs
끝 정리를 해놓자~
반응형
댓글