Skip to content

Commit 7635c21

Browse files
authored
fix: add KMS policy statement only if key given (#1258)
## Description As we assumed that the KMS key is always present (why isn't it?), the policy is created without any resource in case we don't have any key. This PR introduces a separate policy created only if the key is present. Closes #1257
1 parent c711b80 commit 7635c21

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

modules/terminate-agent-hook/iam.tf

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,26 @@ resource "aws_iam_role" "lambda" {
3232
tags = var.tags
3333
}
3434

35+
resource "aws_iam_role_policy_attachment" "lambda_kms" {
36+
count = var.kms_key_id != "" ? 1 : 0
37+
38+
role = aws_iam_role.lambda.name
39+
policy_arn = aws_iam_policy.lambda_kms[0].arn
40+
}
41+
42+
resource "aws_iam_policy" "lambda_kms" {
43+
count = var.kms_key_id != "" ? 1 : 0
44+
45+
name = "${var.name_iam_objects}-${var.name}-lambda-kms"
46+
path = "/"
47+
policy = data.aws_iam_policy_document.kms_key[0].json
48+
49+
tags = var.tags
50+
}
51+
52+
data "aws_iam_policy_document" "kms_key" {
53+
count = var.kms_key_id != "" ? 1 : 0
3554

36-
# This IAM policy is used by the Lambda function.
37-
data "aws_iam_policy_document" "lambda" {
3855
# checkov:skip=CKV_AWS_111:Write access is limited to the resources needed
3956
statement {
4057
sid = "AllowKmsAccess"
@@ -44,7 +61,9 @@ data "aws_iam_policy_document" "lambda" {
4461
resources = [var.kms_key_id]
4562
effect = "Allow"
4663
}
64+
}
4765

66+
data "aws_iam_policy_document" "lambda" {
4867
# Permit the function to get a list of instances
4968
statement {
5069
sid = "GitLabRunnerLifecycleGetInstances"

modules/terminate-agent-hook/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ variable "name_docker_machine_runners" {
5757
}
5858

5959
variable "kms_key_id" {
60-
description = "KMS key id to encrypt the resources, e.g. logs, lambda environment variables, ..."
60+
description = "(optional) KMS key id to encrypt the resources, e.g. logs, lambda environment variables, ..."
6161
type = string
6262
}
6363

0 commit comments

Comments
 (0)