Translate

2024年9月18日水曜日

既存の複数のEC2インスタンスを毎週月~金の8:58から18:02まで起動させておくCloud Formation定義ファイル

トライアンドエラーを繰り返し表題の定義ファイルをつくった。

ようやく動作したので、グログにのこしておく。

 

 忘備録として:

 

  • AWS EventBridgeを使っている(Lambdaでやる方法もある)
  • イベントバスはdefaultでないとスケジュール化できなかった
  • パラメータでInstanceIdを複数選択させるためにList<AWS::EC2::Instance::Id>を使うとStringに直接できなかったので、(負けた気がするが)Stringで書くことにした
  • 対象のEC2インスタンスはプライベートでもいいが、System Managerを使っているのでエンドポイントかNATでIGWに出ていける設定は必要(このテンプレート対象外)
  • 複数リージョンにまたがるEC2インスタンスは指定できない
  • AWS::Events::RuleにはなぜかTimeZone指定でAsia/Tokyoできなかった

 

AWSTemplateFormatVersion: '2010-09-09'
Description: >
  CloudFormation template for EC2 start/stop scheduling using EventBridge and Systems Manager.

Parameters:
  Project:
    Type: String
    Default: "ai-agent"
    Description: "The project name."
  Environment:
    Type: String
    Default: "demo"
    AllowedValues:
      - "demo"
      - "ita"
      - "itb"
      - "st"
      - "prod"
      - "dpsc"
    Description: "The environment name."
  Version:
    Type: String
    Default: "0.0.12"
    Description: "The version of the stack."
  Instances:
    Type: String
    Description: 'The list of EC2 Instance IDs to be managed. ex) ["i-xxxxxxx", "i-yyyyyyy"]'
  StartSchedule:
    Type: String
    Default: "cron(58 23 ? * Sun-Thu *)"
    Description: "Cron expression for the EC2 start schedule (UTC)."
  StopSchedule:
    Type: String
    Default: "cron(32 8 ? * Mon-Fri *)"
    Description: "Cron expression for the EC2 stop schedule (UTC)."

Resources:

  # IAM Role for EventBridge to interact with Systems Manager
  EC2StartStopRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: !Sub "${Project}-${Environment}-${Version}-ec2-start-stop-role"
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service: events.amazonaws.com
            Action: "sts:AssumeRole"
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/service-role/AmazonSSMAutomationRole
      Tags:
        - Key: Project
          Value: !Ref Project
        - Key: Environment
          Value: !Ref Environment
        - Key: Version
          Value: !Ref Version

  # Start Rule for EC2
  StartRule:
    Type: AWS::Events::Rule
    Properties:
      Name: !Sub "${Project}-${Environment}-${Version}-ec2-start-rule"
      ScheduleExpression: !Ref StartSchedule
      EventBusName: default
      Targets:
        - Arn: !Sub "arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:automation-definition/AWS-StartEC2Instance"
          RoleArn: !GetAtt EC2StartStopRole.Arn
          Id: StartTarget
          Input: !Sub '{"InstanceIds": ${Instances}}'


  # Stop Rule for EC2
  StopRule:
    Type: AWS::Events::Rule
    Properties:
      Name: !Sub "${Project}-${Environment}-${Version}-ec2-stop-rule"
      ScheduleExpression: !Ref StopSchedule
      EventBusName: default
      Targets:
        - Arn: !Sub "arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:automation-definition/AWS-StopEC2Instance"
          RoleArn: !GetAtt EC2StartStopRole.Arn
          Id: StopTarget
          Input: !Sub '{"InstanceIds": ${Instances}}'


Outputs:
  StartRuleName:
    Description: "The name of the EventBridge rule for EC2 start."
    Value: !Ref StartRule
  StopRuleName:
    Description: "The name of the EventBridge rule for EC2 stop."
    Value: !Ref StopRule
  IAMRoleName:
    Description: "The IAM role used by EventBridge for EC2 start/stop automation."
    Value: !Ref EC2StartStopRole

 

ちなみにこのCloudFormation定義をつくるのにLLM Chatモデル yi-coder でやり取りをひたすらしていたが、結構粘ってもLLMは完璧な正解を返してはくれなかった(InstanceIdの配列のところは、人力で完成させざるを得なかった)。


コード専門LLMでもかけないCloudFormation..難易度高いなあ..

 

以上

0 件のコメント:

既存の複数のEC2インスタンスを毎週月~金の8:58から18:02まで起動させておくCloud Formation定義ファイル

トライアンドエラーを繰り返し表題の定義ファイルをつくった。 ようやく動作したので、グログにのこしておく。    忘備録として:   AWS EventBridgeを使っている(Lambdaでやる方法もある) イベントバスはdefaultでないとスケジュール化できなかった パラメー...