-
Notifications
You must be signed in to change notification settings - Fork 0
/
logs-logzio.tf
90 lines (80 loc) · 2.3 KB
/
logs-logzio.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
# The auth token to use for sending logs to Logz.io
variable "logz_token" {}
# The endpoint to use for sending logs to Logz.io
variable "logz_url" {
default = "https://listener.logz.io:8071"
}
resource "aws_iam_role" "iam_for_lambda_logz" {
name = "${var.app}-${var.environment}-logz-role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_role_policy" "lambda_policy_logs_logz" {
name = "${var.app}-${var.environment}-logz-role"
role = "${aws_iam_role.iam_for_lambda_logz.id}"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": [
"*"
]
}
]
}
EOF
}
#function code from logzio: https://github.com/logzio/cloudwatch-logs-shipper-lambda
resource "aws_lambda_function" "lambda_logz" {
function_name = "${var.app}-${var.environment}-logz"
description = "Sends Cloudwatch logs to logz."
runtime = "python2.7"
timeout = 60
memory_size = 512
role = "${aws_iam_role.iam_for_lambda_logz.arn}"
handler = "lambda_function.lambda_handler"
filename = "logs-logzio.zip"
source_code_hash = "${base64sha256(file("logs-logzio.zip"))}"
tags = "${var.tags}"
environment {
variables = {
TOKEN = "${var.logz_token}"
URL = "${var.logz_url}"
TYPE = "elb"
}
}
}
resource "aws_lambda_permission" "allow_cloudwatch" {
statement_id = "${var.app}-${var.environment}-logz"
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.lambda_logz.arn}"
principal = "logs.${var.region}.amazonaws.com"
source_arn = "${aws_cloudwatch_log_group.logs.arn}"
}
resource "aws_cloudwatch_log_subscription_filter" "cloudwatch_lambda_subscription" {
depends_on = ["aws_lambda_permission.allow_cloudwatch"]
name = "${var.app}-${var.environment}-logz"
log_group_name = "${aws_cloudwatch_log_group.logs.name}"
filter_pattern = ""
destination_arn = "${aws_lambda_function.lambda_logz.arn}"
}