ã¯ããã«
ãã㯠はてなエンジニア - Qiita Advent Calendar 2024 - Qiita 14æ¥ç®ã®è¨äºã§ããæ¨æ¥ã¯ id:ymse ã® compose.yamlはマージができるし、YAMLのtagでその挙動をコントロールできる - 風に吹かれても ã§ããã
id:utgwkk ã§ããæ¥é±æ«ã«éå½ã«è¡ããã¨ã決ã¾ãã¾ãããããã¯ãã¦ããã¦â¦â¦
å æ¥ (12/8)ãISUCON14ãéå¬ããã¾ãããISUCON14ã«åå ãããã¨ã®è¨é²ã¯ä»¥ä¸ã®è¨äºãã覧ãã ããã
話ã¯å¤ããã¾ãããã¿ãªãã¾ã¯AWS Step Functionsã使ã£ã¦ãã¾ããï¼ ä»æ¥ã¯ISUCONã®ææ³æ¦ãæ¯ããStep Functionsã«ã¤ãã¦ã話ããã¾ãã
ã§ãããã®
ãããªãã§ãã以ä¸ã®ã¹ãã¼ããã·ã³ãã覧ãã ãããã©ãã§ããï¼
ãã®ã¹ãã¼ããã·ã³ã¯ãISUCONã®ç«¶æç¨EC2ã¤ã³ã¹ã¿ã³ã¹ãèµ·åã»åæ¢ããããã®ã¹ãã¼ããã·ã³ã§ãããããªã®AWSã³ã³ã½ã¼ã«ããã½ã¡ã½ã¡èµ·åã»åæ¢ãã¦åã£ãããããªãï¼ï¼ ã¨æãããæ¹ãããã§ãããããã¾ãããè¨ããã¡ãã£ã¨èªãã§ãã£ã¦ãã ããã
使ãæ¹
EventBridge Schedulerçµç±ã§èµ·åãã
ä»äºä¸ã¯ãããã«ææ³æ¦ããã£ã¦ããå ´åã§ã¯ãªã (ãªããªãä»äºããããã)ããã©ä»äºãçµãã£ããããã«ææ³æ¦ã«åããããããããã¨ã¤ã³ã¹ã¿ã³ã¹ã®å®è¡è²»ç¨ãã±ããããâ¦â¦ããããã¨ãã¯EventBridge Schedulerã§ã¹ãã¼ããã·ã³ã®èµ·åãã¹ã±ã¸ã¥ã¼ã«ãã¦ããã¨ä¾¿å©ã§ãã
å¤19æã«EC2ã¤ã³ã¹ã¿ã³ã¹ãèµ·åããã¹ã±ã¸ã¥ã¼ã«ã¨ãæ10æã«EC2ã¤ã³ã¹ã¿ã³ã¹ãåæ¢ããã¹ã±ã¸ã¥ã¼ã«ã®ä¸¡æ¹ãç¨æãããã¨ããã®ããã£ã¦ãã¾ããå®æãéããã¨ã¹ãã¼ã¸ã³ã°ç°å¢ãåæ¢ããã®ã®éã¿ããã§é¢ç½ãã§ããã
EventBridge Schedulerã§ã¯ã¹ã±ã¸ã¥ã¼ã«ã®çµäºæ¥æãè¨å®ã§ããã®ã§ãææ³æ¦ç¨ã«ããã·ã¥ãã¼ãã»ãã³ããã¼ã«ã¼ãå ¬éãããæéãçµãã£ããEC2ã¤ã³ã¹ã¿ã³ã¹ãèµ·åããã¹ã±ã¸ã¥ã¼ã«ãçµäºããããã¨ããè¨å®ãå ¥ãããã¨ã§ãã¤ã³ã¹ã¿ã³ã¹ã®å®è¡è²»ç¨ããããã®ãé²ãã¾ããã¾ãæçµçã«ã¯CloudFormationã®ã¹ã¿ãã¯ãã¨æ¶ãã®ãããã¨æãã¾ããâ¦â¦ã
EventBridge+SNSçµç±ã§éç¥ãã
EventBridgeã«ä»¥ä¸ã®ãããªã¤ãã³ããã¿ã¼ã³ã§SNSã«éç¥ããè¨å®ãç¨æãã¾ããã¹ãã¼ããã·ã³ã®å®è¡åã test-
ããå§ããå ´åã¯éç¥ããªãããã«ãã¦ãã¾ãããããããã¨ã§ãã¹ãå®è¡ã®éç¥ãSlackã«æµãã¾ããã®ãé²ãã¾ãã
{ "source": ["aws.states"], "detail-type": ["Step Functions Execution Status Change"], "detail": { "status": ["SUCCEEDED", "TIMED_OUT", "FAILED", "ABORTED"], "stateMachineArn": ["ã¹ãã¼ããã·ã³ã®ARN"], "name": [{ "anything-but": { "prefix": ["test-"] } }] } }
SNSã®ãµãã¹ã¯ãªãã·ã§ã³ã«AWS ChatBotãç»é²ããSlackãã£ã³ãã«ã«ã¹ãã¼ããã·ã³ã®å®è¡ãéç¥ããããã«ãã¦ãã¾ããEventBridgeâSNSâChatBot ã¨ããæãã§ãSlackéç¥ãããããã®ç»å ´äººç©ãããå¤ãæ°ããã¾ããããããªããã§ããããã
AWS ChatBotçµç±ã§èµ·åãã
ISUCONç¨ã®ãã©ã¤ãã¼ããªSlackãã£ã³ãã«ã«ã¹ãã¼ããã·ã³ã®å®è¡éç¥ãæµãã¦ããã®ã§ããããã®ã¡ãã»ã¼ã¸ãã競æç¨EC2ã¤ã³ã¹ã¿ã³ã¹ãèµ·åã»åæ¢ã§ããããã«ãã¦ããã¾ãããã®ããã«ãã¦ãããã¨ã§ããã¼ã ã¡ã³ãã¼ãAWSã¢ã«ã¦ã³ãã«ãã°ã¤ã³ã§ããªãã¦ããSlackã«æµãã¦ããã¡ãã»ã¼ã¸ã®ãã¿ã³ãæ¼ãã ãã§ææ³æ¦ã«åããããã¾ãã
éç¥ã¡ãã»ã¼ã¸ã®ãã³ãã¼ã¬ã¼ã¡ãã¥ã¼ã§éãã¢ã¼ãã«ããCreate a new custom action buttonãæ¼ããã¨ã§ãä»»æã®AWS CLIã®ã³ãã³ããå®è¡ãããã¿ã³ãè¨ç½®ã§ãã¾ããã¹ãã¼ããã·ã³ãå®è¡ãããªã stepfunctions start-execution
ã³ãã³ããè¨è¿°ãã¦ä¿åããã°ããã§ãã
ã¹ãã¼ããã·ã³ã®è§£èª¬
å®ç¾©
以ä¸ã«ãã®ã¹ãã¼ããã·ã³ã®å®ç¾©ãè¨è¿°ãã¾ãããªãã©ã¤å¦çã¯çç¥ãã¦ãã¾ãã
{ "StartAt": "DescribeInstances", "States": { "DescribeInstances": { "Type": "Task", "Parameters": { "Filters": [ { "Name": "tag:aws:cloudformation:stack-id", "Values": [ "ISUCON競æç¨ã¤ã³ã¹ã¿ã³ã¹ã®CloudFormationã¹ã¿ãã¯ID" ] } ] }, "Resource": "arn:aws:states:::aws-sdk:ec2:describeInstances", "ResultSelector": { "InstanceIds.$": "$.Reservations[*].Instances[*].InstanceId" }, "Next": "DetermineNextInstanceStatus", }, "DetermineNextInstanceStatus": { "Type": "Choice", "Choices": [ { "Variable": "$$.Execution.Input.DesiredState", "StringEquals": "running", "Next": "StartInstances" }, { "Variable": "$$.Execution.Input.DesiredState", "StringEquals": "stopped", "Next": "StopInstances" } ], "Default": "Nop" }, "Nop": { "Type": "Pass", "End": true, "Result": { "Nop": true } }, "StartInstances": { "Type": "Task", "Parameters": { "InstanceIds.$": "$.InstanceIds" }, "Resource": "arn:aws:states:::aws-sdk:ec2:startInstances", "End": true }, "StopInstances": { "Type": "Task", "Parameters": { "InstanceIds.$": "$.InstanceIds" }, "Resource": "arn:aws:states:::aws-sdk:ec2:stopInstances", "End": true } } }
ãã£ã¦ãããã¨
ãã£ã¦ãããã¨ã¯ç°¡åã§ãã
- DescribeInstances APIãå©ããISUCON14ã®ç«¶æç¨EC2ã¤ã³ã¹ã¿ã³ã¹IDãåæãã
- ã¹ãã¼ããã·ã³ã¸ã®å ¥åã«å¿ãã¦ãåæãã競æç¨EC2ã¤ã³ã¹ã¿ã³ã¹ãèµ·åãããã¯åæ¢ãã
ã¿ã©ãã
ResultSelectorã§ã¤ã³ã¹ã¿ã³ã¹IDã®é åãå ¥æãã
DescribeInstances APIã®å®è¡çµæã¯ããã®ããããã£ããæ¸ãã¨*1以ä¸ã®ãããªå½¢å¼ã®JSONã«ãªã£ã¦ãã¾ãã
{ "Reservations": [ { "Instances": [ { "InstanceId": "i-0001", // çç¥ } ] }, // çç¥ ] }
Reservations
ãã£ã¼ã«ãã®é
åã®åè¦ç´ ã®ãªãã¸ã§ã¯ãã«ãã Instances
ãã£ã¼ã«ãã®é
åã®åè¦ç´ ã®ãªãã¸ã§ã¯ãã® InstanceId
ãã£ã¼ã«ãã®å¤ãã¤ã³ã¹ã¿ã³ã¹IDã§ããããã使ã£ã¦EC2ã¤ã³ã¹ã¿ã³ã¹ãèµ·åã»åæ¢ããããã®APIãå¼ã³åºãã¾ãã
ã¨ããã§ãEC2ã¤ã³ã¹ã¿ã³ã¹ãèµ·åã»åæ¢ããããã®APIã«ã¯ã¤ã³ã¹ã¿ã³ã¹IDãè¤æ°æ¸¡ãã¦ä¸æ¬æä½ãããã¨ãã§ãã¾ããããã使ãããã®ã§ãªãã¨ã工夫ãã¾ãã
å
ã«çãããæ¸ãã¦ããã¨ãResultSelectorã使ã£ã¦DescribeInstances APIãè¿ãJSONããã¤ã³ã¹ã¿ã³ã¹IDã®é
åãå¾ããã¨ãã§ãã¾ãã$.Reservations[*].Instances[*].InstanceId
ã¨ããJSONãã¹ãæ¸ãã¦ããç®æãããã«å¯¾å¿ãã¾ãã
ããã㦠DescribeInstances
stateã®åºåã以ä¸ã®ãããªã¤ã³ã¹ã¿ã³ã¹IDã®é
å (ãæã¤ãªãã¸ã§ã¯ã) ã«ã§ãã¾ãããããã¾ã§æ¥ããããã ãã§ããã
{ "InstanceIds": [ "i-0001", "i-0002", "i-0003" ] }
$$.Execution.Input
ã§ã¹ãã¼ããã·ã³ãã®ãã®ã¸ã®å
¥åãå¾ã
$$
ããå§ã¾ãJSONãã¹ã¯context objectãæãã¦ãã¾ããcontext objectã使ããã¨ã§ãã¹ãã¼ããã·ã³ã®å®è¡å
¨ä½ã«ã¾ã¤ããæ
å ±ãä»»æã®stateã§åç
§ã§ãã¾ãã
ã¹ãã¼ããã·ã³ã®å
¥åã® DesiredState
ãã£ã¼ã«ãã®å¤ã«å¿ãã¦EC2ã¤ã³ã¹ã¿ã³ã¹ãèµ·åãããã»åæ¢ããã ã®åå²ãæ¸ãããã«ä½¿ã£ã¦ãã¾ããããããã¨stateãã¨ã«å
ã®å
¥åãå¼ãç¶ããã¨ãèããªãã¦æ¸ãã§ä¾¿å©ã§ããã
éä¸ã®ã¹ãã¼ãã®åºåãå¼ãåãããã¨ãã¯å¤æ°ã使ããã¨æãã¾ãã
ãªãStep Functionsã§æ¸ãã®ã
AWSã®å種APIãé çªã«å©ãã®ã¯Lambdaé¢æ°ã§ãã§ããã®ã§ã¯ï¼ ã¨æãããããããã¾ãããAmazon States Languageãé å¼µã£ã¦è¦ããããã¯ã馴æã¿ã®ããããã°ã©ãã³ã°è¨èªã§å®è£ ã§ããæ¹ãå®æããã®ã¯æ©ãã¨æãã¾ããã¾ããå®è¡é »åº¦ã«ãã£ã¦ã¯Step FunctionsãããLambdaã®ã»ããå§åçã«å®ããªããã¨ãããã§ããããããã§ããªãStep Functionsã§æ¸ãã®ãï¼
é¢ç½ã
ã¹ãã¼ããã·ã³ãæ¸ãã¨ãã¯ãæ®æ®µããã°ã©ã ãæ¸ãã¨ãã¨ã¯ã¾ãå¥ã®çèãæ±ãããã¾ããå ¥åºåããã¾ãå å·¥ãããæã¡åãããããã«ã¯ã©ãããã®ããç¶æ é·ç§»æ°ãæå°ã«ã§ãããããªã©ãæå¤ã¨ãããã¿è¦ç´ ããã£ã¦é¢ç½ãã§ãã
ãã¼ã¿ãå å·¥ããã ãã®Lambdaé¢æ°ã使ããªããã¿ãããªç¸ããå ¥ããã®ãããã§ãããå ¥åºåã®å å·¥ãLambdaé¢æ°ãªãã§éæã§ããã¨è³æ±ãããããåºã¾ããã¨ã¯ãããã³ã¹ããæé©åããã«ã¯Lambdaé¢æ°ãéãã»ããå®ããªãå ´åãããã ããããStep Functionsã ãã§ã¯å®ç¾ã§ããªããã¨ãåºã¦ããã¨æãã®ã§ãè¦ã¯ãã©ã³ã¹ã§ããã
è¨èªã©ã³ã¿ã¤ã ã®EoLãæ°ã«ããªãã¦ãã
ããã°ã©ãã³ã°è¨èªã«ã¯ãå¦çç³»ã®EoLãã¤ããã®ã§ããLambdaé¢æ°ã®ã©ã³ã¿ã¤ã ã ã£ã¦ããã§ããé·ããã¨æ¾ç½®ãã¦å®å®ç¨¼åãã¦ããLambdaé¢æ°ã®ã©ã³ã¿ã¤ã ããã¤ãã¯å¯¿å½ãè¿ãã¾ããå¦çç³»ã®ãã¼ã¸ã§ã³ã¢ããã«è¿½å¾ããã³ã¹ããæããªããã°ãªãã¾ããã
Step Functionsã§æ¸ãã¦ããã¨ãAWSã®APIå¼ã³åºãã®ããã®Lambdaé¢æ°ãªã©ã¯ä¸è¦ã«ãªãã¾ãããã®Lambdaé¢æ°ã®ã©ã³ã¿ã¤ã ã®ãã¼ã¸ã§ã³ãåæã«ä¸ãã£ã¦ããããã ã£ãâ¦â¦ãªã©ã¨æ°ã«ããå¿ è¦ã¯ããã¾ããã
ãªãã©ã¤å¦çãç°¡åã«æ¸ãã
DescribeInstances APIãå
é¨ã¨ã©ã¼ã§å¤±æããã¨ãã«ãã¸ãã¿ã¼ãå
¥ãã¤ã¤ãªãã©ã¤ãããã¨ããå¦çãããããæ¸ãã¾ããï¼ Step Functionsã®å ´åã¯ãstateå®ç¾©ã« Retry
ãã£ã¼ã«ããæ¸ãã ãã§ãããªã«ãã£ã¦ããã¾ãã
ææãç¥ã
ãã®ç¯ã¯Step FunctionsãAWSã«éããªã話é¡ã«ãªãã®ã§ããããããããªéå ·ãçµã¿åããã¦ä½ãã©ãã¾ã§ã»ã©ããããã®å´åã§å®ç¾ã§ããã®ãããç¥ã£ã¦ããã®ãããã¨æãã¾ããStep Functionsãéã®å¼¾ä¸¸ã«è¦ãã¦ãããã¡ã«ããããä½ã£ã¦ã¿ã¦ããããªã«ç°¡åã«å®ç¾ã§ããã¨ããå®ã¯Step Functionsãéãå¿ è¦ããªãã£ãã¨ããããããçµé¨ãç©ãã§ããã®ãããã§ãããã
ãããã«
ã¨ã¯ããçã®JSONã§ã¹ãã¼ããã·ã³ãè¨è¿°ããã®ã¯ãããã«å¤§å¤ã§ããã¡ãã£ã¨åå²ãã¦ç´åã«é²ãããããªããããã©ãåå²ãè¤éã«ãªã£ããJSONã ã¨ãããã¯èªç¥ã®éçãæ¥ããããã¶ã¤ã³ã¨ãã£ã¿ã§å¯è¦åãã¦ä½ãã¤ã¤IaCããããªã£ããJSONã¨ã¯ã¹ãã¼ãããã¨ãï¼
ã¿ãªãã¾ã¯ã©ããã£ã¦ã¹ãã¼ããã·ã³ãæ¸ãã¦ãã¾ããï¼ AWS CDKã§ã¹ãã¼ããã·ã³ãå®ç¾©ã§ãããã¤ã¨ããæ´»ç¨ããã¨ãã£ã¨è¦éãããæ¸ããã®ããªã
æ ªå¼ä¼ç¤¾ã¯ã¦ãªã§ã¯ããµã¼ãã¼ã¬ã¹æè¡ã§ãã£ãããã·ã¹ãã ãä½ãããæ¹ãåéãã¦ãã¾ãã
ææ¥ã®æ å½ã¯ id:mizdra ã§ãã
*1:ã¡ããã¨ããå®ç¾©ã¯ DescribeInstances - Amazon Elastic Compute Cloud ãåç §ãã¦ãã ãã