Skip to content

Commit c175f88

Browse files
Added the iam role and policy
1 parent 83480d5 commit c175f88

3 files changed

Lines changed: 81 additions & 0 deletions

File tree

terraform/modules/iam/main.tf

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# IAM Role for Lambda function
2+
resource "aws_iam_role" "lambda_role" {
3+
name = "${var.environment}-serverless-health-check-api-lambda-role"
4+
5+
assume_role_policy = jsonencode({
6+
Version = "2012-10-17"
7+
Statement = [
8+
{
9+
Action = "sts:AssumeRole"
10+
Effect = "Allow"
11+
Principal = {
12+
Service = "lambda.amazonaws.com"
13+
}
14+
}
15+
]
16+
})
17+
18+
tags = var.common_tags
19+
}
20+
21+
# IAM Policy for CloudWatch Logs
22+
resource "aws_iam_role_policy" "lambda_cloudwatch_policy" {
23+
name = "${var.environment}-serverless-health-check-api-lambda-cloudwatch-policy"
24+
role = aws_iam_role.lambda_role.id
25+
policy = jsonencode({
26+
Version = "2012-10-17"
27+
Statement = [
28+
{
29+
Effect = "Allow"
30+
Action = [
31+
"logs:CreateLogGroup",
32+
"logs:CreateLogStream",
33+
"logs:PutLogEvents"
34+
]
35+
Resource = "arn:aws:logs:*:*:*"
36+
}
37+
]
38+
})
39+
}
40+
41+
# IAM Policy for DynamoDB
42+
resource "aws_iam_role_policy" "lambda_dynamodb_policy" {
43+
name = "${var.environment}-serverless-health-check-api-lambda-dynamodb-policy"
44+
role = aws_iam_role.lambda_role.id
45+
46+
policy = jsonencode({
47+
Version = "2012-10-17"
48+
Statement = [
49+
{
50+
Effect = "Allow"
51+
Action = [
52+
"dynamodb:PutItem"
53+
]
54+
Resource = var.dynamodb_table_arn
55+
}
56+
]
57+
})
58+
}

terraform/modules/iam/outputs.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
output "lambda_role_arn" {
2+
description = "ARN of the Lambda execution role"
3+
value = aws_iam_role.lambda_role.arn
4+
}
5+
6+
output "lambda_role_name" {
7+
description = "Name of the Lambda execution role"
8+
value = aws_iam_role.lambda_role.name
9+
}

terraform/modules/iam/variables.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
variable "environment" {
2+
description = "Environment name"
3+
type = string
4+
}
5+
6+
variable "dynamodb_table_arn" {
7+
description = "ARN of the DynamoDB"
8+
type = string
9+
}
10+
11+
variable "common_tags" {
12+
description = "Common tags for all resources"
13+
type = map(string)
14+
}

0 commit comments

Comments
 (0)