Having multiple environments is important to build a continuous integration/deployment pipeline and be able to reproduce bugs in production with ease but this comes at price. In order to reduce cost of AWS infrastructure, instances which are running 24/7 unnecessarily ( & environments) must be shut down outside of regular business hours. sandbox staging The figure below describes an automated process to schedule, stop and start instances to help cutting costs. The solution is a perfect example of using . Serverless computing Note: full code is available on my . GitHub 2 Lambda functions will be created, they will scan all environments looking for a specific tag. The tag we use is named . Instances without an tag will not be affected: ‘ Environment ’ Environment The function will query the method with the list of instance ids returned by the previous function: StartEnvironment StartInstances Similarly, the function will query the method: StopEnvironment StopInstances Finally, both functions will post a message to channel for real-time notification: Slack Now our functions are defined, let’s build the deployment packages ( files) using the following Bash script: zip The functions require an role to be able to interact with . The StartEnvironment function has to be able to and EC2 instances: IAM EC2 describe start The StopEnvironment function has to be able to and EC2 instances: describe stop Finally, create an IAM role for each function and attach the above policies: The script will output the for each IAM role: ARN Before jumping to deployment part, we need to create a to be able to post messages to Slack channel: Slack WebHook Next, use the following script to deploy your functions to (make sure to replace the IAM roles, Slack WebHook token & the target environment): AWS Lambda Once deployed, if you sign in to , navigate to Lambda Console, you should see both functions has been deployed successfully: AWS Management Console StartEnvironment: StopEnvironment: To further automate the process of invoking the Lambda function at the right time. AWS will be used. CloudWatch Scheduled Events Create a new CloudWatch rule with the below expression (It will be invoked everyday at 9 AM): cron And another rule to stop the environment at 6 PM: Note: All times are time. GMT Testing: a — Stop Environment Result: b — Start Environment Result: The solution is easy to deploy and can help reduce operational costs. Full code can be found on my GitHub . Make sure to drop your comments, feedback, or suggestions below — or connect with me directly on Twitter @ mlabouardy .