-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterraform.tf
111 lines (98 loc) · 2.68 KB
/
terraform.tf
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
111
provider "aws" {
region = "eu-west-1"
}
variable "telegram_bot_key" {
type = "string"
description = "The telegram bot key like: botddddd:aaaaaa"
}
variable "telegram_channel_id" {
type = "string"
description = "Telegram channel id like: @channel_asd_adsa"
}
resource "aws_lambda_function" "open_lambda" {
function_name = "telegram-open"
runtime = "python3.8"
role = "${aws_iam_role.telegram_open_role.arn}"
handler = "scraper.handle"
filename = "function.zip"
source_code_hash = "${base64sha256("scraper.py")}"
timeout = 30
environment {
variables = {
TGCHANNELID = "${var.telegram_channel_id}"
TGBOTKEY = "${var.telegram_bot_key}"
}
}
}
resource "aws_lambda_permission" "scrape-open-permission" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.open_lambda.function_name}"
principal = "events.amazonaws.com"
source_arn = "${aws_cloudwatch_event_rule.scrap-scheduled-event-rule.arn}"
}
resource "aws_iam_role_policy_attachment" "lambda_access_cloudwatch_policy" {
role = "${aws_iam_role.telegram_open_role.id}"
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
resource "aws_iam_role" "telegram_open_role" {
name = "telegram-open-role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow"
}
]
}
EOF
}
resource "aws_iam_role_policy" "access_dynamodb" {
role = "${aws_iam_role.telegram_open_role.id}"
name = "dynamodb-access-policy"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"dynamodb:GetItem",
"dynamodb:PutItem"
],
"Resource": "${aws_dynamodb_table.open_daily_history.arn}"
}
]
}
EOF
}
# dynamo db
resource "aws_dynamodb_table" "open_daily_history" {
name = "open_daily_history"
hash_key = "url"
billing_mode = "PROVISIONED"
write_capacity = "1"
read_capacity = "1"
ttl {
attribute_name = "ttl"
enabled = true
}
attribute {
name = "url"
type = "S"
}
}
resource "aws_cloudwatch_event_rule" "scrap-scheduled-event-rule" {
name = "scheduled-scrap"
description = "Scrap open news"
schedule_expression = "rate(15 minutes)"
}
resource "aws_cloudwatch_event_target" "scarap-target" {
rule = "${aws_cloudwatch_event_rule.scrap-scheduled-event-rule.name}"
arn = "${aws_lambda_function.open_lambda.arn}"
}