/aws-lambda-code-samples

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

Primary LanguagePythonMIT LicenseMIT

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 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 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 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

  • Refer to 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 have tested all scripts using Python3.6 runtime inside Lambda under us-west-2 region, and most of this should work for all Python3 runtime.

  • 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 📢

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