Skip to main content

How to schedule automatic start/stop for AWS Elastic Beanstalk

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

Popular posts from this blog

app-policy

PRIVACY POLICY Last updated April 19, 2023 This privacy notice for Team CoderzDuniya ( " Company ," " we ," " us ," or " our " ), describes how and why we might collect, store, use, and/or share ( " process " ) your information when you use our services ( " Services " ), such as when you: Download and use our mobile application ( Revenue Calculator) , or any other application of ours that links to this privacy notice Engage with us in other related ways, including any sales, marketing, or events Questions or concerns?  Reading this privacy notice will help you understand your privacy rights and choices. If you do not agree with our policies and practices, please do not use our Services. If you still have any questions or concerns, please contact us at droidamar007@gmail.com . SUMMARY OF KEY POINTS This summary provides key points from our privacy notice, but you can find out more details about any of these t...

Java Socket Basics(Socket Programming in Java) Part-2(UDP)

Hi friends, We are going to discuss about UDP Socket Programming . In previous post  we discussed about the differences between TCP & UDP and the sample example of TCP Socket Programming. Below is the sample example of chat application using UDP  Socket Programming . UDP Sample :-  We are going to create an small example which contains two classes. UdpServer.java:-  This is a server class. Means this class will serve the purpose of socket connection. DatagramSocket is the java class and serve the purpose of Server and Client both. The overloaded constructor of DatagramSocket class matters. DatagramPacket is the java class which is responsible to transmit the data/packet over the network from server to client and vice-versa. UdpClient.java:-  This is client class. This serves the purpose of client which will communicate to server and send data to server and receive the data sent by server. UdpServer.java import java.net.DatagramPac...

Working with MPAndroidChart (how to create Bar Chart using MPAndroidChart)

Hi Friends, In this tutorial i am going to show, "How to create Bar Chart using MPAndroidChart". There is a lot of libraries for creating charts in android like AChartEngine, MpAndroidChart, AndroidPlot etc. Your first question may be, Why MPAndroidChart. So MpAndroidChart provides better animation functionality and easy to use in comparision. Using  MPAndroidChart library  we can draw a: ·          Simple Bar Chart ·          Grouped Bar Chart ·          Horizontal Bar Chart ·          Simple Line Chart ·          Line Chart with Cubic Lines ·          Grouped Line Chart ·          Combined Line and Bar Chart ·          Pie Chart ·...