Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloudformation template to push cloudwatch logs to elasticsearch

Tags:

I am looking for a Cloudformation template to push cloudwatch logs to elasticsearch in another account. Even to the same account would be ok and I can update that.

Seems like a standard problem but haven't seen any template which automates the steps described in https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_ES_Stream.html.

like image 445
instanceOfObject Avatar asked Nov 18 '18 21:11

instanceOfObject


People also ask

How do I push Elasticsearch to CloudWatch logs?

Go to the AWS CloudWatch console and click on Logs at the left most; select the CloudTrail Log group that we just created earlier, and click on Actions and select Stream to Amazon Elasticsearch Service.


1 Answers

I should have updated the answer. This is what I wrote which is the minimalistic version of streaming logs from CW to Elastic Search.


  Resources:
      LambdaElasticSearchExecutionRole:
          Properties:
            AssumeRolePolicyDocument:
              Statement:
              - Action: ['sts:AssumeRole']
                Effect: Allow
                Principal:
                  Service: [lambda.amazonaws.com]
              Version: '2012-10-17'
            Policies:
            - PolicyDocument:
                Statement:
                - Action: ['es:ESHttpPost']
                  Effect: Allow
                  Resource: "*"
                - Action: ['logs:CreateLogGroup', 'logs:CreateLogStream', 'logs:PutLogEvents']
                  Effect: Allow
                  Resource: "arn:aws:logs:*:*:*"
                - Action: ['lambda:InvokeFunction']
                  Effect: Allow
                  Resource: "arn:aws:logs:*:*:*"
                Version: '2012-10-17'
              PolicyName: lambdaRoleElasticSearchStreaming
          Type: AWS::IAM::Role

      ESStreamingLambda:
         Type: AWS::Lambda::Function
         DependsOn: LambdaElasticSearchExecutionRole
         Properties:
           Handler: index.handler
           Role:
             Fn::GetAtt: [LambdaElasticSearchExecutionRole, Arn]
           Code:
             S3Bucket: {'Fn::Sub': 'do-not-delete-cw-es-log-streaming-lamda-${AWS::Region}'}
             S3Key: LogsToElasticsearch.zip
           Runtime: nodejs4.3

      LambdaPermissionForCWInvokation:
         DependsOn: ESStreamingLambda
         Type: AWS::Lambda::Permission
         Properties:
          Action: lambda:InvokeFunction
          FunctionName:
            Fn::GetAtt: [ESStreamingLambda, Arn]
          Principal: {'Fn::Sub': 'logs.${AWS::Region}.amazonaws.com'}
  APILogsToElasticSearchSubscriptionFilter:
    Type: AWS::Logs::SubscriptionFilter
    DependsOn: [ESStreamingLambda, LambdaPermissionForCWInvokation]
    Properties:
      DestinationArn:
        Fn::GetAtt: [ESStreamingLambda, Arn]
      FilterPattern: ''
      LogGroupName: {Ref: LambdaLogGroup}
```java
like image 145
instanceOfObject Avatar answered Nov 15 '22 04:11

instanceOfObject