├── myLambdaFunction.py ├── readEventBridge.py ├── insert_into_dynamo.py ├── delete_from_dynamo.py ├── strongly_consistent_read_dynamo.py ├── README.md ├── publishtoEventBridge.py ├── get_picture_from_s3.py ├── ec2_check_tags.py └── lambda_deployer.js /myLambdaFunction.py: -------------------------------------------------------------------------------- 1 | def lambda_handler(event, context): 2 | # TODO implement 3 | print("hello") 4 | return 'Hello from Lambda' -------------------------------------------------------------------------------- /readEventBridge.py: -------------------------------------------------------------------------------- 1 | import json 2 | 3 | def lambda_handler(event, context): 4 | # TODO implement 5 | print(event) 6 | return event 7 | -------------------------------------------------------------------------------- /insert_into_dynamo.py: -------------------------------------------------------------------------------- 1 | import json 2 | import boto3 3 | 4 | def lambda_handler(event, context): 5 | # TODO implement 6 | dynamodb = boto3.resource('dynamodb') 7 | table = dynamodb.Table('People') 8 | try: 9 | # TODO: write code... 10 | response = table.put_item( 11 | Item=event) 12 | return "Done" 13 | except: 14 | raise 15 | 16 | -------------------------------------------------------------------------------- /delete_from_dynamo.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | 3 | def lambda_handler(event, context): 4 | # TODO implement 5 | dynamodb = boto3.resource('dynamodb') 6 | table = dynamodb.Table('People') 7 | PID=event['PersonID'] 8 | try: 9 | # TODO: write code... 10 | response = table.delete_item( 11 | Key={"PersonID":PID}) 12 | return "Done" 13 | except: 14 | raise 15 | -------------------------------------------------------------------------------- /strongly_consistent_read_dynamo.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | 3 | def lambda_handler(event, context): 4 | # TODO implement 5 | dynamodb = boto3.resource('dynamodb') 6 | table = dynamodb.Table('People') 7 | PID=event['PersonID'] 8 | try: 9 | # TODO: write code... 10 | response = table.get_item( 11 | Key={"PersonID":PID}, 12 | ConsistentRead=True) #Remove this to turn off Strongly Consistent Reads 13 | return response 14 | except: 15 | raise 16 | 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Readme to be added 2 | # What it does 3 | This is a repository of various Lambdas that I have written over the years. They do various tasks such as CRUD operation in Dynamo, fetch image from S3, post event to Amazon EventBridge, check EC2 tags etc. Please let me know if you have aquestion on a specific program. 4 | 5 | # Things I learned 6 | * AWS Lambda 7 | * Python coding 8 | * AWS API SDK (Boto3) 9 | * Event driven patterns 10 | * DynamoDB 11 | 12 | # How to run? 13 | * Create a Lambda function from AWS console with Python as the language 14 | * Create appropriate IAM role 15 | * Once Lambda is created, paste the code from the python programs into your Lambda code 16 | * Save and run the Lambda function! 17 | 18 | -------------------------------------------------------------------------------- /publishtoEventBridge.py: -------------------------------------------------------------------------------- 1 | import json 2 | import boto3 3 | import datetime 4 | 5 | client = boto3.client('events') 6 | 7 | def lambda_handler(event, context): 8 | # Replace event bus name with your event bus arn 9 | 10 | response = client.put_events( 11 | Entries=[ 12 | { 13 | 'Time': datetime.datetime.now(), 14 | 'Source': 'Lambda Publish', 15 | 'Resources': [ 16 | ], 17 | 'DetailType': 'EB Demo', 18 | 'Detail': json.dumps(event), 19 | 'EventBusName': 'arn:aws:events:us-west-2:123456789012:event-bus/eventbus', 20 | 'TraceHeader': 'testdemo' 21 | }, 22 | ] 23 | ) 24 | 25 | return response 26 | -------------------------------------------------------------------------------- /get_picture_from_s3.py: -------------------------------------------------------------------------------- 1 | import base64 2 | import boto3 3 | 4 | 5 | s3 = boto3.client('s3') 6 | 7 | def lambda_handler(event, context): 8 | bucket_name = event ["pathParameters"]["bucket"] 9 | file_name = event ["queryStringParameters"]["file"] 10 | fileObj = s3.get_object(Bucket=bucket_name, Key=file_name) 11 | file_content = fileObj["Body"].read() 12 | # print 13 | return { 14 | "statusCode": 200, 15 | "headers": { 16 | "Content-Type": "application/jpg", 17 | "Content-Disposition": "attachment; filename={}".format(file_name) 18 | }, 19 | "body": base64.b64encode(file_content), 20 | "isBase64Encoded": True 21 | } 22 | # return { 23 | # 'headers': { "Content-type": "text/html" }, 24 | 25 | -------------------------------------------------------------------------------- /ec2_check_tags.py: -------------------------------------------------------------------------------- 1 | import json 2 | import boto3 3 | 4 | ec2 = boto3.client('ec2') 5 | sns = boto3.client('sns') 6 | 7 | 8 | def lambda_handler(event, context): 9 | # TODO implement 10 | #print(event) 11 | ec2_instance_id=event['detail']['instance-id'] 12 | 13 | # Put Logic 14 | tag_response= ec2.describe_tags( 15 | Filters=[ 16 | { 17 | 'Name': 'resource-id', 18 | 'Values':[ec2_instance_id], 19 | }, 20 | ], 21 | ) 22 | 23 | alltags=tag_response['Tags'] 24 | 25 | flag='STOP' 26 | for item in alltags: 27 | print(item['Key']) 28 | if item['Key']=='SPECIAL_EXCEPTION': 29 | flag='DONT_STOP' 30 | break 31 | 32 | print(flag) 33 | 34 | # Decision Making 35 | 36 | if flag=='STOP': 37 | ec2.stop_instances(InstanceIds=[ec2_instance_id]) 38 | snsarn="arn:aws:sns:us-east-1:123456789123:email-SNS-topic" 39 | errormsg="EC2 "+ ec2_instance_id + " stopped" 40 | snsresponse=sns.publish(TopicArn=snsarn, 41 | Message=errormsg, 42 | Subject="EC2 Violated Company Policy") 43 | 44 | 45 | return { 46 | 'statusCode': 200, 47 | 'body': json.dumps('Hello from Lambda!') 48 | } 49 | -------------------------------------------------------------------------------- /lambda_deployer.js: -------------------------------------------------------------------------------- 1 | //Make this Lambda function triggered by the S3 bucket object add for the AWS 2 | //CodeBuild Output 3 | console.log('Loading function'); 4 | var AWS = require('aws-sdk'); 5 | var lambda = new AWS.Lambda(); 6 | exports.handler = function(event, context) { 7 | key = event.Records[0].s3.object.key 8 | bucket = event.Records[0].s3.bucket.name 9 | version = event.Records[0].s3.object.versionId 10 | if (bucket == "lambda-package-deploy01" && key == "codedeployoutput.zip" && version) { 11 | var functionName = "codedeployoutput"; 12 | console.log("uploaded to lambda function: " + functionName); 13 | var params = { 14 | Code:{ 15 | S3Key: key, 16 | S3Bucket: bucket, 17 | S3ObjectVersion: version 18 | }, 19 | FunctionName: functionName, 20 | Role: "arn:aws:iam::lambda_role", // replace with the actual arn of the execution role you created 21 | Runtime: "python2.7", //Runtime environment of the application lambda 22 | Handler: "lambda_handler" 23 | }; 24 | lambda.createFunction(params, function(err, data) { 25 | if (err) { 26 | console.log(err, err.stack); 27 | context.fail(err); 28 | } else { 29 | console.log(data); 30 | context.succeed(data); 31 | } 32 | }); 33 | } else { 34 | context.succeed("skipping zip " + key + " in bucket " + bucket + " with version " + version); 35 | } 36 | }; --------------------------------------------------------------------------------