본문 바로가기
클라우드/AWS자격증

(AWS자격증)람다 트리거 만들기 실습

by Cloud_Park 2018. 12. 14.



람다 트리거 만들기


실습 시작





SNS

단위 토픽

오토스케일링 그룹 설정

오토스케일링  알림설정



Iam에서 룰설정





Lambda 설정





---------------------------

#-*- coding: utf-8 -*-

# Snap_and_Tag Lambda 함수

#

# 이 함수는 Auto Scaling이 새 인스턴스를 시작하면 트리거됩니다.

# EBS 볼륨의 스냅샷이 생성되고 태그가 추가됩니다.


from __future__ import print_function


import json, boto3


def lambda_handler(event, context):

    print("Received event: " + json.dumps(event, indent=2))


    # Auto Scaling 이벤트 알림에서 EC2 인스턴스 ID 추출

    message = event['Records'][0]['Sns']['Message']

    autoscalingInfo = json.loads(message)

    ec2InstanceId = autoscalingInfo['EC2InstanceId']


    # 인스턴스에 연결된 모든 EBS 볼륨의 스냅샷

    ec2 = boto3.resource('ec2')

    for v in ec2.volumes.filter(Filters=[{'Name': 'attachment.instance-id', 'Values': [ec2InstanceId]}]):

        description = 'Autosnap-%s-%s' % ( ec2InstanceId, v.volume_id )


        if v.create_snapshot(Description = description):

            print("\t\tSnapshot created with description [%s]" % description)


    # EC2 인스턴스에 태그 추가: Key = Snapshots, Value = Created

    ec2 = boto3.client('ec2')

    response = ec2.create_tags(

        Resources=[ec2InstanceId],

        Tags=[{'Key': 'Snapshots', 'Value': 'Created'}]

    )

    print ("***Tag added to EC2 instance with id: " + ec2InstanceId)


    # 완료!

    return ec2InstanceId


---------------------------



밑으로 내리면




위로 다시올려 SNS선택-추가



저장



EC2- 오토스케일링 그룹 -디테일 -편집










람다코드.txt





-------------------------------실습완료-----------------------------




댓글과 공감은 필자에게 큰 힘이 됩니다. 



↓↓↓로그인 필요 없습니다↓↓↓ 

광고 클릭