-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws-lambda-setup.tf
110 lines (96 loc) · 3.09 KB
/
aws-lambda-setup.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
data "aws_iam_policy_document" "assume_role" {
statement {
effect = "Allow"
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
actions = ["sts:AssumeRole"]
}
}
# Create the Lambda execution role and policy
resource "aws_iam_role" "flight_consolidator_lambda" {
name = "ccaf_flight_consolidator_app_role"
assume_role_policy = data.aws_iam_policy_document.assume_role.json
}
data "aws_secretsmanager_secret" "ccaf_secrets_path" {
name = var.ccaf_secrets_path
}
resource "aws_iam_policy" "flight_consolidator_lambda_policy" {
name = "ccaf_flight_consolidator_app_policy"
description = "IAM policy for the flight_consolidator Lambda execution role."
policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Action = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
Effect = "Allow",
Resource = "arn:aws:logs:*:*:*"
},
{
Action = [
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage",
"ecr:BatchCheckLayerAvailability"
],
Effect = "Allow",
Resource = local.ecr_repo_uri
},
{
Action = "ecr:GetAuthorizationToken",
Effect = "Allow",
Resource = "*"
},
{
Action = [
"secretsmanager:GetSecretValue"
],
Effect = "Allow",
Resource = [
data.aws_secretsmanager_secret.ccaf_secrets_path.arn
]
}
]
})
}
# Attach the policy to the role
resource "aws_iam_role_policy_attachment" "flight_consolidator_lambda_policy_attachment" {
role = aws_iam_role.flight_consolidator_lambda.name
policy_arn = aws_iam_policy.flight_consolidator_lambda_policy.arn
depends_on = [
aws_iam_role.flight_consolidator_lambda,
aws_iam_policy.flight_consolidator_lambda_policy
]
}
# Lambda function
resource "aws_lambda_function" "flight_consolidator_lambda_function" {
function_name = "ccaf_flight_consolidator_app_function"
role = aws_iam_role.flight_consolidator_lambda.arn
package_type = "Image"
image_uri = local.repo_uri
memory_size = var.aws_lambda_memory_size
timeout = var.aws_lambda_timeout
depends_on = [ aws_iam_role.flight_consolidator_lambda ]
}
# Create a CloudWatch log group for the Lambda function
resource "aws_cloudwatch_log_group" "flight_consolidator_lambda_function_log_group" {
name = "/aws/lambda/${aws_lambda_function.flight_consolidator_lambda_function.function_name}"
retention_in_days = var.aws_log_retention_in_days
}
# Lambda function invocation
resource "aws_lambda_invocation" "flight_consolidator_lambda_function" {
function_name = aws_lambda_function.flight_consolidator_lambda_function.function_name
input = jsonencode({
catalog_name = var.catalog_name
database_name = var.database_name
ccaf_secrets_path = var.ccaf_secrets_path
})
depends_on = [
aws_iam_policy.flight_consolidator_lambda_policy,
aws_lambda_function.flight_consolidator_lambda_function
]
}