-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudwatch.tf
More file actions
110 lines (88 loc) · 2.25 KB
/
cloudwatch.tf
File metadata and controls
110 lines (88 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
provider "aws" {
region = "us-east-1"
}
resource "aws_vpc" "my_vpc" {
cidr_block = "10.0.0.0/18"
enable_dns_hostnames="true"
}
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.my_vpc.id
}
resource "aws_subnet" "pub_sub"{
vpc_id = aws_vpc.my_vpc.id
map_public_ip_on_launch = true
availability_zone = "us-east-1a"
cidr_block = "10.0.1.0/24"
}
resource "aws_route_table" "rtb1" {
vpc_id = aws_vpc.my_vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
}
resource "aws_route_table_association" "rtba" {
subnet_id = aws_subnet.pub_sub.id
route_table_id = aws_route_table.rtb1.id
}
resource "aws_security_group" "pub_nsg" {
vpc_id = aws_vpc.my_vpc.id
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
ingress{
from_port =22
to_port =22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_instance" "pub_ec2" {
instance_type = "t2.micro"
ami = "ami-0182f373e66f89c85"
security_groups = [aws_security_group.pub_nsg.id]
subnet_id = aws_subnet.pub_sub.id
key_name = "devops"
tags = {
"env"= "cloudwatch"
}
user_data = <<EOF
#!/bin/bash
# Update the package lists
yum update -y
# Install Python 3
yum install python3 -y
# Install Git
yum install git -y
# Clone the repository
git clone https://github.com/seena127/Terraform_script_files.git
# Navigate to the cloned directory
cd Terraform_script_files
# Run your script (ensure your script is correctly named and exists)
python3 cpu_spike.py
EOF
}
resource "aws_sns_topic" "email" {
name = "cloudwatch_to_email_notification"
}
resource "aws_sns_topic_subscription" "email"{
topic_arn = aws_sns_topic.email.arn
protocol = "email"
endpoint = "bsreenivasasarma1999@gmail.com"
}
resource "aws_cloudwatch_metric_alarm" "demo-py1" {
alarm_name = "demo-py1-app"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods=1
metric_name="CPUUtilization"
period=30
statistic="Maximum"
threshold=30
dimensions={
InstanceId= aws_instance.pub_ec2.id
}
namespace = "AWS/EC2"
}