Setting up Email Notification for EC2 instance changes using Amazon SNS and Eventbridge
Published: 2022-03-17 10:57:40

**Output:** To receive email notif everytime your EC2 instances changes its status. (Terminated, Stopped, etc)

**What is Amazon SNS?**
Amazon SNS enables you to send notifications directly to your customers.

**What is Amazon EventBridge?**
Amazon EventBridge is a serverless event bus that makes it easier to build event-driven applications at scale using events generated from your applications, integrated Software-as-a-Service (SaaS) applications, and AWS services.

**Overview**
1. Setup SNS topic
2. Create email subscription on topic
3. Setup EC2 Instance State-change Notification eventbridge rule

## Setting up SNS topic and email subscription

### 1. Go to AWS SNS console, and then choose Topics from the navigation pane.

### 2. Select Create topic.
![Create topic](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5v89zh5nvigzusbo2x6p.png)
For the type, I would be selecting Standard. Then just enter name and display name for your topic.

### 3. Click Create topic.
![Topic created](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k6toqkw0pgwdeew2voyf.png)

### 4. On the Subscriptions tab, click Create subscription.
![Create Subscription](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gv60xjfig6j9qj3wao3v.png)

For Protocol, select Email.
For Endpoint, enter the email address where you want to receive the notifications.

If you want to change this to a SMS notif, you can simple select SMS for protocal then enter mobile number on endpoint where you want to receive notifications.

### 5. Click Create subscription.

![subscription created](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zuqlccq5xkjjhyxjf70v.png)

### 6. Confirm email subscription sent to the email address you entered.

![email confirmation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ndzptdvsfbnbij84gl0t.png)

![subscription confirmed](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v9m6ktrq9blq01afh7tg.png)

### 7. Go to your topic then check email status.

![status confirmed](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d8t7mbk2pb7nr1v0xvcp.png)

It should show that your email is now confirmed.

## Setting up EC2 TerminateInstances eventbridge rule

### 1. Open the EventBridge console, and then click Create Rule

![Eventbridge](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qf44kbja2m2ahst30r4z.png)

### 2. Fill up details on define rule detail then click next
![Create rule](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g6ah6expqrdpz1ayrd20.png)

Enter a Name and description for your rule. 
On rule type, select rule with an event pattern

### 3. Fill up details on Build event pattern then click next

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/brd7ob0hvghiyo7v1f6k.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/90r0twimygwb6n6zdwyj.png)
For Service provider, choose AWS.
For Service name, choose EC2.
For Event type, choose EC2 Instance State-change Notification.


### 4. Fill up select targets then click next


![target](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8xyt3oe311h9qki9bgu9.png)
In Select targets, choose SNS topic from the Target dropdown list.
For Topic, choose the topic name that you created earlier.

### 5. Click Create rule

You can test this rule by terminating your instance. This rule generates an email notification every time an instance is terminated. You can select other states as well.


![Iterminatedinstance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j5pc79ozrwqx7i9om940.png)


![email notif terminated](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g1yagrf830h55acnel3u.png)