This repository was archived by the owner on Feb 6, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathunsplash_lambda.tf
95 lines (80 loc) · 2.12 KB
/
unsplash_lambda.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
variable "unsplash_name" {
type = string
default = "deckdeckgo-unsplash-lambda"
}
resource "aws_lambda_function" "unsplash" {
function_name = var.unsplash_name
filename = data.external.build-function-unsplash.result.build_function_zip_path
handler = "main.handler"
runtime = "nodejs10.x"
timeout = 10
role = aws_iam_role.iam_for_unsplash_lambda.arn
environment {
variables = {
UNSPLASH_CLIENT_ID = data.external.unsplash-client-id.result.unsplash-client-id
}
}
depends_on = [
aws_iam_role_policy_attachment.unsplash_lambda_logs,
aws_cloudwatch_log_group.unsplash_group,
]
}
resource "aws_iam_role" "iam_for_unsplash_lambda" {
name = "deckdeckgo-unsplash-lambda-iam"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
data "external" "unsplash-client-id" {
program = [
"${path.module}/script/unsplash-client-id",
]
}
data "external" "build-function-unsplash" {
program = [
"${path.module}/script/build-unsplash-proxy",
]
}
# This is to optionally manage the CloudWatch Log Group for the Lambda Function.
# If skipping this resource configuration, also add "logs:CreateLogGroup" to the IAM policy below.
resource "aws_cloudwatch_log_group" "unsplash_group" {
name = "/aws/lambda/${var.unsplash_name}"
retention_in_days = 7
}
# See also the following AWS managed policy: AWSLambdaBasicExecutionRole
resource "aws_iam_policy" "lambda_unsplash_logging" {
name = "lambda_unsplash_logging"
path = "/"
description = "IAM policy for logging from a lambda"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*",
"Effect": "Allow"
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "unsplash_lambda_logs" {
role = aws_iam_role.iam_for_unsplash_lambda.name
policy_arn = aws_iam_policy.lambda_unsplash_logging.arn
}