-
Notifications
You must be signed in to change notification settings - Fork 16
/
Lambda_Start.tf
60 lines (50 loc) · 2.15 KB
/
Lambda_Start.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
# ----------------------------------
# Lambda_Function
# ----------------------------------
#archiving py file to zip
data "archive_file" "start_lambda_zip" {
type = "zip"
source_dir = "./lambda_code/start/"
output_path = "tmp/${local.start_lambda_function}.zip"
}
resource "aws_lambda_function" "ec2_start_lambda_function" {
filename = data.archive_file.start_lambda_zip.output_path
function_name = "ec2-start-lambda-function"
role = aws_iam_role.lambda_iam_role_terraform.arn
description = "This lambda using to start the ec2 instance which we mentioned"
handler = "lambda_function.lambda_handler"
runtime = local.runtime_lambda_function
timeout = 60
memory_size = 128
environment {
variables = {
REGION = var.aws_region
TAG = var.ec2_tag
}
}
tags = tomap({"Name" = "ec2 start lambda function"})
}
#-----------------------
# CloudWatch Trigger to start ec2 instance
#-----------------------
resource "aws_cloudwatch_event_rule" "trigger_to_start_ec2_instance" {
name = "Trigger-start-ec2-instance-lambda"
description = "Trigger that moving data lambda"
schedule_expression = var.start_cron
tags = tomap({"Name" = "ec2 start cloudwatch trigger"})
depends_on = [aws_lambda_function.ec2_start_lambda_function]
}
resource "aws_cloudwatch_event_target" "send_to_start_lambda_target" {
rule = aws_cloudwatch_event_rule.trigger_to_start_ec2_instance.name
target_id = "SendToLambda"
arn = aws_lambda_function.ec2_start_lambda_function.arn
depends_on = [aws_lambda_function.ec2_start_lambda_function]
}
resource "aws_lambda_permission" "allow_cloudwatch_to_call_start_lambda" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.ec2_start_lambda_function.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.trigger_to_start_ec2_instance.arn
depends_on = [aws_lambda_function.ec2_start_lambda_function,aws_cloudwatch_event_rule.trigger_to_start_ec2_instance]
}