Amr
Amr

Reputation: 2460

How can I automatically stop an EC2 Instance once network bandwidth exceeds a predefined limit?

I am currently experimenting with AWS Free Tier. I understand that, for AWS Data Transfer, the following limit applies:

$0 for 100GB of data transfer out to the internet, aggregated globally, each month.

I am using a single EC2 instance in one AWS Region. I wonder if I can configure the instance to automatically stop, once the data transfer out to the internet hits the 100GB limit?

Upvotes: 1

Views: 884

Answers (2)

Amr
Amr

Reputation: 2460

Oookay.. So I figured out how to achieve this. The solution below takes into account the accumulative network bandwidth (Internet OUT) over a month period, as opposed to the moving average retrieved in the CloudWatch.

  1. Create a new tag in your EC2 instance. In my case, I created a new tag name and assigned my custom value, to uniquely identify my instance (example: tag key "email-server", tag value "us-server"
  2. Wait up to 24 hours (essential)
  3. Activating User-Defined Cost Allocation Tags (https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/activating-tags.html)
  4. In the above step, you should find your new tag key. You need to activate it (Note: in case you create a new tag key, you really need to wait for up to 24 hours for the user-defined cost allocation tags list to update)
  5. Once activated, you need to wait again for up to 24 hours
  6. Go to AWS Budgets
  7. Create a new Budget. In my case, I chose "Usage Budget"
  8. In the "Usage Type Groups", select "EC2: Data Transfer - Internet (Out)"
  9. Under "Budget Scope", select "Filter specific AWS cost dimensions"
  10. Click "Add Filter", Dimension --> "Tag", Tag --> Choose your newly created tag, in my case "email-server". Finally in the Values filed, select the tag values you want to include in your budget, in my case "us-server".
  11. Afterwards, you will optionally be able to add custom alerting (example: if 80% of the budget is consumed), and you can also define action items (example: shut down instance).

Upvotes: 2

Marcin
Marcin

Reputation: 238101

You could setup AWS CloudWatch Alarm based on NetworkOut metric for your instance. When triggered, the alarm would stop the instance.

Upvotes: 1

Related Questions