-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
executable file
·86 lines (72 loc) · 2.13 KB
/
main.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
provider "aws" {
region = "ap-southeast-2"
}
# IAM
resource "aws_iam_role" "instacron_exec_role" {
name = "instacron_exec"
path = "/"
description = "Allows Lambda Function to call AWS services on your behalf."
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "iam_role_policy_attachment_lambda_basic_execution" {
role = aws_iam_role.instacron_exec_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
# Lambda
resource "aws_lambda_function" "instacron" {
filename = "instacronjs.zip"
function_name = "instaCron"
role = aws_iam_role.instacron_exec_role.arn
handler = "lambda.handler"
runtime = "nodejs14.x"
timeout = 90
source_code_hash = filebase64sha256("instacronjs.zip")
memory_size = 512
environment {
variables = {
DROPBOX_TOKEN = var.dropbox_token
IG_USERNAME = var.ig_username
IG_PASSWORD = var.ig_password
}
}
depends_on = [
# aws_iam_role_policy_attachment.lambda_logs,
aws_cloudwatch_log_group.instacron,
]
}
# Cloudwatch
resource "aws_cloudwatch_event_rule" "every_day" {
name = "every-day"
description = "Fires every day at 23.15 UTC"
# https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html
schedule_expression = "cron(15 23 * * ? *)"
}
resource "aws_cloudwatch_event_target" "instacron_every_day" {
rule = "${aws_cloudwatch_event_rule.every_day.name}"
target_id = "instacron"
arn = "${aws_lambda_function.instacron.arn}"
}
resource "aws_lambda_permission" "allow_cloudwatch_to_call_instacron" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.instacron.function_name}"
principal = "events.amazonaws.com"
source_arn = "${aws_cloudwatch_event_rule.every_day.arn}"
}
resource "aws_cloudwatch_log_group" "instacron" {
name = "/aws/lambda/instaCron"
retention_in_days = 14
}