Skip to content

shreyasgaonkar/aws-lambda-code-samples

Repository files navigation

AWS Lambda sample codes Language grade: Python HitCount

A few of the sample AWS Lambda function codes for common use-cases with Amazon EC2, AWS Lambda, API Gateway & Amazon SNS using Python runtime.

EC2

  1. Delete EBS volumes using a snapshot and its status - delete_volumes_by_snapshot.py

  2. Delete EBS volumes by snapshot - delete_volumes_by_snapshot.py

  3. Describe all AMIs for your account across all regions - describe_ami.py

  4. Describe EC2's metadata in a region - Associated subnets, Instance ID & NACL ID for a target VPC - describe_ec2_securitygroup.py

  5. Start/Stop EC2 instances using CloudWatch Event Trigger - start_stop_ec2_instances_with_cloudwatch_event.py

Lambda

  1. Get all async configuration (Max retry attempts, max event age, destinations) across all regions - async_config_dashboard.py

  2. Delete orphaned Event Source Mappings - clean_orphaned_event_source_mappings.py

  3. Extract Lambda code from the entire deployment package - extract_deployment_package_without_layers

  4. Check Async queue congestions and delays in processing async events - get_async_invoke_delay.py

  5. Get underlying Lambda's CPU hardware, /tmp storage, os-release and it's contents - get_cpu_info.py

  6. Test HTTP connection for your Lambda function inside VPC - http_connection_test.py

  7. List code storage for all Lambda functions in a region - lambda_code_size_all_functions.py

  8. List code storage for a function including all attached layers - lambda_code_size_including_layers.py

  9. List all ENIs created by Lambda functions(s) - lambda_created_enis.py

  10. Get Lambda dashboard metrics across all regions - lambda_dashboard.py

  11. List Lambda function version(s) using an ENI - lambda_hyperplane_eni_checker.py

  12. Create "Memory Used" Metrics for your Lambda functions - lambda_memory_plot.py

  13. Get all functions using reserved or provisional concurrency in a region - list_concurrency_functions.py

  14. List Lambda functions using a runtime - list_functions_by_a_runtime

  15. List all Lambda layers and it's info - list_layer_info.py

  16. Use Python modules from layers over the deployment package - use_modules_from_layers

  17. Lambda X-Ray examples - x_ray_sample.py

SNS

  1. List all subscriptions tied to a topic in an account - list_account_topic_subscriptions.py

  2. Programmatically create subscription filters for SNS - set_subscription_filters.py

  3. Programmatically set SenderID while sending SMS text messages - sender_id.py

  4. Programmatically set max price while sending SMS text messages - set_max_price.py

  5. Get SMS month to date spend in USD - sms_month_to_date_spent_usd.py

  6. SMS Dashboard - sms_dashboard.py

  7. Send SMS with a custom originating number - sms_with_custom_originating_number

API Gateway

  1. Upload Binary Image to S3 using a POST request - upload_image_to_s3.py

Additional Information

  • Please take a look at the individual .md files for additional information.

Built with

Missing Info / Bugs

  • 😰 Something broken? Open an issue with a few sample inputs where it breaks. Screenshots help!

  • More additional services/use-cases, open a new issue

Contributing

This is an iterative repository, I'll keep adding more sample codes for more use cases as I come across them. I had initially created all sample scripts using Python3.6 runtime which is now deprecated. Moving forward, I'll use the latest supported Python runtime in Lambda.

  • Fork repo
  • Set runtime as Python3 (Python3.7 preferred)
  • Send your awesome πŸ™Œ Pull Request with code/.md changes
  • Your PR gets merged βœ… and a shout-outπŸ“’

Would you be looking for CLI samples?

Repo structure:

$ tree
.
β”œβ”€β”€ CODE_OF_CONDUCT.md
β”œβ”€β”€ LICENSE
β”œβ”€β”€ README.md
β”œβ”€β”€ api-gateway-samples
β”‚Β Β  └── upload_image_to_s3
β”‚Β Β      β”œβ”€β”€ README.md
β”‚Β Β      β”œβ”€β”€ template.yml
β”‚Β Β      └── upload_image_to_s3.py
β”œβ”€β”€ ec2-samples
β”‚Β Β  β”œβ”€β”€ delete_older_snapshots
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ README.md
β”‚Β Β  β”‚Β Β  └── delete_older_snapshots.py
β”‚Β Β  β”œβ”€β”€ delete_volumes_by_snapshot
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ README.md
β”‚Β Β  β”‚Β Β  └── delete_volumes_by_snapshot.py
β”‚Β Β  β”œβ”€β”€ describe_ami
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ README.md
β”‚Β Β  β”‚Β Β  └── describe_ami.py
β”‚Β Β  β”œβ”€β”€ describe_ec2_securitygroup
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ README.md
β”‚Β Β  β”‚Β Β  └── describe_ec2_securitygroup.py
β”‚Β Β  └── start_stop_ec2_instances_with_cloudwatch_event
β”‚Β Β      β”œβ”€β”€ README.md
β”‚Β Β      └── start_stop_ec2_instances_with_cloudwatch_event.py
β”œβ”€β”€ lambda-layer
β”‚Β Β  β”œβ”€β”€ README.md
β”‚Β Β  β”œβ”€β”€ boto3.zip
β”‚Β Β  β”œβ”€β”€ pandasnumpy.zip
β”‚Β Β  β”œβ”€β”€ prettyTable.zip
β”‚Β Β  β”œβ”€β”€ psycopg2.zip
β”‚Β Β  β”œβ”€β”€ python38+
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ boto3_python38+.zip
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ numpy_pandas_scipy_python38+.zip
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ requests_python38+.zip
β”‚Β Β  β”‚Β Β  └── urllib3_python38+.zip
β”‚Β Β  β”œβ”€β”€ requests.zip
β”‚Β Β  β”œβ”€β”€ urllib3.zip
β”‚Β Β  └── x-ray.zip
β”œβ”€β”€ lambda-samples
β”‚Β Β  β”œβ”€β”€ async_config_dashboard
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ README.md
β”‚Β Β  β”‚Β Β  └── async_config_dashboard.py
β”‚Β Β  β”œβ”€β”€ clean_orphaned_event_source_mappings
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ README.md
β”‚Β Β  β”‚Β Β  └── clean_orphaned_event_source_mappings.py
β”‚Β Β  β”œβ”€β”€ extract_deployment_package_without_layers
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ README.md
β”‚Β Β  β”‚Β Β  └── extract_deployment_package_without_layers.py
β”‚Β Β  β”œβ”€β”€ get_async_invoke_delay
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ README.md
β”‚Β Β  β”‚Β Β  └── get_async_invoke_delay.py
β”‚Β Β  β”œβ”€β”€ get_cpu_info
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ README.md
β”‚Β Β  β”‚Β Β  └── get_cpu_info.py
β”‚Β Β  β”œβ”€β”€ http_connection_test
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ README.md
β”‚Β Β  β”‚Β Β  └── http_connection_test.py
β”‚Β Β  β”œβ”€β”€ lambda_code_size_all_functions
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ README.md
β”‚Β Β  β”‚Β Β  └── lambda_code_size_all_functions.py
β”‚Β Β  β”œβ”€β”€ lambda_code_size_including_layers
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ README.md
β”‚Β Β  β”‚Β Β  └── lambda_code_size_including_layers.py
β”‚Β Β  β”œβ”€β”€ lambda_created_enis
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ README.md
β”‚Β Β  β”‚Β Β  └── lambda_created_enis.py
β”‚Β Β  β”œβ”€β”€ lambda_dashboard
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ README.md
β”‚Β Β  β”‚Β Β  └── lambda_dashboard.py
β”‚Β Β  β”œβ”€β”€ lambda_hyperplane_eni_checker
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ README.md
β”‚Β Β  β”‚Β Β  └── lambda_hyperplane_eni_checker.py
β”‚Β Β  β”œβ”€β”€ lambda_memory_plot
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ README.md
β”‚Β Β  β”‚Β Β  └── lambda_memory_plot.py
β”‚Β Β  β”œβ”€β”€ list_concurrency_functions
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ README.md
β”‚Β Β  β”‚Β Β  └── list_concurrency_functions.py
β”‚Β Β  β”œβ”€β”€ list_functions_by_a_runtime
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ README.md
β”‚Β Β  β”‚Β Β  └── list_functions_by_a_runtime.py
β”‚Β Β  β”œβ”€β”€ list_layer_info
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ README.md
β”‚Β Β  β”‚Β Β  └── list_layer_info.py
β”‚Β Β  β”œβ”€β”€ use_modules_from_layers
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ README.md
β”‚Β Β  β”‚Β Β  └── use_modules_from_layers.py
β”‚Β Β  └── x_ray_sample
β”‚Β Β      β”œβ”€β”€ README.md
β”‚Β Β      └── x_ray_sample.py
β”œβ”€β”€ sns-samples
β”‚Β Β  β”œβ”€β”€ list_account_topic_subscriptions
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ README.md
β”‚Β Β  β”‚Β Β  └── list_account_topic_subscriptions.py
β”‚Β Β  β”œβ”€β”€ sender_id
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ README.md
β”‚Β Β  β”‚Β Β  └── sender_id.py
β”‚Β Β  β”œβ”€β”€ set_max_price_sms
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ README.md
β”‚Β Β  β”‚Β Β  └── set_max_price_sms.py
β”‚Β Β  β”œβ”€β”€ set_subscription_filters
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ README.md
β”‚Β Β  β”‚Β Β  └── set_subscription_filters.py
β”‚Β Β  β”œβ”€β”€ sms_dashboard
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ README.md
β”‚Β Β  β”‚Β Β  └── sms_dashboard.py
β”‚Β Β  β”œβ”€β”€ sms_month_to_date_spent_usd
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ README.md
β”‚Β Β  β”‚Β Β  └── sms_month_to_date_spent_usd.py
β”‚Β Β  └── sms_with_custom_originating_number
β”‚Β Β      β”œβ”€β”€ README.md
β”‚Β Β      └── sms_with_custom_originating_number.py
└── tmp
    └── images
        β”œβ”€β”€ AWSLambdaAsyncDelayCloudWatchInsights.png
        β”œβ”€β”€ AWSLambdaCloudWatchAsyncDelay.png
        β”œβ”€β”€ AWSLambdaCloudWatchMetric.png
        └── AWSLambdaX-Ray.PNG

About

Lambda sample codes for EC2, Lambda, API Gateway and SNS in python runtime.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages