├── README.md ├── csv_json_dynamodb.py ├── deregister_unused_ami.py ├── lambda_start.py ├── lambda_stop.py ├── read_s3_json.py └── unused_volumes.py /README.md: -------------------------------------------------------------------------------- 1 |
2 | 3 | ![online live webinar on](https://user-images.githubusercontent.com/88716270/197322179-3866434e-6562-4b3f-b7fc-9362c35ec6b0.png) 4 | 5 | ### Welcome to the series of AWS Lambda where we have discussed about 6 realtime useacases and how we can code it practically. 6 | 7 | ### If you like the series dont forget to [nominate me for Github Star](https://stars.github.com/nominate/) 🌟 8 |
9 | 10 | ## 📋 TABLE OF CONTENTS 📋 11 | #### ✍ AWS Lambda Realtime Usecases Blogs 12 | #### 📽 AWS Lambda Realtime Usecases Youtube Series 13 | #### 👨🏻‍💻 Git Repo Code Library 14 | 15 | ## ✍ AWS Lambda Realtime Usecases Blogs 16 | 17 | [![image](https://user-images.githubusercontent.com/88716270/197322288-c30f76cd-d2d6-4ff7-bd95-5fd0b18c4d2f.png)](https://dheeraj3choudhary.com/aws-lambda-and-s3or-automate-csv-file-processing-from-s3-bucket-and-push-in-dynamodb-using-lambda-python "Blog") 18 | 19 | [![image](https://user-images.githubusercontent.com/88716270/197322299-80dce49b-db57-49e1-9195-e21d55afe165.png)](https://dheeraj3choudhary.com/aws-lambda-and-s3or-automate-json-file-processing-from-s3-bucket-and-push-in-dynamodb-using-lambda-python "Blog") 20 | 21 | [![image](https://user-images.githubusercontent.com/88716270/197322312-ea5604fc-66f6-46b3-a2bb-aecb26a44109.png)](https://dheeraj3choudhary.com/aws-lambda-and-eventbridge-or-deregister-unused-ami-in-account-on-weekly-basis-and-notify-via-email "Blog") 22 | 23 | [![image](https://user-images.githubusercontent.com/88716270/197322318-42e5cec6-b3eb-4741-9a3f-5706244bc695.png)](https://dheeraj3choudhary.com/aws-lambda-and-eventbridge-or-schedule-start-and-stop-of-ec2-instances-based-on-tags "Blog") 24 | 25 | [![image](https://user-images.githubusercontent.com/88716270/197322345-c88a88fb-7da1-4f20-bc92-b28d9916282e.png)](https://dheeraj3choudhary.com/aws-lambda-and-eventbridge-or-find-unused-ebs-volumes-on-weekly-basis-and-notify-via-email "Blog") 26 | 27 | [![image](https://user-images.githubusercontent.com/88716270/197322350-6a8be8a7-6162-4bec-95e8-506732fa966d.png)](https://dheeraj3choudhary.com/aws-lambda-and-eventbridge-or-find-unused-aws-elastic-ips-in-aws-account-on-weekly-basis-and-notify-via-email "Blog") 28 | 29 | ## 👁‍🗨👁‍🗨 AWS Lambda Realtime Usecases Youtube Series 30 | 31 | [![image](https://user-images.githubusercontent.com/88716270/197322438-958d781a-797e-4462-b2ea-ccabee5a6884.png)](https://www.youtube.com/watch?v=lwyr6E5kaQA "") 32 | 33 | [![image](https://user-images.githubusercontent.com/88716270/197322444-c2ea9a2c-3f87-4da9-ad0d-17b189b8669a.png)](https://www.youtube.com/watch?v=tp84tyMDJtY "") 34 | 35 | [![image](https://user-images.githubusercontent.com/88716270/197322462-878bd9d5-622f-4e2b-b5ff-bd18ad978a4b.png)](https://www.youtube.com/watch?v=4dA22fA-Gyw "") 36 | 37 | [![image](https://user-images.githubusercontent.com/88716270/197322491-e79f597d-bfbc-4460-9086-0fe9755f1bf4.png)](https://www.youtube.com/watch?v=noBCcz6AZBo "") 38 | 39 | [![image](https://user-images.githubusercontent.com/88716270/197322495-b1005331-68a4-4369-ae12-82a9f9f7b58c.png)](https://www.youtube.com/watch?v=hnfeyKNJ4pM "") 40 | 41 | 42 |
43 | 44 | ### Connect And Follow Me On Social Handles: 45 | 46 | 47 | Dheeraj Choudhary | LinkedIN 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | ## [Subscribe](https://www.youtube.com/channel/UCFiAytcPIlbvvVc7yHKxkMg/videos) for more About Cloud & Technology. ❤ 59 |
60 | -------------------------------------------------------------------------------- /csv_json_dynamodb.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | s3_client = boto3.client('s3') 3 | dynamodb = boto3.resource('dynamodb') 4 | table = dynamodb.Table('user') 5 | def lambda_handler(event, context): 6 | # First we will fetch bucket name from event json object 7 | bucket = event['Records'][0]['s3']['bucket']['name'] 8 | # Now we will fetch file name which is uploaded in s3 bucket from event json object 9 | csv_file_name = event['Records'][0]['s3']['object']['key'] 10 | #Lets call get_object() function which Retrieves objects from Amazon S3 as dictonary 11 | csv_object = s3_client.get_object(Bucket=bucket,Key=csv_file_name) 12 | file_reader = csv_object['Body'].read().decode("utf-8") 13 | print(file_reader) 14 | # Split a string into a list where each word is a list item 15 | users = file_reader.split("\n") 16 | # The filter() method filters the given sequence with the help of a function that tests each element in the sequence to be true or not. 17 | users = list(filter(None, users)) 18 | #Now we will traverse throught the list pick elements one by one and push it to dynamodb table 19 | for user in users: 20 | user_data = user.split(",") 21 | table.put_item(Item = { 22 | "id" : user_data[0], 23 | "name" : user_data[1], 24 | "salary" : user_data[2] 25 | }) 26 | return 'success' 27 | -------------------------------------------------------------------------------- /deregister_unused_ami.py: -------------------------------------------------------------------------------- 1 | ### Author:-Dheeraj Choudhary 2 | import boto3 3 | ec2 = boto3.client('ec2') 4 | sns_client = boto3.client('sns') 5 | instances = ec2.describe_instances() 6 | def lambda_handler(event, context): 7 | used_ami = [] # Create empty list to save used ami 8 | for reservation in instances['Reservations']: 9 | for instance in reservation['Instances']: 10 | state = instance['State'] 11 | state = (state['Name']) 12 | if state == 'running': 13 | used_ami.append(instance['ImageId']) 14 | 15 | # Remove duplicate entries from list 16 | used_ami = list(set(used_ami)) 17 | 18 | # Get all images from account in available state 19 | images = ec2.describe_images( 20 | Filters=[ 21 | { 22 | 'Name': 'state', 23 | 'Values': ['available'] 24 | }, 25 | ], 26 | Owners=['self'], 27 | ) 28 | 29 | # Traverse dictonary returned and fetch Image ID and append in list 30 | custom_ami = [] # Create empty list to save custom ami 31 | for image in images['Images']: 32 | custom_ami.append(image['ImageId']) 33 | 34 | # Check if custom ami is there in used AMI list if not deregister the AMI 35 | deregister_list = [] 36 | for ami in custom_ami: 37 | if ami not in used_ami: 38 | print("AMI {} has been deregistered".format(ami)) 39 | ec2.deregister_image(ImageId=ami) 40 | deg = ("-----Unused AMI ID = {} is Deregistered------".format(ami)) 41 | deregister_list.append(deg) 42 | 43 | sns_client.publish( 44 | TopicArn='', 45 | Subject='Alert - Unused AMIs Are Deregistered', 46 | Message=str(deregister_list) 47 | ) 48 | return "success" 49 | -------------------------------------------------------------------------------- /lambda_start.py: -------------------------------------------------------------------------------- 1 | ###Author :- Dheeraj Choudhary 2 | 3 | import json 4 | import boto3 5 | 6 | ec2 = boto3.resource('ec2', region_name='us-east-1') 7 | 8 | def lambda_handler(event, context): 9 | instances = ec2.instances.filter(Filters=[{'Name': 'instance-state-name', 'Values': ['stopped']},{'Name': 'tag:Env','Values':['Dev']}]) 10 | for instance in instances: 11 | id=instance.id 12 | ec2.instances.filter(InstanceIds=[id]).start() 13 | print("Instance ID is started :- "+instance.id) 14 | return "success" 15 | -------------------------------------------------------------------------------- /lambda_stop.py: -------------------------------------------------------------------------------- 1 | ### Author :- Dheeraj Choudhary 2 | 3 | import json 4 | import boto3 5 | 6 | ec2 = boto3.resource('ec2', region_name='us-east-1') 7 | 8 | def lambda_handler(event, context): 9 | instances = ec2.instances.filter(Filters=[{'Name': 'instance-state-name', 'Values': ['running']},{'Name': 'tag:Env','Values':['Dev']}]) 10 | for instance in instances: 11 | id=instance.id 12 | ec2.instances.filter(InstanceIds=[id]).stop() 13 | print("Instance ID is stopped"+instance.id) 14 | 15 | return "success" 16 | -------------------------------------------------------------------------------- /read_s3_json.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | import json 3 | import ast 4 | s3_client = boto3.client('s3') 5 | dynamodb_client = boto3.resource('dynamodb') 6 | def lambda_handler(event, context): 7 | # First we will fetch bucket name from event json object 8 | bucket = event['Records'][0]['s3']['bucket']['name'] 9 | # Now we will fetch file name which is uploaded in s3 bucket from event json object 10 | json_file_name = event['Records'][0]['s3']['object']['key'] 11 | #Lets call get_object() function which Retrieves objects from Amazon S3 as dictonary 12 | json_object = s3_client.get_object(Bucket=bucket,Key=json_file_name) 13 | # Lets decode the json object returned by function which will retun string 14 | file_reader = json_object['Body'].read().decode("utf-8") 15 | # We will now change this json string to dictonary 16 | file_reader = ast.literal_eval(file_reader) 17 | # As we have retrieved the dictionary we will put it in dynamodb table 18 | table = dynamodb_client.Table('user') 19 | table.put_item(Item=file_reader) 20 | return 'success' 21 | -------------------------------------------------------------------------------- /unused_volumes.py: -------------------------------------------------------------------------------- 1 | ### Author:- Dheeraj Choudhary 2 | 3 | import boto3 4 | ec2 = boto3.client('ec2') 5 | sns_client = boto3.client('sns') 6 | volumes = ec2.describe_volumes() 7 | 8 | def lambda_handler(event, context): 9 | unused_volumes = [] 10 | for vol in volumes['Volumes']: 11 | if len(vol['Attachments']) == 0: 12 | vol1 = ("-----Unused Volume ID = {}------".format(vol['VolumeId'])) 13 | unused_volumes.append(vol1) 14 | 15 | #email 16 | sns_client.publish( 17 | TopicArn='', 18 | Subject='Warning - Unused Volume List', 19 | Message=str(unused_volumes) 20 | ) 21 | return "success" 22 | --------------------------------------------------------------------------------