-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
92 lines (77 loc) · 2.83 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
82
83
84
85
86
87
88
89
90
91
92
# Create DynamoDb table
resource "aws_dynamodb_table" "holtz_lambda_dynamodb_table" {
name = "holtz_lambda_dynamodb"
billing_mode = "PROVISIONED" # Use Provisioned Mode
read_capacity = 1 # Read Capacity Units
write_capacity = 1 # Write Capacity Units
attribute {
name = "pk" # Partition key
type = "S" # S = String
}
hash_key = "pk" # Partition key
stream_enabled = true # Enable DynamoDB Stream
stream_view_type = "NEW_AND_OLD_IMAGES" # Stream new and old images
tags = {
Name = "Holtz.Lambda.DynamoDb"
Environment = "Prod"
}
}
# Create IAM Role for Lambda to assume
resource "aws_iam_role" "lambda_role" {
name = "holtz_lambda_dynamodb_role"
assume_role_policy = jsonencode({
Version = "2012-10-17",
Statement = [{
Action = "sts:AssumeRole",
Effect = "Allow",
Principal = {
Service = "lambda.amazonaws.com"
},
}]
})
}
# Attach policy to the role for CloudWatch logging
resource "aws_iam_role_policy_attachment" "lambda_basic_execution" {
role = aws_iam_role.lambda_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
# Attach policy for DynamoDB access
resource "aws_iam_role_policy" "lambda_dynamodb_access" {
name = "lambda_dynamodb_access_policy"
role = aws_iam_role.lambda_role.id
policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Action = [
"dynamodb:ListStreams",
"dynamodb:GetRecords",
"dynamodb:GetShardIterator",
"dynamodb:DescribeStream",
"dynamodb:Read"
],
Effect = "Allow",
Resource = aws_dynamodb_table.holtz_lambda_dynamodb_table.stream_arn
}
]
})
}
# Deploy the Lambda function
resource "aws_lambda_function" "holtz_lambda_dynamodb" {
function_name = "HoltzLambdaDynamoDb"
role = aws_iam_role.lambda_role.arn
handler = "Holtz.Lambda.DynamoDb::Holtz.Lambda.DynamoDb.Function::FunctionHandler" # Set to your handler
runtime = "dotnet8" # Ensure you're using the correct .NET runtime version
# Specify the Lambda deployment package (ZIP file)
filename = "src/Holtz.Lambda.DynamoDb/publish/HoltzLambdaDynamoDb.zip"
source_code_hash = filebase64sha256("src/Holtz.Lambda.DynamoDb/publish/HoltzLambdaDynamoDb.zip")
memory_size = 512
timeout = 5 # Timeout in seconds
}
# Create Event Source Mapping to trigger Lambda from DynamoDB Stream
resource "aws_lambda_event_source_mapping" "dynamodb_trigger" {
event_source_arn = aws_dynamodb_table.holtz_lambda_dynamodb_table.stream_arn
function_name = aws_lambda_function.holtz_lambda_dynamodb.arn
starting_position = "LATEST"
depends_on = [aws_dynamodb_table.holtz_lambda_dynamodb_table]
}