time ec2 = boto3.client('ec2',region_name='ap-northeast-1') ssm = boto3.client('ssm',region_name='ap-northeast-1') def lambda_handler(event, context): target=[{ 'Key': 'tag:HAproxy', 'Values': ['target'] }] #HAproxyの流量制御状態確認 check = ssm.send_command( #InstanceIds = instances, Targets=target, DocumentName = "AWS-RunShellScript", Parameters = { "commands": [ "/opt/infra/script/bin/checkConf.sh" ], "executionTimeout": ["30"] }, ) command_id = check['Command']['CommandId'] time.sleep(2) res = ssm.list_command_invocations( CommandId = command_id, Details = True ) invocations = res['CommandInvocations'] status = invocations[0]['Status'] if status == "Failed": print("RunCommand実行エラー") account = invocations[0]['CommandPlugins'][0]['Output'] print("status: "+account) #流量制御状態切替 if account == "open¥n": ssm.send_command( #InstanceIds = instances, Targets=target, DocumentName = "AWS-RunShellScript", Parameters = { "commands": [ "/opt/infra/script/bin/proxyOpenClose.sh close" ], "executionTimeout": ["30"] }, ) print("open状態からclose状態に切り替えました") elif account=="close¥n": print("すでにclose状態です") elif account=="error¥n": print("異常終了しました") Lambdaには、2つ関数を設定する。 ①FlowControl_close:流量制御発動用のスクリプトを実行。 ②FlowControl_open:流量制御解除用のスクリプトを実行。 以下にFlowControl_closeで実行するサンプルプログラムを示す。