LoginSignup
1
1

More than 1 year has passed since last update.

CloudWatch Logs ロググループの保持期間を自動で設定するやつ

Posted at

概要

AWSを利用していると意識していないのに、CloudWatch Logsのロググループが作成されていて、保持期間の設定なしの状態で保管されているので結構な費用が発生している何てことあると思います。
ということで、新規ロググループが作成されたら自動で保持期間を設定するマシンを作成します!

下図が今回のステートマシン……もはやシンプルすぎて別にステートマシンじゃなくてもいいんですが…
stepfunctions_graph.png

やってることはシンプルで、ロググループの作成をEventBridgeで検知して、ステートマシンを呼び出して保持期間を設定します。
細かい手順は割愛して、CloudFormationテンプレートを用意したので、使ってみてください~

CloudFormationテンプレート

パラメータは以下のとおりです!

設定項目 設定値
StateMachineName StepFunctionsのステートマシン名
RetentionDate ロググループに設定する保持期間
AWSTemplateFormatVersion: '2010-09-09'

Description: State machine to Change retention policy

Parameters:
  StateMachineName:
    Default: PutRetentionPolicyMachine
    Type: String
  RetentionDate:
    Default: 30
    Type: Number
    AllowedValues:
      - 1
      - 3
      - 5
      - 7
      - 14
      - 30
      - 60
      - 90
      - 120
      - 150
      - 180
      - 365
      - 400
      - 545
      - 731
      - 1827
      - 3653

Resources:
  XRayAccessPolicy:
    Type: 'AWS::IAM::Policy'
    Properties:
      PolicyName: !Sub XRayAccessPolicy-${AWS::StackName}
      PolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Action:
              - 'xray:PutTraceSegments'
              - 'xray:PutTelemetryRecords'
              - 'xray:GetSamplingRules'
              - 'xray:GetSamplingTargets'
            Resource: '*'
      Roles:
        - !Ref MyStateMachineRole
  
  PutRetentionPolicyAccess:
    Type: 'AWS::IAM::Policy'
    Properties:
      PolicyName: !Sub PutRetentionPolicy-${AWS::StackName}
      PolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Action:
              - 'logs:PutRetentionPolicy'
            Resource: '*'
      Roles:
        - !Ref MyStateMachineRole

  MyStateMachineRole:
    Type: "AWS::IAM::Role"
    Properties: 
      AssumeRolePolicyDocument: 
        Version: "2012-10-17"
        Statement: 
          - 
            Effect: "Allow"
            Principal: 
              Service: 
                - "states.amazonaws.com"
            Action: 
              - "sts:AssumeRole"
      Path: "/"

  MyStateMachine:
    Type: AWS::StepFunctions::StateMachine
    Properties:
      StateMachineName: !Ref StateMachineName
      Definition:
        Comment: A description of my state machine
        StartAt: PutRetentionPolicy
        States:
          PutRetentionPolicy:
            Type: Task
            End: true
            Parameters:
              LogGroupName.$: $.detail.requestParameters.logGroupName
              RetentionInDays: !Ref RetentionDate
            Resource: arn:aws:states:::aws-sdk:cloudwatchlogs:putRetentionPolicy
      RoleArn: !GetAtt MyStateMachineRole.Arn

  EventBridgeRule:
    Type: AWS::Events::Rule
    Properties: 
      EventBusName: default
      EventPattern:
        source:
          - aws.logs
        detail-type:
          - "AWS API Call via CloudTrail"
        detail:
          eventSource:
            - logs.amazonaws.com
          eventName:
            - CreateLogGroup
      Name: !Sub CreateLogGroupEventRule-${AWS::StackName}
      Targets: 
        - Arn: !GetAtt MyStateMachine.Arn
          Id: !Sub CreateLogGroupEventRule-${AWS::StackName}
          RoleArn: !GetAtt EventRole.Arn

  EventRole:
    Type: 'AWS::IAM::Role'
    Properties:
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Principal:
              Service: !Sub events.amazonaws.com
            Action: 'sts:AssumeRole'
      Path: /
      Policies:
        - PolicyName: !Sub PutEventsPolicy-${AWS::StackName}
          PolicyDocument:
            Version: 2012-10-17
            Statement:
              - Effect: Allow
                Action:
                  - 'states:StartExecution'
                Resource:
                  - !GetAtt MyStateMachine.Arn

終わりに

こういうコスパのいいもの増やしていきたいですね!
以上!!

1
1
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
1
1