DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Operator Overloading in Java
  • The SPACE Framework for Developer Productivity
  • Mastering Go-Templates in Ansible With Jinja2
  • Event-Driven Architecture Using Serverless Technologies

Trending

  • Operator Overloading in Java
  • The SPACE Framework for Developer Productivity
  • Mastering Go-Templates in Ansible With Jinja2
  • Event-Driven Architecture Using Serverless Technologies
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. CloudWatch Disk Space Monitoring For Elastic Beanstalk Instances

CloudWatch Disk Space Monitoring For Elastic Beanstalk Instances

Take the manual work out of monitoring AWS Elastic Beanstalk using CloudWatch and setting up automated monitoring and alarms.

PremKumar Kadali user avatar by
PremKumar Kadali
·
Dec. 12, 19 · Tutorial
Like (3)
Save
Tweet
Share
8.74K Views

Join the DZone community and get the full member experience.

Join For Free

If you’re responsible for developing and deploying web applications within your AWS environment for your organization, then it’s likely you may have heard of AWS Elastic Beanstalk already. The service’s automation and simplification make it ideal for deploying, provisioning, monitoring and scaling efficient environments to run developed applications in.

By leveraging Amazon’s orchestration service AWS Elastic Beanstalk, we can quickly deploy and manage all the resources for applications that we wish to upload to the AWS cloud. Elastic Beanstalk is a great developer tool that’s easy to get started with and comes packed with several other benefits including:

  • Autoscaling options

  • Improved developer productivity

  • Customization options

  • Cost-effective capabilities

You may also enjoy: Deployment Orchestration With AWS Elastic Beanstalk

With AWS Elastic Beanstalk, we can spin up our entire application environment without having to configure any of the resources manually. Rather, AWS Elastic Beanstalk takes responsibility for deploying the correct infrastructure to run the uploaded code. Plus, there are no separate costs for running Elastic Beanstalk in AWS; you only pay for the resources you use to run your application, for example, the storage you use in Amazon S3. Also, the cost is not fixed; it can vary according to the number of EC2 instances, the size of your S3 bucket, and how you’ve configured your database instances.

CloudWatch Alarms For Elastic Beanstalk Instances

By default, AWS Elastic Beanstalk only shares the  EnvironmentHealth metrics with CloudWatch. To enable any other metrics that are essential for monitoring Beanstalk, you will have to configure enhanced monitoring. AWS provides detailed technical documentation on how to enable enhanced monitoring using the Beanstalk console, CLI, or configuration files. You just have to select which metrics you want to be collected by instance or environment.

Once you’ve set up enhanced monitoring, Elastic Beanstalk will publish a host of metrics to CloudWatch. Setting up alarms on these to look for any abnormalities and limits is a great way to spot issues in your AWS environments. In this post, we will cover how to monitor the disk space of Elastic Beanstalk instances with the help of Cloudwatch by creating alarms for those metrics.

To monitor the "disk," "swap," and "memory" spaces for our example Linux instances, we just need to deploy certain scripts. With the help of these scripts, we can monitor whatever requirements we wish.

.Ebextensions:

If you haven’t used .ebextensions before; it’s important to know that the .ebextensions folder will run within your Elastic Beanstalk environment creation. Here we create a script that monitors the disk space metric which will be kept inside the .ebextension folder. For example, cloudwatch.config. See the full script below for more details:

Packages:

Java




xxxxxxxxxx
1


 
1
  yum:
2
    perl-DateTime: []
3
    perl-Sys-Syslog: []
4
    perl-LWP-Protocol-https: []
5
    perl-Switch: []
6
    perl-URI: []
7
    perl-Bundle-LWP: []


Source Material:

https://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.1.zip

Container_commands:

Java




x


 
1
 01-setupcron:
2
    command: |
3
      echo '*/5 * * * * root perl /opt/cloudwatch/aws-scripts-mon/mon-put-instance-data.pl `{"Fn::GetOptionSetting" : { "OptionName" : "CloudWatchMetrics", "DefaultValue" : "--mem-util --disk-space-util --disk-path=/" }}` >> /var/log/cwpump.log 2>&1' > /etc/cron.d/cwpump
4
  02-changeperm:
5
    command: chmod 644 /etc/cron.d/cwpump
6
  03-changeperm:
7
    command: chmod u+x /opt/cloudwatch/aws-scripts-mon/mon-put-instance-data.pl


Option_settings:

Java




x


 
1
  "aws:autoscaling:launchconfiguration" :
2
    IamInstanceProfile : "aws-elasticbeanstalk-ec2-role"
3
  "aws:elasticbeanstalk:customoption" :
4
    CloudWatchMetrics : "--disk-space-util --disk-space-used --disk-space-avail --disk-path=/"


Creating Alarms For Disk Space Monitoring

To create Cloudwatch Alarms for disk space monitoring, put this below script in a .ebextension folder and deploy it on the app. Save the script below as alarms.config.

Packages:

Java




xxxxxxxxxx
1


 
1
yum:
2
    jq: []






x


 
1
  yum:
2
    perl-DateTime: []
3
    perl-Sys-Syslog: []
4
    perl-LWP-Protocol-https: []
5
    perl-Switch: []
6
    perl-URI: []
7
    perl-Bundle-LWP: []
8
 
        



Files:


Java




xxxxxxxxxx
1


 
1
 "/tmp/alarams.sh" :
2
      mode: "000777"
3
      content : |
4
        #!/bin/bash
5
        RESULT="`wget -qO- http://169.254.169.254/latest/meta-data/instance-id`"
6
  Region="`curl --silent http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r .region`"
7
 EB_ENV=$(/opt/elasticbeanstalk/bin/get-config environment | jq -r 'Test')
8
 aws cloudwatch put-metric-alarm --region=$Region --alarm-name DiskSpaceUtilization6-$RESULT-$EB_ENV --alarm-description "Alarm when DiskSpaceUtilization exceeds 50 percent" --metric-name DiskSpaceUtilization --namespace System/Linux --statistic Average --period 300 --threshold 50 --comparison-operator GreaterThanThreshold  --dimensions Name=Filesystem,Value=/dev/xvda1 Name=MountPath,Value=/ Name=InstanceId,Value=$RESULT --evaluation-periods 2 --alarm-actions arn:aws:sns:us-east-1:XXXXXXXXXXX:example.name --unit Percent


Container_commands:

Java




x


 
1
  01_alarams: 
2
    command: "/tmp/alarams.sh"


Publishing CloudWatch Metrics

In the above script, we set the threshold as 50%. However, you can modify that setting as per your individual requirements. You can also set up to receive alarms by email. You just need to create an SNS topic and subscribe that to your email ID and input your SNS ARN into the script.

In order to publish custom Amazon CloudWatch metrics, the instances in your environment need permission to use CloudWatch. You can grant permissions to your environment’s instances by adding them to the environment’s instance profile. You can add permissions to the instance profile before or after deploying your application.

Also, the IAM role which you’re using for your Elastic Beanstalk application has to have full access to Cloudwatch Alarms. Only then can it create disk space monitoring metrics and alarms for the instances.

Now to monitor disk space and create an alarm for when it hits our 50% threshold, we create a folder called .ebextension in the code environment and place the above two scripts in that folder, then zip them and upload them to Beanstalk.

If you run your app successfully, you’ll see the following output:

Metrics output
Metrics output

Beanstalk CloudWatch metrics can be viewed normally through the Metrics section of CloudWatch, but it is also possible to use the Monitoring tab in the Beanstalk console. For alarms, go to the Alarms tab under CloudWatch and you will find your alarm created there, as per the image below:

Created alarm
Created alarm

Use the method described to take the manual work out of monitoring Beanstalk and run this automatic setup to effortlessly watch for anomalies with your disk space within AWS Elastic Beanstalk without any overly complex configuration.

Further Reading

Slack Notification with CloudWatch Alarms & Lambda

AWS Elastic Load Balancing (ELB) and Monitoring

Space (architecture) AWS Elastic Beanstalk AWS Metric (unit) application

Published at DZone with permission of PremKumar Kadali. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Operator Overloading in Java
  • The SPACE Framework for Developer Productivity
  • Mastering Go-Templates in Ansible With Jinja2
  • Event-Driven Architecture Using Serverless Technologies

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: