DEV Community

Cover image for ازاي تمسح EBS غير مستخدمه عن طريق Lambda و EventBridge
Muhammed Ashraf
Muhammed Ashraf

Posted on

1

ازاي تمسح EBS غير مستخدمه عن طريق Lambda و EventBridge

كلنا عارفين عامل ال Cost ف AWS من العوامل المهمه اللي كتير مش بناخد بالنا منها.. سواء كنت بتسخدم ال Service على مستوى شخصي كا تجربه او على مستوى Enterprise

و دي بتفرق كتير اوي ف اختياري للطريقه او Services اللي هستخدمها و انا ببني ال Solution بتاعي

اوقات كتير اوي بيبقى فيه Resources كتيره مش بنستخدمها او بننساها و دي ممكن تحسب تكلفه مع الوقت و احنا مش واخدين بالنا.. و نلاقي فاتوره كبيره اخر الشهر جايلنا و مش عارفين بتبقى بسبب ايه

علشان كده فكرت ازاي ممكن نحل الموضوع ده عن طريق Automated Solution يقدر ينفذلنا ده علشان يحافظ على الفاتوره بتاعتنا

خلينا نبدا ب ال EBS Volumes.. اه مش بيبقلى تكلفتها كبيره زي بقيت ال Services بس خلينا نبدأ ب دي كا اول السلسله

ال Solution بتاعنا انهارده بيبدأ ب كذا Service:
• Lambda Function: ودي هيبقى فيها ال Logic بتاعنا اللي بينفذ ال Code
• EventBridge: و دي هتبقى ال Service اللي بتخلي ال Lambda function تشتغل على فترات زمنيه بعرفها
• AWS SES: و دي بتبعتلي email ب ال volumes اللي حصل Action عليهم

عندنا ف ال Demo
ده 2 Lamda Functions و احده بتعمل list لل volumes و التانيه بتاخد Action

هنبدا اننا نعمل Create ل Lambda ودي ال Configuration بتاعت اول Lambda
هنشغتل Python

Image description

هنعدل ال Execution Role لل Lambda ب ال Permissions دي

Image description

ده ال Code بتاعنا

import boto3

def lambda_handler(event, context):
    ec2_client = boto3.client('ec2')
    ses_client = boto3.client('ses')

    unused_volumes = []
    CHARSET='UTF-8'

    volumes = ec2_client.describe_volumes()


    for volume in volumes['Volumes']:
        if len(volume['Attachments']) == 0:
            unused_volumes.append(volume['VolumeId'])
            print(unused_volumes)
            print("-------"*5)


    email_body = """
            <html>
                <head></head>
                <h1 style='text_aligned:center'>Unused Volumes in your account </h1>
                <p style='color:red'>below list contains the unused volumes </p>
            </html>
        """

    for vol in unused_volumes:
        email_body = email_body + "VolumeId {} \n".format(vol)

    print(email_body)

    for delete_vol in unused_volumes:
        response_delete = ec2_client.delete_volume(
                VolumeId=delete_vol,
                DryRun=False
        )

    print(response_delete)

    response = ses_client.send_email(
            Destination={
                "ToAddresses": ['x@example.com','y@example.com']
             },
            Message={
                "Body":{
                    "Html":{
                        "Charset":CHARSET,
                        "Data": email_body
                    }
                },
                "Subject":{
                        "Charset":CHARSET,
                        "Data": "This email address notify you with the unused volumes into your account"
                    }
                },
                Source = "x@example.com"
        )
Enter fullscreen mode Exit fullscreen mode

فكره ال Code بأختصار انه بيعمل describe لل volumes و بيشوف مين attached
و فيه ال Email Body اللي هيتبعت فيه ال List

دلوقتي هنظبط AWS SES و نحط ال destintations اللي عايزينهم يستقبلوا ال email

Image description

الخطوه الجايه اننا هنعل automate عن طريق EventBridge

هنروح لل Function بتاعتنا و هنعمل ال Trigger بتاعنا يكون EventBridge زي الصوره

Image description

Image description

هنظبط ال Configration بتاعت EventBrige انها تشتغل زي ما احنا عايزين و نحط ال schedulded expression براحتنا

و دي النتيجه :)

Image description

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay