改善
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"
}
参照
- https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/elasticbeanstalk.html
- https://docs.aws.amazon.com/ja_jp/elasticbeanstalk/latest/dg/environment-configuration-methods-after.html
- https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-beanstalk-environment.html