Best Practice to Monitor Beanstalk Instances Dynamically

0

Hi, We are looking into ways to best monitor our Beanstalk environment, in the most dynamic way possible. Meaning we would like to be able that whenever we have a new instance to set corresponding metric alarms, we are interested in. Since our environment was built long time ago and does not have much automation around it I have decide to leverage some CDK code, and build Clouwatch code that we can run on a schedule to detect differences and add/remove monitoring to new instances accordingly. Another option was to use eventBridge and trigger lambdas and based on some SDK code get the proper monitoring in place.

The question is, based on the situation above, does eb-extensions or any other Beanstalk feature allow us to set "metric alarms" from within Beanstalk during deployment (not using the console), or other ways using other parts of the Beanstalk infrastructure to set those alarms upon deploying new app build versions?

Update: After doing some digging into eb-extensions looks like we can use custom resources, but is there a way that when setting the alarm I can use insatnce IDs for the dimensions, to set the alarm per instance in the environment ?

example:

MetricName: "RootFilesystemUtil"
Namespace: "AWS/ElasticBeanstalk"
Dimensions:
  - Name: InstanceId
    Value: <ref_all_instances_in_the_env>
1 Answer
0

Yes, you can use the Elastic Beanstalk (EB) Extensions feature to set "metric alarms" from within Beanstalk during deployment. EB Extensions allow you to run custom scripts on instances in your Elastic Beanstalk environment. You can use these scripts to set up monitoring and alarms for your instances.

To set up alarms that use the instance ID as a dimension, you can use the AWS SDK for your preferred programming language (Java, Python, etc.) in your EB Extension script to retrieve the instance ID of the instances in your environment, and then use that information to set up the alarms.

You can use the CloudWatch.Client.put_metric_alarm() method to create an alarm and set dimensions by passing them as a dictionary. example :

dimensions = [{'Name': 'InstanceId', 'Value': instance_id}]
cloudwatch.put_metric_alarm(
    AlarmName='RootFilesystemUtil',
    MetricName='RootFilesystemUtil',
    Namespace='AWS/ElasticBeanstalk',
    ComparisonOperator='GreaterThanOrEqualToThreshold',
    EvaluationPeriods=1,
    Period=60,
    Threshold=90.0,
    AlarmActions=[sns_arn],
    Dimensions=dimensions
)

Alternatively, you can also use CloudFormation Custom Resources to create Alarms and pass the instance ids as parameter.

In any case, you would need to manage the lifecycle of the alarms. If an instance is terminated or replaced, you would need to delete the corresponding alarm.

profile picture
answered a year ago
  • Thanks for the Answer, its helpful and re-enforces some of the limitations and challenges that I have already identified. We currently have an approach using cdk and its fully automated and leverages Cloudwatch cdk directly. We plan to run overnight ci/cd scheduled task to check the diff and keep the monitoring per env or instance up to date. Would you think this approach be more robust or eb-extension scripts ? Also, just to confirm, using the approach I mentioned above in my original question, I can't directly somehow reference the instance ID, for the dimension right ?

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions