Scheduling automatic start and stop for AWS Elastic Beanstalk environments can help optimise costs by ensuring that environments are only running during required hours. While AWS Elastic Beanstalk doesn't natively support scheduling, you can achieve this through AWS Lambda, EventBridge (formerly CloudWatch Events), and the AWS SDK.
Steps to Schedule Automatic Start/Stop:
1. Define Start and Stop Actions
Elastic Beanstalk environments are started and stopped by updating their environment configuration. Stopping involves changing the environment's instances to a scaled-down state (e.g., 0 instances).
2. Create AWS Lambda Functions
You need two Lambda functions:
Start Function: Scales the environment to a desired instance count.
Stop Function: Scales the environment to 0 instances.
Example Lambda Code (Python):
Stop Environment
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import boto3 def lambda_handler(event, context): eb_client = boto3.client('elasticbeanstalk') env_name = 'your-environment-name' app_name = 'your-application-name' # Update environment to set min and max instance count to 0 eb_client.update_environment( ApplicationName=app_name, EnvironmentName=env_name, OptionSettings=[ { 'Namespace': 'aws:autoscaling:asg', 'OptionName': 'MinSize', 'Value': '0' }, { 'Namespace': 'aws:autoscaling:asg', 'OptionName': 'MaxSize', 'Value': '0' } ] ) return { 'statusCode': 200, 'body': f"Stopped environment {env_name}" } |
Start Environment
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import boto3 def lambda_handler(event, context): eb_client = boto3.client('elasticbeanstalk') env_name = 'your-environment-name' app_name = 'your-application-name' # Update environment to set min and max instance count to desired values desired_instance_count = '1' # Adjust as needed eb_client.update_environment( ApplicationName=app_name, EnvironmentName=env_name, OptionSettings=[ { 'Namespace': 'aws:autoscaling:asg', 'OptionName': 'MinSize', 'Value': desired_instance_count }, { 'Namespace': 'aws:autoscaling:asg', 'OptionName': 'MaxSize', 'Value': desired_instance_count } ] ) return { 'statusCode': 200, 'body': f"Started environment {env_name}" } |
3. Set Up EventBridge Rules
Create two EventBridge (CloudWatch) rules:
- Start Rule: Triggers the "Start Function" at a specific time (e.g., weekdays at 8 AM).
- Stop Rule: Triggers the "Stop Function" at a specific time (e.g., weekdays at 6 PM).
Example Schedule Expression
- Start: cron(0 8 ? * MON-FRI *) (8 AM, Monday to Friday)
- Stop: cron(0 18 ? * MON-FRI *) (6 PM, Monday to Friday)
4. Grant Necessary Permissions
Ensure the Lambda functions have the required IAM permissions to update the Elastic Beanstalk environment. Attach a policy with the following permissions:
1 2 3 4 5 6 7 8 9 10 11 12 13 | { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "elasticbeanstalk:UpdateEnvironment", "elasticbeanstalk:DescribeEnvironments" ], "Resource": "*" } ] } |
5. Test and Monitor
- Test the Lambda functions manually to ensure they work correctly.
- Check the EventBridge rules to verify they trigger the Lambda functions at the correct times.
- Use Amazon CloudWatch Logs to debug and monitor the Lambda functions.
Alternative: Use AWS Instance Scheduler (Optional)
AWS provides an Instance Scheduler solution that can be adapted for Elastic Beanstalk environments. This requires deploying an AWS CloudFormation template and customizing the configuration for your needs. However, the Lambda approach is more flexible for Elastic Beanstalk-specific use cases.
If you would like to contribute, you can write an article and mail your article to droidamar007@gmail.com. You can see your article appearing on the main page and helping others.
Comments
Post a Comment
You are responsible person and please write responsibly