-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda.tf
54 lines (49 loc) · 1.77 KB
/
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
/*
* Process long running jobs, triggered by a Cloudwatch event (below)
*/
resource "aws_lambda_function" "example" {
function_name = "processMessage-${local.suffix}"
s3_bucket = "${var.bucket}"
s3_key = "v${var.app_version}/processMessage.zip"
handler = "processMessage.handler"
runtime = "nodejs8.10"
role = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/${var.lambda_role}"
environment {
variables = {
queueUrl = "${aws_sqs_queue.example_queue.id}"
table = "${aws_dynamodb_table.status.name}"
}
}
tags = local.tags
}
/*
* enable our function to be called by the API for testing, it'll usually
* be called by the Cloudwatch event below.
*/
resource "aws_lambda_permission" "apigw" {
statement_id = "AllowAPIGatewayInvoke"
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.example.arn}"
principal = "apigateway.amazonaws.com"
# grant permission on the API instead of the deployment to enable testing
# from the API Gateway AWS portal.
source_arn = "${aws_api_gateway_rest_api.example.execution_arn}/*/*"
}
/*
* Run our function to process long running jobs on a regular basis.
*/
resource "aws_cloudwatch_event_rule" "builder_jobs" {
name = "BuilderJobs"
schedule_expression = "rate(1 minute)"
}
resource "aws_cloudwatch_event_target" "process_jobs" {
rule = "${aws_cloudwatch_event_rule.builder_jobs.name}"
arn = "${aws_lambda_function.example.arn}"
}
resource "aws_lambda_permission" "allow_cloudwatch" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.example.function_name}"
principal = "events.amazonaws.com"
source_arn = "${aws_cloudwatch_event_rule.builder_jobs.arn}"
}