1

Cloudtrail 默认日志可以流式传输到弹性搜索域,如此镜像所显示。如何使用 cloudformation 模板实现这一目标?

在此处输入图像描述

4

1 回答 1

6

更新:

如果您使用的是 aws-cli,请在此处查看我的回答。


好吧,经过几个小时的探索和阅读大量文档后,我终于成功创建了这个模板。

设计师概述:

在此处输入图像描述

为了将流日志启用到 elasticsearch,我们需要创建以下资源:

  1. lambda 函数会将来自 cloudwatch 日志组的日志转发到 Elasticsearch。
  2. 相关 IAM 角色,用于从 cloudwatch 获取日志并插入到 Elasticsearch。
  3. Lambda 权限-The AWS::Lambda::Permission resource grants an AWS service or another account permission to use a function允许 cloudwatch 日志组触发 lambda。
  4. 订阅过滤器 -The AWS::Logs::SubscriptionFilter resource specifies a subscription filter and associates it with the specified log group. Subscription filters allow you to subscribe to a real-time stream of log events and have them delivered to a specific destination.

模板使用:

  1. 从我的Github 页面下载 LogsToElasticsearch.zip 。
  2. 使用您的 Elasticseatch url更新var endpoint = '${Elasticsearch_Endpoint}';index.js,例如 - 'search-xxx-yyyy.eu-west-1.es.amazonaws.com';
  3. 将 zip 文件复制到将在模板 (LambdaArtifactBucketName) 中使用的 s3 存储桶。
  4. 填写相关参数 - 您可以找到每个资源的描述。

模板 YAML:

AWSTemplateFormatVersion: 2010-09-09
Description: Enable logs to elasticsearch
Parameters:
  ElasticsearchDomainName:
    Description: Name of the Elasticsearch domain that you want to insert logs to
    Type: String
    Default: amitb-elastic-domain
  CloudwatchLogGroup:
    Description: Name of the log group you want to subscribe
    Type: String
    Default: /aws/eks/amitb-project/cluster
  LambdaName:
    Description: Name of the lambda function
    Type: String
    Default: amitb-cloudwatch-logs
  LambdaRole:
    Description: Name of the role used by the lambda function
    Type: String
    Default: amit-cloudwatch-logs-role
  LambdaArtifactBucketName:
    Description: The bucket where the lambda function located
    Type: String
    Default: amit-bucket
  LambdaArtifactName:
    Description: The name of the lambda zipped file
    Type: String
    Default: LogsToElasticsearch.zip
  VPC:
    Description: Choose which VPC the Lambda-functions should be deployed to
    Type: 'AWS::EC2::VPC::Id'
    Default: vpc-1111111
  Subnets:
    Description: Choose which subnets the Lambda-functions should be deployed to
    Type: 'List<AWS::EC2::Subnet::Id>'
    Default: 'subnet-123456789,subnet-123456456,subnet-123456741'
  SecurityGroup:
    Description: Select the Security Group to use for the Lambda-functions
    Type: 'List<AWS::EC2::SecurityGroup::Id>'
    Default: 'sg-2222222,sg-12345678'
Resources:
  ExampleInvokePermission:
    Type: 'AWS::Lambda::Permission'
    DependsOn: ExampleLambdaFunction
    Properties:
      FunctionName:
        'Fn::GetAtt':
          - ExampleLambdaFunction
          - Arn
      Action: 'lambda:InvokeFunction'
      Principal: !Sub 'logs.${AWS::Region}.amazonaws.com'
      SourceArn: !Sub >-
        arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:${CloudwatchLogGroup}:*
      SourceAccount: !Ref 'AWS::AccountId'
  LambdaExecutionRole:
    Type: 'AWS::IAM::Role'
    Properties:
      RoleName: !Ref LambdaRole
      ManagedPolicyArns:
        - 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action:
              - 'sts:AssumeRole'
      Path: /
      Policies:
        - PolicyName: lambda-to-es-via-vpc-policy
          PolicyDocument:
            Version: 2012-10-17
            Statement:
              - Effect: Allow
                Action:
                  - 'es:*'
                Resource:
                  - !Sub >-
                    arn:aws:es:${AWS::Region}:${AWS::AccountId}:domain/${ElasticsearchDomainName}
        - PolicyName: logs-and-ec2-permissions
          PolicyDocument:
            Version: 2012-10-17
            Statement:
              - Effect: Allow
                Action:
                  - 'ec2:CreateNetworkInterface'
                  - 'ec2:DescribeNetworkInterfaces'
                  - 'ec2:DeleteNetworkInterface'
                  - 'logs:CreateLogGroup'
                  - 'logs:CreateLogStream'
                  - 'logs:PutLogEvents'
                Resource: '*'
  ExampleLambdaFunction:
    Type: 'AWS::Lambda::Function'
    DependsOn: LambdaExecutionRole
    Properties:
      Code:
        S3Bucket: !Ref LambdaArtifactBucketName
        S3Key: !Ref LambdaArtifactName
      FunctionName: !Ref LambdaName
      Handler: !Sub '${LambdaName}.handler'
      Role:
        'Fn::GetAtt':
          - LambdaExecutionRole
          - Arn
      Runtime: nodejs8.10
      Timeout: '300'
      VpcConfig:
        SecurityGroupIds: !Ref SecurityGroup
        SubnetIds: !Ref Subnets
      MemorySize: 512
  SubscriptionFilter:
    Type: 'AWS::Logs::SubscriptionFilter'
    DependsOn: ExampleInvokePermission
    Properties:
      LogGroupName: !Ref CloudwatchLogGroup
      FilterPattern: '[host, ident, authuser, date, request, status, bytes]'
      DestinationArn:
        'Fn::GetAtt':
          - ExampleLambdaFunction
          - Arn

结果:

在此处输入图像描述

在此处输入图像描述

云观察日志: 在此处输入图像描述

希望你觉得它有帮助。

2020 年 2 月 9 日更新:

node.js 8.10 现在已弃用,您应该使用 node.js 10 或 12。

https://docs.aws.amazon.com/lambda/latest/dg/runtime-support-policy.html

在此处输入图像描述

于 2019-10-25T12:52:23.503 回答