forked from tucnak/telebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
114 lines (105 loc) · 2.87 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
variable "name" {
type = string
default = "awslambdaechobot" # basename(path.root)
description = "Name of the binary (basename of this directory)"
}
variable "token" {
type = string
description = "Telegram bot token"
}
variable "region" {
type = string
description = "AWS deployment region"
}
locals {
exe = var.name
zip = "${local.exe}.zip"
func = "${var.name}Function"
api = "${var.name}API"
role = "${var.name}Role"
policy = "${var.name}Policy"
perm = "${var.name}Permission"
log = "/aws/lambda/${local.func}"
envToken = "TELEBOT_SECRET"
}
# Run ./init.sh to install github.com/yi-jiayu/terraform-provider-telegram
provider "telegram" {
bot_token = var.token
}
resource "telegram_bot_webhook" "a" {
url = aws_apigatewayv2_api.a.api_endpoint
max_connections = 100
}
provider "archive" {
version = "~> 1.3"
}
data "archive_file" "a" {
type = "zip"
source_file = local.exe
output_path = local.zip
}
provider "aws" {
version = "~> 2.59"
region = var.region
}
resource "aws_apigatewayv2_api" "a" {
name = local.api
protocol_type = "HTTP"
target = aws_lambda_function.a.arn
route_key = "POST /"
}
resource "aws_lambda_permission" "a" {
statement_id = local.perm
function_name = aws_lambda_function.a.function_name
action = "lambda:InvokeFunction"
principal = "apigateway.amazonaws.com"
source_arn = "${aws_apigatewayv2_api.a.execution_arn}/*/*/*" # Any stage, method, resource.
}
resource "aws_lambda_function" "a" {
function_name = local.func
runtime = "go1.x"
handler = local.exe
memory_size = 128 # MB, 128 + 64*x
timeout = 60 # seconds
role = aws_iam_role.a.arn
filename = data.archive_file.a.output_path
source_code_hash = data.archive_file.a.output_base64sha256
environment {
variables = {
(local.envToken) = var.token
}
}
}
resource "aws_cloudwatch_log_group" "a" {
# AWS Lambda automatically logs to the group with this name.
name = local.log
retention_in_days = 1
}
resource "aws_iam_role_policy_attachment" "a" {
role = aws_iam_role.a.name
policy_arn = aws_iam_policy.a.arn
}
resource "aws_iam_role" "a" {
name = local.role
assume_role_policy = data.aws_iam_policy_document.assume_role_policy.json
}
data "aws_iam_policy_document" "assume_role_policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
resource "aws_iam_policy" "a" {
name = local.policy
policy = data.aws_iam_policy_document.a.json
}
data "aws_iam_policy_document" "a" {
# Based on AWSLambdaBasicExecutionRole.
statement {
actions = ["logs:CreateLogStream", "logs:PutLogEvents"]
resources = [aws_cloudwatch_log_group.a.arn]
}
}