Welcome to my collection of AWS Lambda functions! β‘ This repository contains Python scripts designed for various automation tasks in AWS environments.
- π§ Infrastructure Automation
- π₯οΈ EC2 Management
- π Network & VPN Monitoring
- π Quick Start
- π Deployment Guide
- π Security Best Practices
- π€ Contributing
File: python/Create-CLIENT-Route53-and-ACM.py
Automates DNS setup and SSL certificate provisioning for new clients or applications.
Features:
- π Hosted Zone Management: Creates or uses existing Route53 hosted zones
- π DNS Record Creation: Automatically creates A records for:
{client}.{domain.com}{client}-api.{domain.com}ecoaas-api-{client}.{domain.com}
- π SSL Certificate Provisioning: Requests ACM certificates for HTTPS
- β‘ Multi-region Support: Works with both regional and us-east-1 ACM
Environment Variables:
DOMAIN_NAME=example.com
BASE_SUB_DOMAIN=client1
IP_ADDRESS=1.2.3.4IAM Permissions Required:
route53:CreateHostedZoneroute53:ListHostedZonesByNameroute53:ChangeResourceRecordSetsacm:RequestCertificateacm:DescribeCertificateacm:ListCertificates
Use Case: Perfect for SaaS platforms needing to quickly provision DNS and certificates for new clients.
File: python/EC2-StartStopStatus-Simple-Auth.py
Provides a secure web interface for controlling EC2 instances with simple authentication.
Features:
- π Simple Authentication: Username/password with SHA256 hashing
- πͺ Session Management: Secure session tokens with expiration
- ποΈ Instance Control: Start, stop, and check status of EC2 instances
- π Web Interface: Clean HTML interface for easy management
- π± API Gateway Compatible: Works with API Gateway or ALB
Environment Variables:
INSTANCE_ID=i-1234567890abcdef0
AWS_ALT_REGION=us-west-2
AUTH_USERNAME=admin
AUTH_PASSWORD_HASH=sha256_hash_of_password
SESSION_SECRET=random_32_byte_hex_stringIAM Permissions Required:
ec2:StartInstancesec2:StopInstancesec2:DescribeInstances
Security Features:
- π Password hashing with SHA256
- π« Secure session tokens
- β° Session expiration (24 hours)
- π‘οΈ HMAC-based token verification
Use Case: Ideal for providing controlled access to EC2 instances for non-technical users or temporary access scenarios.
File: python/check-vpn-on-EC2.py
Monitors VPN connections on EC2 instances and performs automatic recovery actions.
Features:
- π Connectivity Testing: Configurable network connectivity tests
- π Auto-Recovery: Automatic VPN service restart on failure
- π° Cost Optimization: Terminates non-functional instances to save costs
- π Detailed Logging: Comprehensive logging for troubleshooting
- βοΈ SSM Integration: Uses Systems Manager for remote command execution
Environment Variables:
EC2_INSTANCE_ID=i-1234567890abcdef0
VPN_TEST_COMM=nc -w3 -zvvv
VPN_RESTART_COMM=sudo systemctl restart strongswan
TARGET_IP=10.0.1.100
PORT=22IAM Permissions Required:
ssm:SendCommandssm:GetCommandInvocationec2:TerminateInstances
Prerequisites:
- EC2 instance with SSM Agent installed
- Instance role with
AmazonSSMManagedInstanceCorepolicy
Recovery Logic:
- π§ͺ Test Connection: Execute connectivity test command
- β Success: Log success and exit
- β Failure: Attempt VPN service restart
- π Retry: Test connection again after restart
- π Terminate: If still failing, terminate instance to prevent costs
Use Case: Essential for hybrid cloud environments with VPN connections that need high availability and cost control.
- AWS CLI configured with appropriate permissions
- Python 3.8+ runtime for Lambda
- Basic understanding of AWS Lambda and IAM
- Clone the repository:
git clone https://github.com/Lechu77/Lambda-Templates.git
cd Lambda-Templates-
Choose your function and configure environment variables
-
Deploy using AWS CLI:
# Create deployment package
zip -r function.zip python/your-function.py
# Create Lambda function
aws lambda create-function \
--function-name your-function-name \
--runtime python3.9 \
--role arn:aws:iam::account:role/lambda-execution-role \
--handler your-function.lambda_handler \
--zip-file fileb://function.zip \
--environment Variables='{
"ENV_VAR1":"value1",
"ENV_VAR2":"value2"
}'# Set environment variables
aws lambda update-function-configuration \
--function-name route53-acm-setup \
--environment Variables='{
"DOMAIN_NAME":"example.com",
"BASE_SUB_DOMAIN":"client1",
"IP_ADDRESS":"1.2.3.4"
}'
# Test the function
aws lambda invoke \
--function-name route53-acm-setup \
--payload '{}' \
response.json# Generate password hash
echo -n "your_password" | shasum -a 256
# Generate session secret
openssl rand -hex 32
# Deploy with API Gateway
aws lambda update-function-configuration \
--function-name ec2-control \
--environment Variables='{
"INSTANCE_ID":"i-1234567890abcdef0",
"AWS_ALT_REGION":"us-west-2",
"AUTH_USERNAME":"admin",
"AUTH_PASSWORD_HASH":"your_sha256_hash",
"SESSION_SECRET":"your_32_byte_hex"
}'# Configure monitoring
aws lambda update-function-configuration \
--function-name vpn-monitor \
--environment Variables='{
"EC2_INSTANCE_ID":"i-1234567890abcdef0",
"VPN_TEST_COMM":"nc -w3 -zvvv",
"VPN_RESTART_COMM":"sudo systemctl restart strongswan",
"TARGET_IP":"10.0.1.100",
"PORT":"22"
}'
# Schedule with EventBridge
aws events put-rule \
--name vpn-monitor-schedule \
--schedule-expression "rate(5 minutes)"- π― Principle of Least Privilege: Grant only necessary permissions
- π·οΈ Resource-Specific Policies: Limit access to specific resources when possible
- π Regular Audits: Review and update permissions regularly
- π Sensitive Data: Use AWS Secrets Manager for passwords and keys
- π Rotation: Implement regular rotation of secrets
- π Monitoring: Log access to sensitive environment variables
- π VPC Configuration: Deploy Lambda functions in private subnets when needed
- π‘οΈ Security Groups: Configure appropriate security group rules
- π Monitoring: Enable CloudTrail and CloudWatch for audit trails
| Category | Functions | Purpose |
|---|---|---|
| Infrastructure π§ | Route53 & ACM | DNS and certificate automation |
| Compute π₯οΈ | EC2 Control | Instance management interface |
| Monitoring π | VPN Health Check | Network connectivity monitoring |
- π Error Handling: Implement comprehensive error handling and logging
- β° Timeouts: Set appropriate timeout values for your functions
- πΎ Memory Optimization: Right-size memory allocation for performance
- π Retry Logic: Implement exponential backoff for external API calls
- π Monitoring: Use CloudWatch metrics and alarms
- π§ͺ Testing: Test functions thoroughly in development environments
Contributions are welcome! Please:
- π΄ Fork the repository
- πΏ Create a feature branch
- β Test your functions thoroughly
- π Update documentation
- π Submit a pull request
- Follow PEP 8 Python style guidelines
- Include comprehensive error handling
- Add detailed docstrings and comments
- Test with multiple AWS regions when applicable
For questions or issues:
- π Open an issue in this repository
- π§ Contact: [Your contact information]
This project is licensed under the terms included in the LICENSE file.
β If you find these Lambda functions useful, please give this repository a star!
Made with β€οΈ by Lechu