SumOfUs/Champaign

View on GitHub
.ebextensions/cwl-setup.config

Summary

Maintainability
Test Coverage
########################################################################
##
########################################################################

Conditions:
  SNSTopicExists : { "Fn::Not" : [{ "Fn::Equals" : [ "", {"Fn::FindInMap" : ["AWSEBOptions", "options", "EBSNSTopicArn"]}]}]}    

Outputs:
  ElasticBeanstalkSNSTopicArn:
    Description: "The SNS Topic ARN for Elastic Beanstalk event notifications. Empty string if none specified."
    Value: { "Fn::FindInMap": ["AWSEBOptions", "options", "EBSNSTopicArn"] }


Resources:
  AWSEBAutoScalingGroup:
    Metadata:
      "AWS::CloudFormation::Init":
        configSets:
          "_OnInstanceBoot":
            "CmpFn::Insert":
              values:
                - CWLogsAgentClearConfig
                - CWLogsAgentConfigSetup
                - CWLogsAgentUpdaterInstallation
                - CWLogsAgentInstallation
          "CWLogsAgentInstallation":
            - CWLogsAgentClearConfig
            - CWLogsAgentConfigSetup
            - CWLogsAgentUpdaterInstallation
            - CWLogsAgentInstallation
          "CWLogsAgentConfigUpdate":
            - CWLogsAgentClearConfig
            - CWLogsAgentConfigSetup
            - CWLogsAgentUpdateConfig
        CWLogsAgentClearConfig:
          commands:
            01-clear-agent-config:
              command: rm -rf /tmp/cwlogs/conf.d
        CWLogsAgentConfigSetup:
          files:
            "/tmp/cwlogs/conf.d/general.conf":
              content : |
                [general]
                state_file = /var/awslogs/state/agent-state
              mode  : "000400"
              owner : root
              group : root
          commands:
            01-setup-agent-config:
              ## Every .conf file in /tmp/cwlogs/conf.d/ will be concatenated together to form the cwl agent config file
              command: |
                cat /tmp/cwlogs/conf.d/*.conf > /tmp/cwlogs/cwlogs-config.conf
        CWLogsAgentInstallation:
          files:
            "/tmp/cwlogs/awslogs-agent-setup.py" :
              source : "https://s3.amazonaws.com/aws-cloudwatch/downloads/latest/awslogs-agent-setup.py"
              mode  : "000700"
              owner : root
              group : root
          commands:
            01-setup-cwlogs-agent:
              ## Install the cwlogs agent - this will also install the cli it needs (in a virtualenv)
              command: |
                setsid /tmp/cwlogs/awslogs-agent-setup.py -n -r `{"Ref" : "AWS::Region" }` -c /tmp/cwlogs/cwlogs-config.conf && exit 0;

        ## Configuration deployment command - update agent if needed
        CWLogsAgentUpdaterInstallation:
          files:
            "/opt/elasticbeanstalk/hooks/configdeploy/enact/00_update_cwlogs_agent.sh":
              content: |
                #!/bin/bash
                . /etc/elasticbeanstalk/.aws-eb-stack.properties
                /opt/aws/bin/cfn-init -v -s "$stack_name" -r "$resource" --region "$region" --configsets CWLogsAgentConfigUpdate > /var/log/eb-cfn-init-call.log 2>&1
              mode  : "000700"
              owner : root
              group : root
        CWLogsAgentUpdateConfig:
          files:
            "/tmp/cwlogs/bin/cwlogs-update.sh":
              content: |
                #!/bin/bash
                log() {
                   echo [$(date -u +"%Y-%m-%d %TZ")] $1
                }
                log "Executing cwlogs agent configuration update"

                if cmp /var/awslogs/etc/awslogs.conf /tmp/cwlogs/cwlogs-config.conf > /dev/null 2>&1; then
                   log "Configuration has not changed - not updating"
                   exit 0;
                fi
                
                log "Copying new cwlogs config to /var/awslogs/etc/awslogs.conf"
                cp /tmp/cwlogs/cwlogs-config.conf /var/awslogs/etc/awslogs.conf;
                log "Restarting awslogs service"
                service awslogs restart;
                log "awslogs service restart complete"
                log "Completed cwlogs agent configuration update"
                exit 0;
              mode  : "000700"
              owner : root
              group : root
          commands:
            01-update-agent-config:
              command: "/tmp/cwlogs/bin/cwlogs-update.sh >> /var/log/eb-cwlogs.log 2>&1"


## Version deployment command - ensure agent is installed
files:
  "/tmp/cwlogs/bin/cwlogs-install.sh":
    content: |
      #!/bin/bash
      log() {
         echo [$(date -u +"%Y-%m-%d %TZ")] $1
      }

      log "Initiating cwlogs agent installation/update"
      . /etc/elasticbeanstalk/.aws-eb-stack.properties
      CONFIG_SET=CWLogsAgentInstallation

      ## just do an update if we think it's already installed
      if [ -f "/var/awslogs/etc/awslogs.conf" ]; then   
        CONFIG_SET=CWLogsAgentConfigUpdate
        log "Running agent configuration update"
      else
        log "Running agent installation"
      fi

      /opt/aws/bin/cfn-init -v -s "$stack_name" -r "$resource" --region "$region" --configsets $CONFIG_SET;
      log "Completed cwlogs agent installation/update"
      exit 0;
    mode  : "000700"
    owner : root
    group : root
    
commands:
  01-ensure-agent-installation:
    command:  "/tmp/cwlogs/bin/cwlogs-install.sh >> /var/log/eb-cwlogs.log 2>&1"