-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
196 lines (174 loc) · 4.93 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
terraform {
required_providers {
mongodbatlas = {
source = "mongodb/mongodbatlas"
version = "1.4.2"
}
aws = {
source = "hashicorp/aws"
version = "4.22.0"
}
}
cloud {
organization = "cohky"
workspaces {
name = "jrb"
}
}
}
provider "mongodbatlas" {}
variable "MONGODB_ORG_ID" {}
variable "MONGODB_API_KEY" {}
variable "MONGODB_PASS" {}
resource "mongodbatlas_project" "jrb" {
name = "jrb"
org_id = var.MONGODB_ORG_ID
api_keys {
api_key_id = var.MONGODB_API_KEY
role_names = [
"GROUP_OWNER",
]
}
is_collect_database_specifics_statistics_enabled = true
is_data_explorer_enabled = true
is_performance_advisor_enabled = true
is_realtime_performance_panel_enabled = true
is_schema_advisor_enabled = true
}
resource "mongodbatlas_cluster" "jrb" {
name = "jrb"
project_id = mongodbatlas_project.jrb.id
provider_instance_size_name = "M0"
provider_name = "TENANT"
auto_scaling_compute_enabled = false
auto_scaling_compute_scale_down_enabled = false
auto_scaling_disk_gb_enabled = null
backing_provider_name = "AWS"
cloud_backup = false
cluster_type = "REPLICASET"
disk_size_gb = 0.5
encryption_at_rest_provider = "NONE"
mongo_db_major_version = "5.0"
pit_enabled = false
provider_region_name = "AP_NORTHEAST_1"
replication_factor = 3
replication_specs {
regions_config {
analytics_nodes = 0
electable_nodes = 3
priority = 7
read_only_nodes = 0
region_name = "AP_NORTHEAST_1"
}
num_shards = 1
zone_name = "Zone 1"
}
}
resource "mongodbatlas_project_ip_access_list" "ip_access_list" {
project_id = mongodbatlas_project.jrb.id
cidr_block = "0.0.0.0/0"
comment = "all"
}
resource "mongodbatlas_database_user" "db_user" {
username = "cohky"
password = var.MONGODB_PASS
project_id = mongodbatlas_project.jrb.id
auth_database_name = "admin"
roles {
role_name = "atlasAdmin"
database_name = "admin"
}
}
variable "AWS_ACCESS_KEY_ID" {}
variable "AWS_SECRET_ACCESS_KEY" {}
variable "AWS_REGION" {}
provider "aws" {
access_key = var.AWS_ACCESS_KEY_ID
secret_key = var.AWS_SECRET_ACCESS_KEY
region = var.AWS_REGION
}
resource "aws_iam_role" "sts_for_iam" {
name = "sts_for_iam"
assume_role_policy = <<-EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
data "aws_caller_identity" "current" {}
resource "aws_iam_role_policy" "iam_for_lambda" {
name = "iam_for_lambda"
role = aws_iam_role.sts_for_iam.id
policy = <<-EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": [
"arn:aws:logs:ap-northeast-1:${data.aws_caller_identity.current.account_id}:log-group:/aws/lambda/batch_jun_remind:*"
]
}
]
}
EOF
}
variable "APP_ENV" {}
variable "CLIENT_ID" {}
variable "CLIENT_SECRET" {}
variable "LINE_CHANNEL_ACCESS_TOKEN" {}
variable "LINE_CHANNEL_SECRET" {}
resource "aws_lambda_function" "batch_jun_remind" {
filename = "upload.zip"
function_name = "batch_jun_remind"
role = aws_iam_role.sts_for_iam.arn
handler = "jrb"
architectures = ["x86_64"]
runtime = "go1.x"
timeout = 180
environment {
variables = {
APP_ENV = var.APP_ENV
CLIENT_ID = var.CLIENT_ID
CLIENT_SECRET = var.CLIENT_SECRET
LINE_CHANNEL_ACCESS_TOKEN = var.LINE_CHANNEL_ACCESS_TOKEN
LINE_CHANNEL_SECRET = var.LINE_CHANNEL_SECRET
MONGODB_URI = "mongodb+srv://cohky:${var.MONGODB_PASS}@${substr(mongodbatlas_cluster.jrb.connection_strings.0.standard_srv, 14, 100)}/?retryWrites=true&w=majority"
}
}
}
resource "aws_cloudwatch_log_group" "log" {
name = "/aws/lambda/${aws_lambda_function.batch_jun_remind.function_name}"
retention_in_days = 14
}
resource "aws_cloudwatch_event_rule" "every_minutes" {
name = "every_minutes"
description = "every_minutes"
schedule_expression = "rate(1 minute)"
}
resource "aws_cloudwatch_event_target" "target" {
target_id = "batch_jun_remind"
rule = aws_cloudwatch_event_rule.every_minutes.name
arn = aws_lambda_function.batch_jun_remind.arn
}
resource "aws_lambda_permission" "permission" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.batch_jun_remind.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.every_minutes.arn
}