SumOfUs/Champaign

View on GitHub
.ebextensions/cwl-webrequest-metrics.config

Summary

Maintainability
Test Coverage
###############################################################################
## Option Settings
##    Namespace: "aws:elasticbeanstalk:application:environment"
##    OptionName: WebRequestCWLogGroup
##       Default:   <EnvironmentName>-webrequests
##    Description: This is the name of the cloudwatch log group for web requests (access log)
##
## To get an SNS alert, add a subscription to the Elastic Beanstalk Notification
##   topic.  (e.g. set your Notificiation Endpoint to your email address)
##
## Metrics
##  Namespace:  ElasticBeanstalk/<EnvironmentName>
##  Metrics:    CWLHttp4xx  CWLHttp5xx
##
## Cloudwatch Alarms
##   CWLHttp5xxCount   - this alarm fires if the number of calls returning
##                        5xx response codes > 10
##   CWLHttp4xxPercent - this alarm fires if the percentage of calls returning
##                        4xx response codes > 10%
##
###############################################################################

# Nginx access log pattern:
# $remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"
Mappings:
  CWLogs:
    WebRequestLogGroup:
      LogFile: "/var/log/nginx/access.log"
      TimestampFormat: "%d/%b/%Y:%H:%M:%S %z"
    FilterPatterns:
      Http4xxMetricFilter: "[..., status=4*, size, referer, agent, xforward]"
      HttpNon4xxMetricFilter: "[..., status!=4*, size, referer, agent, xforward]"
      Http5xxMetricFilter: "[..., status=5*, size, referer, agent, xforward]"
      HttpNon5xxMetricFilter: "[..., status!=5*, size, referer, agent, xforward]"
    RailsProductionLogGroup:
      LogFile: "/var/log/eb-docker/containers/eb-current-app/production.log"
      TimestampFormat: "%d/%b/%Y:%H:%M:%S %z"
    EbCommandProcessorLogGroup:
      LogFile: "/var/log/eb-commandprocessor.log"
      TimestampFormat: "%d/%b/%Y:%H:%M:%S %z"
    EbVersionDeploymentLogGroup:
      LogFile: "/var/log/eb-version-deployment.log"
      TimestampFormat: "%d/%b/%Y:%H:%M:%S %z"


Outputs:
  WebRequestCWLogGroup:
    Description: "The name of the Cloudwatch Logs Log Group created for this environments web server access logs. You can specify this by setting the value for the environment variable: WebRequestCWLogGroup. Please note: if you update this value, then you will need to go and clear out the old cloudwatch logs group and delete it through Cloudwatch Logs."
    Value: { "Ref" : "AWSEBCloudWatchLogs8832c8d3f1a54c238a40e36f31ef55a0WebRequestLogGroup" }
  RailsProductionCWLogGroup:
    Description: "Log group for the production.log file containing Rails log stream"
    Value: { "Ref" : "AWSEBCloudWatchLogs8832c8d3f1a54c238a40e36f31ef55a0RailsProductionLogGroup" }
  EbCommandProcessorCWLogGroup:
    Description: "Log group for eb-commandprocessor.log with logs for execution of deploy time scripts"
    Value: { "Ref" : "AWSEBCloudWatchLogs8832c8d3f1a54c238a40e36f31ef55a0EbCommandProcessorLogGroup" }
  EbVersionDeploymenCWtLogGroup:
    Description: "Log group for eb-version-deployment.log that contain information about deployment and application versions."
    Value: { "Ref" : "AWSEBCloudWatchLogs8832c8d3f1a54c238a40e36f31ef55a0EbVersionDeploymentLogGroup" }


Resources :
  AWSEBCloudWatchLogs8832c8d3f1a54c238a40e36f31ef55a0WebRequestLogGroup:    ## Must have prefix:  AWSEBCloudWatchLogs8832c8d3f1a54c238a40e36f31ef55a0
    Type: "AWS::Logs::LogGroup"
    DependsOn: AWSEBBeanstalkMetadata
    DeletionPolicy: Retain     ## this is required
    Properties:
      LogGroupName:
        "Fn::GetOptionSetting":
          Namespace: "aws:elasticbeanstalk:application:environment"
          OptionName: WebRequestCWLogGroup
          DefaultValue: {"Fn::Join":["-", [{ "Ref":"AWSEBEnvironmentName" }, "webrequests"]]}
      RetentionInDays: 14

  AWSEBCloudWatchLogs8832c8d3f1a54c238a40e36f31ef55a0RailsProductionLogGroup:
    Type: "AWS::Logs::LogGroup"
    DependsOn: AWSEBBeanstalkMetadata
    DeletionPolicy: Retain
    Properties:
      LogGroupName:
        "Fn::GetOptionSetting":
          Namespace: "aws:elasticbeanstalk:application:environment"
          OptionName: RailsProductionCWLogGroup
          DefaultValue: {"Fn::Join":["-", [{ "Ref":"AWSEBEnvironmentName" }, "railsproduction"]]}
      RetentionInDays: 14

  AWSEBCloudWatchLogs8832c8d3f1a54c238a40e36f31ef55a0EbCommandProcessorLogGroup:
    Type: "AWS::Logs::LogGroup"
    DependsOn: AWSEBBeanstalkMetadata
    DeletionPolicy: Retain
    Properties:
      LogGroupName:
        "Fn::GetOptionSetting":
          Namespace: "aws:elasticbeanstalk:application:environment"
          OptionName: EbCommandProcessorCWLogGroup
          DefaultValue: {"Fn::Join":["-", [{ "Ref":"AWSEBEnvironmentName" }, "ebcommandprocessor"]]}
      RetentionInDays: 14

  AWSEBCloudWatchLogs8832c8d3f1a54c238a40e36f31ef55a0EbVersionDeploymentLogGroup:
    Type: "AWS::Logs::LogGroup"
    DependsOn: AWSEBBeanstalkMetadata
    DeletionPolicy: Retain
    Properties:
      LogGroupName:
        "Fn::GetOptionSetting":
          Namespace: "aws:elasticbeanstalk:application:environment"
          OptionName: EbVersionDeploymentCWLogGroup
          DefaultValue: {"Fn::Join":["-", [{ "Ref":"AWSEBEnvironmentName" }, "ebversiondeployment"]]}
      RetentionInDays: 14

  ## Register the files/log groups for monitoring
  AWSEBAutoScalingGroup:
    Metadata:
      "AWS::CloudFormation::Init":
        CWLogsAgentConfigSetup:
          files:
            ## any .conf file put into /tmp/cwlogs/conf.d will be added to the cwlogs config (see cwl-agent.config)
            "/tmp/cwlogs/conf.d/nginx-access.conf":
              content : |
                [nginx-access_log]
                file = `{"Fn::FindInMap":["CWLogs", "WebRequestLogGroup", "LogFile"]}`
                log_group_name = `{ "Ref" : "AWSEBCloudWatchLogs8832c8d3f1a54c238a40e36f31ef55a0WebRequestLogGroup" }`
                log_stream_name = {instance_id}
                datetime_format = `{"Fn::FindInMap":["CWLogs", "WebRequestLogGroup", "TimestampFormat"]}`
              mode  : "000400"
              owner : root
              group : root

            "/tmp/cwlogs/conf.d/rails-logs.conf":
              content : |
                file = `{"Fn::FindInMap":["CWLogs", "RailsProductionLogGroup", "LogFile"]}`
                log_group_name = `{ "Ref" : "AWSEBCloudWatchLogs8832c8d3f1a54c238a40e36f31ef55a0RailsProductionLogGroup" }`
                log_stream_name = {instance_id}
                datetime_format = `{"Fn::FindInMap":["CWLogs", "RailsProductionLogGroup", "TimestampFormat"]}`
              mode  : "000400"
              owner : root
              group : root

            "/tmp/cwlogs/conf.d/eb-commandprocessor.conf":
              content : |
                file = `{"Fn::FindInMap":["CWLogs", "EbCommandProcessorLogGroup", "LogFile"]}`
                log_group_name = `{ "Ref" : "AWSEBCloudWatchLogs8832c8d3f1a54c238a40e36f31ef55a0EbCommandProcessorLogGroup" }`
                log_stream_name = {instance_id}
                datetime_format = `{"Fn::FindInMap":["CWLogs", "EbCommandProcessorLogGroup", "TimestampFormat"]}`
              mode  : "000400"
              owner : root
              group : root

            "/tmp/cwlogs/conf.d/eb-commandprocessor.conf":
              content : |
                file = `{"Fn::FindInMap":["CWLogs", "EbCommandProcessorLogGroup", "LogFile"]}`
                log_group_name = `{ "Ref" : "AWSEBCloudWatchLogs8832c8d3f1a54c238a40e36f31ef55a0EbCommandProcessorLogGroup" }`
                log_stream_name = {instance_id}
                datetime_format = `{"Fn::FindInMap":["CWLogs", "EbCommandProcessorLogGroup", "TimestampFormat"]}`
              mode  : "000400"
              owner : root
              group : root

  #######################################
  ## Cloudwatch Logs Metric Filters

  AWSEBCWLHttp4xxMetricFilter :
    Type : "AWS::Logs::MetricFilter"
    Properties :
      LogGroupName: { "Ref" : "AWSEBCloudWatchLogs8832c8d3f1a54c238a40e36f31ef55a0WebRequestLogGroup" }
      FilterPattern : {"Fn::FindInMap":["CWLogs", "FilterPatterns", "Http4xxMetricFilter"]}
      MetricTransformations :
        - MetricValue : 1
          MetricNamespace: {"Fn::Join":["/", ["ElasticBeanstalk", {"Ref":"AWSEBEnvironmentName"}]]}
          MetricName : CWLHttp4xx

  AWSEBCWLHttpNon4xxMetricFilter :
    Type : "AWS::Logs::MetricFilter"
    DependsOn : AWSEBCWLHttp4xxMetricFilter
    Properties :
      LogGroupName: { "Ref" : "AWSEBCloudWatchLogs8832c8d3f1a54c238a40e36f31ef55a0WebRequestLogGroup" }
      FilterPattern : {"Fn::FindInMap":["CWLogs", "FilterPatterns", "HttpNon4xxMetricFilter"]}
      MetricTransformations :
        - MetricValue : 0
          MetricNamespace: {"Fn::Join":["/", ["ElasticBeanstalk", {"Ref":"AWSEBEnvironmentName"}]]}
          MetricName : CWLHttp4xx

  AWSEBCWLHttp5xxMetricFilter :
    Type : "AWS::Logs::MetricFilter"
    Properties :
      LogGroupName: { "Ref" : "AWSEBCloudWatchLogs8832c8d3f1a54c238a40e36f31ef55a0WebRequestLogGroup" }
      FilterPattern : {"Fn::FindInMap":["CWLogs", "FilterPatterns", "Http5xxMetricFilter"]}
      MetricTransformations :
        - MetricValue : 1
          MetricNamespace: {"Fn::Join":["/", ["ElasticBeanstalk", {"Ref":"AWSEBEnvironmentName"}]]}
          MetricName : CWLHttp5xx

  AWSEBCWLHttpNon5xxMetricFilter :
    Type : "AWS::Logs::MetricFilter"
    DependsOn : AWSEBCWLHttp5xxMetricFilter
    Properties :
      LogGroupName: { "Ref" : "AWSEBCloudWatchLogs8832c8d3f1a54c238a40e36f31ef55a0WebRequestLogGroup" }
      FilterPattern : {"Fn::FindInMap":["CWLogs", "FilterPatterns", "HttpNon5xxMetricFilter"]}
      MetricTransformations :
        - MetricValue : 0
          MetricNamespace: {"Fn::Join":["/", ["ElasticBeanstalk", {"Ref":"AWSEBEnvironmentName"}]]}
          MetricName : CWLHttp5xx


  ######################################################
  ## Alarms

  AWSEBCWLHttp5xxCountAlarm :
    Type : "AWS::CloudWatch::Alarm"
    DependsOn : AWSEBCWLHttpNon5xxMetricFilter
    Properties :
      AlarmDescription: "Application is returning too many 5xx responses (count too high)."
      MetricName: CWLHttp5xx
      Namespace: {"Fn::Join":["/", ["ElasticBeanstalk", {"Ref":"AWSEBEnvironmentName"}]]}
      Statistic: Sum
      Period: 60
      EvaluationPeriods: 1
      Threshold: 10
      ComparisonOperator: GreaterThanThreshold
      AlarmActions:
        - "Fn::If":
            - SNSTopicExists
            - "Fn::FindInMap":
                - AWSEBOptions
                - options
                - EBSNSTopicArn
            - { "Ref" : "AWS::NoValue" }

  #AWSEBCWLHttp4xxPercentAlarm :
  #  Type : "AWS::CloudWatch::Alarm"
  #  DependsOn : AWSEBCWLHttpNon4xxMetricFilter
  #  Properties :
  #    AlarmDescription: "Application is returning too many 4xx responses (percentage too high)."
  #    MetricName: CWLHttp4xx
  #    Namespace: {"Fn::Join":["/", ["ElasticBeanstalk", {"Ref":"AWSEBEnvironmentName"}]]}
  #    Statistic: Average
  #    Period: 60
  #    EvaluationPeriods: 1
  #    Threshold: 0.10
  #    ComparisonOperator: GreaterThanThreshold
  #    AlarmActions:
  #      - "Fn::If":
  #          - SNSTopicExists
  #          - "Fn::FindInMap":
  #              - AWSEBOptions
  #              - options
  #              - EBSNSTopicArn
  #          - { "Ref" : "AWS::NoValue" }