@@ -46,17 +46,24 @@ data "sysdig_secure_cloud_ingestion_assets" "assets" {
4646locals  {
4747  trusted_identity  =  . is_gov_cloud_onboarding  ?  data. sysdig_secure_trusted_cloud_identity . trusted_identity . gov_identity  :  data. sysdig_secure_trusted_cloud_identity . trusted_identity . identity 
4848
49-   topic_name  =  split (" :" . topic_arn )[5 ]
50-   topic_region  =  split (" :" . topic_arn )[3 ]
49+ 
5150  routing_key       =  . sysdig_secure_cloud_ingestion_assets . assets . aws . sns_routing_key 
5251  ingestion_url     =  . sysdig_secure_cloud_ingestion_assets . assets . aws . sns_routing_url 
53-   
54-   #  Determine bucket owner account ID - use provided value or default to current account
52+ 
53+   #  Topic variables
54+   topic_name  =  split (" :" . topic_arn )[5 ]
55+   topic_region  =  split (" :" . topic_arn )[3 ]
56+   topic_account_id  =  split (" :" . topic_arn )[4 ]
57+   is_cross_account_topic  =  . topic_account_id  !=  data. aws_caller_identity . current . account_id 
58+ 
59+   #  Bucket variables
5560  bucket_account_id  =  . bucket_account_id  !=  null  ?  var. bucket_account_id  :  data. aws_caller_identity . current . account_id 
56-   
57-   #  Flag for cross-account bucket access
5861  is_cross_account  =  . bucket_account_id  !=  null  &&  var. bucket_account_id  !=  data. aws_caller_identity . current . account_id 
5962
63+   #  KMS variables
64+   kms_account_id  =  split (" :" . kms_key_arn )[3 ]
65+   need_kms_policy  =  . bucket_account_id  !=  null  &&  var. bucket_account_id  !=  local. kms_account_id 
66+ 
6067  account_id_hash   =  substr (md5 (local. bucket_account_id ), 0 , 4 )
6168  role_name         =  " ${ var . name } -${ random_id . suffix . hex } -${ local . account_id_hash } " 
6269
@@ -183,6 +190,7 @@ resource "aws_sns_topic_policy" "cloudtrail_notifications" {
183190}
184191
185192resource  "aws_sns_topic_subscription"  "cloudtrail_notifications"  {
193+   count  =  ! local. is_cross_account_topic  ?  1  :  0 
186194  topic_arn  =  . topic_arn 
187195  provider  =  . sns 
188196  protocol   =  " https" 
@@ -207,9 +215,12 @@ resource "aws_cloudformation_stack_set" "cloudlogs_s3_access" {
207215  parameters  =  
208216    RoleName =  local.role_name
209217    BucketAccountId =  local.bucket_account_id
218+     TopicAccountId =  local.topic_account_id
210219    SysdigTrustedIdentity =  local.trusted_identity
211220    SysdigExternalId =  data.sysdig_secure_tenant_external_id.external_id.external_id
212221    KmsKeyArn =  var.kms_key_arn
222+     TopicArn =  var.topic_arn
223+     IngestionUrl =  local.ingestion_url
213224  }
214225
215226  permission_model        =  " SERVICE_MANAGED" 
@@ -229,7 +240,8 @@ resource "aws_cloudformation_stack_set" "cloudlogs_s3_access" {
229240  tags  =  . tags 
230241}
231242
232- resource  "aws_cloudformation_stack_set_instance"  "cloudlogs_s3_access"  {
243+ #  StackSet instance for the bucket account
244+ resource  "aws_cloudformation_stack_set_instance"  "cloudlogs_s3_access_bucket"  {
233245  count  =  . is_cross_account  ?  1  :  0 
234246
235247  stack_set_name  =  . cloudlogs_s3_access [0 ]. name 
@@ -249,6 +261,27 @@ resource "aws_cloudformation_stack_set_instance" "cloudlogs_s3_access" {
249261  }
250262}
251263
264+ #  StackSet instance for the topic account
265+ resource  "aws_cloudformation_stack_set_instance"  "cloudlogs_s3_access_topic"  {
266+   count  =  . is_cross_account  ?  1  :  0 
267+ 
268+   stack_set_name  =  . cloudlogs_s3_access [0 ]. name 
269+   
270+   deployment_targets  {
271+     organizational_unit_ids  =  . org_units 
272+     account_filter_type  =  " INTERSECTION" 
273+     accounts  =  local . topic_account_id ]
274+   }
275+   
276+   region  =  . topic_region 
277+ 
278+   timeouts  {
279+     create  =  . timeout 
280+     update  =  . timeout 
281+     delete  =  . timeout 
282+   }
283+ }
284+ 
252285# -----------------------------------------------------------------------------------------------------------------------------------------
253286#  Call Sysdig Backend to add the cloud logs integration
254287# -----------------------------------------------------------------------------------------------------------------------------------------
@@ -272,6 +305,7 @@ resource "sysdig_secure_cloud_auth_account_component" "aws_cloud_logs" {
272305
273306  depends_on  =  
274307    aws_iam_role . cloudlogs_s3_access ,
275-     aws_cloudformation_stack_set_instance . cloudlogs_s3_access 
308+     aws_cloudformation_stack_set_instance . cloudlogs_s3_access_bucket ,
309+     aws_cloudformation_stack_set_instance . cloudlogs_s3_access_topic 
276310  ]
277311}
0 commit comments