LoginSignup
0
0

More than 1 year has passed since last update.

Elastic Beanstalk インスタンスの定期スケールアップ・ダウン

Last updated at Posted at 2020-01-24

改善

12:00~13:00のみ負荷のかかるElastic Beanstalkの環境がある。
インスタンスタイプの調整で費用を抑えたい。

lambda_function.lambda_handler

python3.8
import json
import boto3
from datetime import datetime

def lambda_handler(event, context):
    envName = str(event['env-name'])

    instance_type = ''
    curr_hour = (datetime.now() + timedelta(hours=9)).hour

    print(curr_hour)
    
    if(curr_hour == 11): #11時に上げる
        instance_type = 'c5.xlarge'
    elif(curr_hour == 13): #13時に下げる
        instance_type = 't3.micro'
    else:
        return 'NONE'

    client = boto3.client('elasticbeanstalk', region_name="ap-northeast-1")

    response = client.update_environment(
        EnvironmentName=envName,
        OptionSettings=[
            {
                'Namespace': 'aws:autoscaling:launchconfiguration',
                'OptionName': 'InstanceType',
                'Value': instance_type,
            }
        ],
    )

    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }


CloudWatch Events

例)cron(5 * ? * * *)

テストイベント

{
  "env-name": "app-env"
}

参照

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0