Skip to content

Commit 60f9d7a

Browse files
authored
Merge pull request #16 from aquasecurity/SAAS-29539
SAAS-29539 - Auto-Discovery | AWS | Single | Terraform | Split volume scanning deployment
2 parents 6ead075 + 2761b5b commit 60f9d7a

File tree

19 files changed

+127
-72
lines changed

19 files changed

+127
-72
lines changed

main.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ module "single" {
4040
custom_vpc_subnet_route_table1_name = var.custom_vpc_subnet_route_table1_name
4141
custom_vpc_subnet_route_table2_name = var.custom_vpc_subnet_route_table2_name
4242
custom_cspm_regions = var.custom_cspm_regions
43+
volume_scanning_deployment = var.volume_scanning_deployment
4344
}
4445

4546
module "organization" {
@@ -70,5 +71,5 @@ module "organization" {
7071
custom_vpc_subnet_route_table1_name = var.custom_vpc_subnet_route_table1_name
7172
custom_vpc_subnet_route_table2_name = var.custom_vpc_subnet_route_table2_name
7273
custom_cspm_regions = var.custom_cspm_regions
73-
volume_scanning_deployment = var.volume_scanning_deployment
74+
volume_scanning_deployment = var.volume_scanning_deployment
7475
}

modules/organization/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ resource "aws_cloudformation_stack_set" "stack_set" {
1414

1515
operation_preferences {
1616
failure_tolerance_percentage = 100
17-
region_concurrency_type = "PARALLEL"
17+
region_concurrency_type = "PARALLEL"
1818
max_concurrent_percentage = 100
1919
}
2020

modules/organization/variables.tf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,13 @@ variable "custom_security_group_name" {
122122
}
123123

124124
variable "custom_cspm_regions" {
125-
description = "Custom CSPM regions"
126-
type = string
127-
default = ""
125+
description = "Custom CSPM regions"
126+
type = string
127+
default = ""
128128
}
129129

130130
variable "volume_scanning_deployment" {
131131
description = "Toggle to deploy Volume Scanning resources"
132-
type = string
133-
default = "true"
132+
type = string
133+
default = "true"
134134
}

modules/single/main.tf

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module "kinesis" {
77
aqua_volscan_api_token = var.aqua_volscan_api_token
88
custom_bucket_name = var.custom_bucket_name
99
custom_processor_lambda_role_name = var.custom_processor_lambda_role_name
10+
create_vol_scan_resource = var.volume_scanning_deployment == "true" ? true : false
1011
}
1112

1213
module "lambda" {
@@ -25,9 +26,9 @@ module "lambda" {
2526
aqua_cspm_role_prefix = var.aqua_cspm_role_prefix
2627
custom_agentless_role_name = var.custom_agentless_role_name
2728
custom_cspm_role_name = var.custom_cspm_role_name
28-
custom_cspm_regions = var.custom_cspm_regions
29+
custom_cspm_regions = var.custom_cspm_regions
30+
create_vol_scan_resource = var.volume_scanning_deployment == "true" ? true : false
2931
depends_on = [module.kinesis]
30-
3132
}
3233

3334
module "stackset" {
@@ -46,6 +47,7 @@ module "stackset" {
4647
custom_vpc_subnet2_name = var.custom_vpc_subnet2_name
4748
custom_security_group_name = var.custom_security_group_name
4849
event_bus_arn = module.kinesis.event_bus_arn
50+
create_vol_scan_resource = var.volume_scanning_deployment == "true" ? true : false
4951
depends_on = [module.lambda]
5052
}
5153

@@ -64,5 +66,6 @@ module "trigger" {
6466
volscan_role_arn = module.lambda.agentless_role_arn
6567
volscan_external_id = module.lambda.volscan_external_id
6668
additional_tags = var.additional_tags
69+
create_vol_scan_resource = var.volume_scanning_deployment == "true" ? true : false
6770
depends_on = [module.stackset]
6871
}

modules/single/modules/kinesis/main.tf

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
# Create Cloudwatch event bus
44
resource "aws_cloudwatch_event_bus" "event_bus" {
5-
name = "aqua-bus-${var.random_id}"
5+
count = var.create_vol_scan_resource ? 1 : 0
6+
name = "aqua-bus-${var.random_id}"
67
}
78

89
# Create Cloudwatch event rule for EBS events
910
resource "aws_cloudwatch_event_rule" "event_rule" {
11+
count = var.create_vol_scan_resource ? 1 : 0
1012
name = "aqua-autoconnect-event-rule-${var.random_id}"
1113
description = "Aqua EventBridge rule"
12-
event_bus_name = aws_cloudwatch_event_bus.event_bus.name
13-
role_arn = aws_iam_role.kinesis_stream_events_role.arn
14+
event_bus_name = aws_cloudwatch_event_bus.event_bus[0].name
15+
role_arn = aws_iam_role.kinesis_stream_events_role[0].arn
1416
event_pattern = jsonencode({
1517
"detail" : {
1618
"event" : [
@@ -33,12 +35,14 @@ resource "aws_cloudwatch_event_rule" "event_rule" {
3335
# Create Kinesis Processor lambda Cloudwatch log group
3436
# trivy:ignore:AVD-AWS-0017
3537
resource "aws_cloudwatch_log_group" "kinesis_processor_lambda_log_group" {
38+
count = var.create_vol_scan_resource ? 1 : 0
3639
name = "/aws/lambda/aqua-autoconnect-kinesis-processor-lambda-${var.random_id}"
3740
retention_in_days = 7
3841
}
3942

4043
# Create Kinesis Data Stream Events role
4144
resource "aws_iam_role" "kinesis_stream_events_role" {
45+
count = var.create_vol_scan_resource ? 1 : 0
4246
assume_role_policy = jsonencode({
4347
"Version" : "2012-10-17",
4448
"Statement" : [
@@ -61,7 +65,7 @@ resource "aws_iam_role" "kinesis_stream_events_role" {
6165
"kinesis:PutRecord",
6266
"kinesis:PutRecords"
6367
],
64-
"Resource" : aws_kinesis_stream.kinesis_stream.arn,
68+
"Resource" : aws_kinesis_stream.kinesis_stream[0].arn,
6569
"Effect" : "Allow"
6670
}
6771
]
@@ -73,6 +77,7 @@ resource "aws_iam_role" "kinesis_stream_events_role" {
7377
# Create Kinesis Firehose role
7478
#tfsec:ignore:aws-iam-no-policy-wildcards
7579
resource "aws_iam_role" "kinesis_firehose_role" {
80+
count = var.create_vol_scan_resource ? 1 : 0
7681
assume_role_policy = jsonencode({
7782
"Version" : "2012-10-17",
7883
"Statement" : [
@@ -98,7 +103,7 @@ resource "aws_iam_role" "kinesis_firehose_role" {
98103
"kinesis:GetShardIterator",
99104
"kinesis:ListShards"
100105
],
101-
"Resource" : aws_kinesis_stream.kinesis_stream.arn,
106+
"Resource" : aws_kinesis_stream.kinesis_stream[0].arn,
102107
"Effect" : "Allow",
103108
"Sid" : "kinesisStreamPermissions"
104109
},
@@ -107,7 +112,7 @@ resource "aws_iam_role" "kinesis_firehose_role" {
107112
"lambda:GetFunctionConfiguration",
108113
"lambda:InvokeFunction"
109114
],
110-
"Resource" : aws_kinesis_stream.kinesis_stream.arn,
115+
"Resource" : aws_kinesis_stream.kinesis_stream[0].arn,
111116
"Effect" : "Allow",
112117
"Sid" : "lambdaPermissions"
113118
},
@@ -120,8 +125,8 @@ resource "aws_iam_role" "kinesis_firehose_role" {
120125
"s3:PutObject"
121126
],
122127
"Resource" : [
123-
aws_s3_bucket.kinesis_firehose_bucket.arn,
124-
"${aws_s3_bucket.kinesis_firehose_bucket.arn}/*"
128+
aws_s3_bucket.kinesis_firehose_bucket[0].arn,
129+
"${aws_s3_bucket.kinesis_firehose_bucket[0].arn}/*"
125130
],
126131
"Effect" : "Allow",
127132
"Sid" : "s3Permissions"
@@ -134,6 +139,7 @@ resource "aws_iam_role" "kinesis_firehose_role" {
134139

135140
# Create Kinesis Processor lambda execution role
136141
resource "aws_iam_role" "processor_lambda_execution_role" {
142+
count = var.create_vol_scan_resource ? 1 : 0
137143
assume_role_policy = jsonencode({
138144
"Version" : "2012-10-17",
139145
"Statement" : [
@@ -169,12 +175,14 @@ resource "aws_iam_role" "processor_lambda_execution_role" {
169175
# trivy:ignore:AVD-AWS-0090
170176
# trivy:ignore:AVD-AWS-0089
171177
resource "aws_s3_bucket" "kinesis_firehose_bucket" {
178+
count = var.create_vol_scan_resource ? 1 : 0
172179
bucket = var.custom_bucket_name == "" ? "aqua-autoconnect-kinesis-firehose-bucket-${var.random_id}" : var.custom_bucket_name
173180
}
174181

175182
# Create Kinesis Firehose S3 bucket lifecycle configuration
176183
resource "aws_s3_bucket_lifecycle_configuration" "kinesis_firehose_bucket" {
177-
bucket = aws_s3_bucket.kinesis_firehose_bucket.bucket
184+
count = var.create_vol_scan_resource ? 1 : 0
185+
bucket = aws_s3_bucket.kinesis_firehose_bucket[0].bucket
178186
rule {
179187
expiration {
180188
days = 7
@@ -187,7 +195,8 @@ resource "aws_s3_bucket_lifecycle_configuration" "kinesis_firehose_bucket" {
187195

188196
# Create Kinesis Firehose S3 bucket public access block
189197
resource "aws_s3_bucket_public_access_block" "kinesis_firehose_bucket" {
190-
bucket = aws_s3_bucket.kinesis_firehose_bucket.bucket
198+
count = var.create_vol_scan_resource ? 1 : 0
199+
bucket = aws_s3_bucket.kinesis_firehose_bucket[0].bucket
191200
block_public_acls = true
192201
block_public_policy = true
193202
ignore_public_acls = true
@@ -197,7 +206,8 @@ resource "aws_s3_bucket_public_access_block" "kinesis_firehose_bucket" {
197206
# Create Kinesis Firehose S3 bucket SSE configuration
198207
# trivy:ignore:AVD-AWS-0132
199208
resource "aws_s3_bucket_server_side_encryption_configuration" "kinesis_firehose_bucket" {
200-
bucket = aws_s3_bucket.kinesis_firehose_bucket.bucket
209+
count = var.create_vol_scan_resource ? 1 : 0
210+
bucket = aws_s3_bucket.kinesis_firehose_bucket[0].bucket
201211
rule {
202212
apply_server_side_encryption_by_default {
203213
sse_algorithm = "AES256"
@@ -209,11 +219,12 @@ resource "aws_s3_bucket_server_side_encryption_configuration" "kinesis_firehose_
209219
# Create Kinesis Processor lambda function
210220
# trivy:ignore:AVD-AWS-0066
211221
resource "aws_lambda_function" "kinesis_processor_lambda" {
222+
count = var.create_vol_scan_resource ? 1 : 0
212223
architectures = ["x86_64"]
213224
description = "Aqua Kinesis Firehose Processor Lambda"
214225
function_name = "aqua-autoconnect-kinesis-processor-lambda-function-${var.random_id}"
215226
handler = "index.handler"
216-
role = aws_iam_role.processor_lambda_execution_role.arn
227+
role = aws_iam_role.processor_lambda_execution_role[0].arn
217228
runtime = "python3.12"
218229
timeout = 900
219230
filename = data.archive_file.kinesis_processor_function.output_path
@@ -225,6 +236,7 @@ resource "aws_lambda_function" "kinesis_processor_lambda" {
225236

226237
# Create Kinesis Stream
227238
resource "aws_kinesis_stream" "kinesis_stream" {
239+
count = var.create_vol_scan_resource ? 1 : 0
228240
encryption_type = "KMS"
229241
kms_key_id = "alias/aws/kinesis"
230242
name = "aqua-autoconnect-kinesis-datastream-${var.random_id}"
@@ -233,6 +245,7 @@ resource "aws_kinesis_stream" "kinesis_stream" {
233245

234246
# Create Kinesis Firehose Delivery Stream
235247
resource "aws_kinesis_firehose_delivery_stream" "kinesis_firehose" {
248+
count = var.create_vol_scan_resource ? 1 : 0
236249
destination = "http_endpoint"
237250
http_endpoint_configuration {
238251
access_key = var.aqua_volscan_api_token
@@ -244,21 +257,21 @@ resource "aws_kinesis_firehose_delivery_stream" "kinesis_firehose" {
244257
processors {
245258
parameters {
246259
parameter_name = "LambdaArn"
247-
parameter_value = aws_lambda_function.kinesis_processor_lambda.arn
260+
parameter_value = aws_lambda_function.kinesis_processor_lambda[0].arn
248261
}
249262
type = "Lambda"
250263
}
251264
}
252-
role_arn = aws_iam_role.kinesis_firehose_role.arn
265+
role_arn = aws_iam_role.kinesis_firehose_role[0].arn
253266
url = var.aqua_volscan_api_url
254267
s3_configuration {
255-
bucket_arn = aws_s3_bucket.kinesis_firehose_bucket.arn
256-
role_arn = aws_iam_role.kinesis_firehose_role.arn
268+
bucket_arn = aws_s3_bucket.kinesis_firehose_bucket[0].arn
269+
role_arn = aws_iam_role.kinesis_firehose_role[0].arn
257270
}
258271
}
259272
kinesis_source_configuration {
260-
kinesis_stream_arn = aws_kinesis_stream.kinesis_stream.arn
261-
role_arn = aws_iam_role.kinesis_firehose_role.arn
273+
kinesis_stream_arn = aws_kinesis_stream.kinesis_stream[0].arn
274+
role_arn = aws_iam_role.kinesis_firehose_role[0].arn
262275
}
263276
name = "aqua-autoconnect-kinesis-firehose-${var.random_id}"
264277
}

modules/single/modules/kinesis/outputs.tf

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,50 @@
22

33
output "event_bus_arn" {
44
description = "Cloudwatch Event Bus ARN"
5-
value = aws_cloudwatch_event_bus.event_bus.arn
5+
value = try(aws_cloudwatch_event_bus.event_bus[0].arn, "")
66
}
77

88
output "event_rule_arn" {
99
description = "Cloudwatch Event Rule ARN"
10-
value = aws_cloudwatch_event_rule.event_rule.arn
10+
value = try(aws_cloudwatch_event_rule.event_rule[0].arn, "")
1111
}
1212

1313
output "kinesis_processor_lambda_log_group_name" {
1414
description = "Kinesis Processor Lambda Cloudwatch Log Group Name"
15-
value = aws_cloudwatch_log_group.kinesis_processor_lambda_log_group.name
15+
value = try(aws_cloudwatch_log_group.kinesis_processor_lambda_log_group[0].name, "")
1616
}
1717

1818
output "kinesis_stream_events_role_arn" {
1919
description = "Kinesis Stream Events Role ARN"
20-
value = aws_iam_role.kinesis_stream_events_role.arn
20+
value = try(aws_iam_role.kinesis_stream_events_role[0].arn, "")
2121
}
2222

2323
output "kinesis_firehose_role_arn" {
2424
description = "Kinesis Firehose Role ARN"
25-
value = aws_iam_role.kinesis_firehose_role.arn
25+
value = try(aws_iam_role.kinesis_firehose_role[0].arn, "")
2626
}
2727

2828
output "kinesis_processor_lambda_execution_role_arn" {
2929
description = "Kinesis Processor Lambda Execution Role ARN"
30-
value = aws_iam_role.processor_lambda_execution_role.arn
30+
value = try(aws_iam_role.processor_lambda_execution_role[0].arn, "")
3131
}
3232

3333
output "kinesis_firehose_bucket_name" {
3434
description = "Kinesis Firehose S3 Bucket Name"
35-
value = aws_s3_bucket.kinesis_firehose_bucket.bucket
35+
value = try(aws_s3_bucket.kinesis_firehose_bucket[0].bucket, "")
3636
}
3737

3838
output "kinesis_processor_lambda_function_arn" {
3939
description = "Kinesis Processor Lambda Function ARN"
40-
value = aws_lambda_function.kinesis_processor_lambda.arn
40+
value = try(aws_lambda_function.kinesis_processor_lambda[0].arn, "")
4141
}
4242

4343
output "kinesis_stream_arn" {
4444
description = "Kinesis Stream ARN"
45-
value = aws_kinesis_stream.kinesis_stream.arn
45+
value = try(aws_kinesis_stream.kinesis_stream[0].arn, "")
4646
}
4747

4848
output "kinesis_firehose_delivery_stream_arn" {
4949
description = "Kinesis Firehose Delivery Stream ARN"
50-
value = aws_kinesis_firehose_delivery_stream.kinesis_firehose.arn
50+
value = try(aws_kinesis_firehose_delivery_stream.kinesis_firehose[0].arn, "")
5151
}

modules/single/modules/kinesis/variables.tf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,10 @@ variable "custom_bucket_name" {
2323
variable "custom_processor_lambda_role_name" {
2424
description = "Custom Processor lambda role Name"
2525
type = string
26-
}
26+
}
27+
28+
variable "create_vol_scan_resource" {
29+
description = "Create Volume Scanning Resource"
30+
type = bool
31+
default = true
32+
}

modules/single/modules/lambda/locals.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
locals {
44
# Decode the results of Lambda function invocations
55
cspm_external_id = jsondecode(aws_lambda_invocation.generate_cspm_external_id_function.result)["ExternalId"]
6-
volscan_external_id = jsondecode(aws_lambda_invocation.generate_volscan_external_id_function.result)["ExternalId"]
6+
volscan_external_id = try(jsondecode(aws_lambda_invocation.generate_volscan_external_id_function[0].result)["ExternalId"], "")
77
is_already_cspm_client = jsondecode(aws_lambda_invocation.create_cspm_key_function.result)["IsAlreadyCSPMClient"]
88
}

0 commit comments

Comments
 (0)