Create CloudWatch Alarms with Python AWS CDK

Create the alarm

Put this into your Python AWS CDK stack to create an alarm on a bucket asigned to a varibale named bucket

bucket = s3.Bucket…

s3_size_alarm = cloudwatch.Alarm(self, ‘bucket_overloaded_alarm’,
metric= cloudwatch.Met…


This content originally appeared on DEV Community and was authored by chrishart0

Create the alarm

Put this into your Python AWS CDK stack to create an alarm on a bucket asigned to a varibale named bucket

bucket = s3.Bucket...

s3_size_alarm = cloudwatch.Alarm(self, 'bucket_overloaded_alarm',
    metric= cloudwatch.Metric(
        namespace = "AWS/S3", 
        metric_name = "BucketSizeBytes",
        dimensions={
            "BucketName": bucket.bucket_name,
            "StorageType": "StandardStorage",
        },
        period = core.Duration.days(1),
        statistic="Maximum",
    ), 
    evaluation_periods=1, 
    threshold=1000000000 #1 GB
)

This will create a CloudWatch alarm which:

  • Alarm if the size of the bucket goes over 1GB
  • Only monitors objects with the StandardStorage type

Relevant CDK docs:

Warning: Don't rely on an alarm watching the BucketSizeBytes metric to make sure your bucket doesn't grow too large. This metric is only collected once a day. Consider opting in to S3 request metrics for more in-depth S3 monitoring

Create alarm action

Alarms aren't very useful if they don't do anything. Setup some actions:

topic = sns.Topic.from_topic_arn(self,'snstopic',"arn:aws:sns:us-east-1:123456789012:alarm-go-ahhhhhhhhhhhh")

s3_size_alarm.add_alarm_action(
    cloudwatch_actions.SnsAction(
        topic = topic
    )
)

Relevant CDK docs:

More CDK or AWS Serverless Questions?

Feel free to leave a comment here or hit us up on LinkedIn.

Want to learn more about how SignetSeal can help make your chats more secure? Read more about us on our website


This content originally appeared on DEV Community and was authored by chrishart0


Print Share Comment Cite Upload Translate Updates
APA

chrishart0 | Sciencx (2021-10-21T23:24:14+00:00) Create CloudWatch Alarms with Python AWS CDK. Retrieved from https://www.scien.cx/2021/10/21/create-cloudwatch-alarms-with-python-aws-cdk/

MLA
" » Create CloudWatch Alarms with Python AWS CDK." chrishart0 | Sciencx - Thursday October 21, 2021, https://www.scien.cx/2021/10/21/create-cloudwatch-alarms-with-python-aws-cdk/
HARVARD
chrishart0 | Sciencx Thursday October 21, 2021 » Create CloudWatch Alarms with Python AWS CDK., viewed ,<https://www.scien.cx/2021/10/21/create-cloudwatch-alarms-with-python-aws-cdk/>
VANCOUVER
chrishart0 | Sciencx - » Create CloudWatch Alarms with Python AWS CDK. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/21/create-cloudwatch-alarms-with-python-aws-cdk/
CHICAGO
" » Create CloudWatch Alarms with Python AWS CDK." chrishart0 | Sciencx - Accessed . https://www.scien.cx/2021/10/21/create-cloudwatch-alarms-with-python-aws-cdk/
IEEE
" » Create CloudWatch Alarms with Python AWS CDK." chrishart0 | Sciencx [Online]. Available: https://www.scien.cx/2021/10/21/create-cloudwatch-alarms-with-python-aws-cdk/. [Accessed: ]
rf:citation
» Create CloudWatch Alarms with Python AWS CDK | chrishart0 | Sciencx | https://www.scien.cx/2021/10/21/create-cloudwatch-alarms-with-python-aws-cdk/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.