-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.tf
81 lines (67 loc) · 2.81 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
locals {
output_file = data.null_data_source.lambda_file.outputs["filename"]
service_identifier = var.service_identifier
logdna_tags = join(",", concat([data.aws_region.current.name], var.logdna_tags))
environment = {
"LOGDNA_KEY" = var.logdna_key
"LOGDNA_TAGS" = local.logdna_tags
}
}
data "null_data_source" "lambda_file" {
inputs = {
filename = "${path.module}/files/lambda/package.zip"
}
}
data "http" "logdna_cloudwatch" {
url = var.url
}
data "archive_file" "logdna_cloudwatch" {
type = "zip"
output_path = local.output_file
source {
filename = "logdna_cloudwatch.py"
content = data.http.logdna_cloudwatch.body
}
}
data "aws_iam_policy_document" "lambda_execution_role" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com", "edgelambda.amazonaws.com", "events.amazonaws.com"]
}
}
}
data "aws_region" "current" {
}
resource "aws_iam_role" "lambda_execution_role" {
name_prefix = "lambda.${local.service_identifier}."
assume_role_policy = data.aws_iam_policy_document.lambda_execution_role.json
}
resource "aws_iam_role_policy_attachment" "lambda_execution_basic" {
role = aws_iam_role.lambda_execution_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
resource "aws_iam_role_policy_attachment" "xray_wo" {
role = aws_iam_role.lambda_execution_role.name
policy_arn = "arn:aws:iam::aws:policy/AWSXrayWriteOnlyAccess"
}
resource "aws_lambda_function" "logdna_cloudwatch" {
description = "AWS Lambda for logging into LogDNA"
filename = local.output_file
source_code_hash = data.archive_file.logdna_cloudwatch.output_base64sha256
function_name = local.service_identifier
role = aws_iam_role.lambda_execution_role.arn
handler = "logdna_cloudwatch.lambda_handler"
runtime = "python2.7"
publish = true
reserved_concurrent_executions = var.reserved_concurrent_executions
timeout = var.lambda_timeout
layers = data.aws_region.current.name == "us-east-1" ? ["arn:aws:lambda:us-east-1:668099181075:layer:AWSLambda-Python-AWS-SDK:4"] : data.aws_region.current.name == "us-west-1" ? ["arn:aws:lambda:us-west-1:325793726646:layer:AWSLambda-Python-AWS-SDK:4"] : data.aws_region.current.name == "us-east-2" ? ["arn:aws:lambda:us-east-2:259788987135:layer:AWSLambda-Python-AWS-SDK:4"] : data.aws_region.current.name == "us-west-2" ? ["arn:aws:lambda:us-west-2:420165488524:layer:AWSLambda-Python-AWS-SDK:5"] : []
tags = {
Application = local.service_identifier
}
environment {
variables = local.environment
}
}