Step - 1 - IAM Policy create
==========
AWS IAM select
left menu "policy" select
select "create policy" in left sidecopy and paste next contents
===========================
{
"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": "*"
}
]
}
============================
select "tag"
insert name & description
create "role"
select "role" in left menu
select "create role" in left side
connect to policy "ec2_instance_turn_on"
select "next" button till done==========
Select "Lambda" services
insert function name "ec2_turn_off"
select "use already exist role" in radio button
select role name that maked before
select "component" tab
copy & paste next contents
==================
import boto3
region = 'us-west-1'
instances = ['i-ddddddddddddddd', 'i-ssssssssssss']
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
ec2.stop_instances(InstanceIds=instances)
print('stopped your instances: ' + str(instances))
==================
----------change region , instances value
region names can get in top left, click "region" button
do it again for create start instance lambda function
copy & paste next contents
========================
import boto3
region = 'us-west-1'
instances = ['i-ddddddddddddddd', 'i-ssssssssssss']
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
ec2.start_instances(InstanceIds=instances)
print('started your instances: ' + str(instances))
=========================
Step - 3 - cloudwatch services connect
==========
Select CloudWatch services
select "create role" button
select cron expression radio button
===using cron expression in linux - minute hour day month day_of_the_week
===time zone is UTC so that in KST must calcuate in -9 hour
screen must change like next picture
do it again for instance turn on
댓글 없음:
댓글 쓰기