terraform-provider-aws: [Bug]: Org Trail resource creation failed for CloudTrail Delegated Administrator

Terraform Core Version

1.3.6

AWS Provider Version

4.47.0

Affected Resource(s)

aws_cloudtrail

Expected Behavior

  1. We choose a Delegated Administrator for the CloudTrail Service (see AWS doc). All CloudTrail resources remain in the Management Account, even if they are created by the Delegated Account. (the ARN show the Account ID of the Management Account).
  2. We deploy a new organization trail on this Delegated Account with Terraform.
  3. The organization trail is fully created and enabled in the Management Account.

Actual Behavior

When Terraform applies, it’s failed when it try to enable logging it in the Delegated Account because it’s did not find the trail.

After successfully creation, The AWS provider seems try to enable the trail locally (in the Delegated account) instead to referer to the trail ‘stored’ in the Management Account. The ARN in the below log explains the bug.

Relevant Error/Panic Output Snippet

‘111111111111’ is the Delegated Account ID.

aws_cloudtrail.org_main: Creating...
╷
│ Error: Error starting logging on CloudTrail (org-cloudtrail): TrailNotFoundException: Unknown trail: arn:aws:cloudtrail:us-east-1:111111111111:trail/org-cloudtrail for the user: 111111111111
│ 
│   with aws_cloudtrail.org_main,
│   on 01-cloudtrail.tf line 4, in resource "aws_cloudtrail" "org_main":
│    4: resource "aws_cloudtrail" "org_main" {
│ 
╵
Operation failed: failed running terraform apply (exit 1)

Terraform Configuration Files

resource "aws_cloudtrail" "org_main" {
  name                          = "org-cloudtrail"
  enable_logging                = true
  s3_bucket_name                = aws_s3_bucket.cloudtrail.id
  s3_key_prefix                 = ""
  include_global_service_events = true
  enable_log_file_validation    = false
  is_multi_region_trail         = true
  is_organization_trail         = true

  event_selector {
    read_write_type                  = "WriteOnly"
    include_management_events        = true
    exclude_management_event_sources = ["kms.amazonaws.com", "rdsdata.amazonaws.com"]
  }
}

Steps to Reproduce

  1. TF plan (ok)
  2. TF apply (failed)

Debug Output

No response

Panic Output

No response

Important Factoids

No response

References

https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-delegated-administrator.html

Would you like to implement a fix?

No

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 68
  • Comments: 17

Most upvoted comments

As a follow up to @andrewnicolalde, if you run aws --profile security-acct --region us-east-1 cloudtrail get-trail --name <arn of org trail> instead of the trail name in the Delegated Admin account, you DO get the details back.

So it would seem a simple fix would be to update the terraform aws provider to use the ARN of the trail instead of the name. The other option would be to use the describe-trails without any option, which DOES return the org trail, and then iterate over the values find the one that matches the Name that is being requested. Not as efficient, but given that the vast majority of users are not going to have more than a small number of trails defined (there isn’t much of a reasons to do so) it shouldn’t be an issue.

@bflad @ewbankkit This is impacting quite a few people and is critical functionality for cloudtrail management Could someone have a look at the PR above please?

Thank you

and we too, so just waiting (

Could you merge the PR please? Have the same issue

I have got the same issue. The following error occur when “terraform apply” is run again: “Error creating CloudTrail: TrailAlreadyExistsException: Trail [trailname] already exists for customer: 111111111 (Service: AWSCloudTrail; Status Code: 400; Error Code: TrailAlreadyExistsException;…”.

I have the same issue. Upvoting 👍🏻

This functionality has been released in v5.25.0 of the Terraform AWS Provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!

I tried the workaround suggested by @andrewnicolalde and it didn’t work. Could you please merge the PR- would make it amazing if this got fixed. 🥺

Have the exact same issue occurring as the original poster, when i go into the AWS console i can see the trail has been created but has not been activated. If i click start logging it starts up fine, so could be the issue be a timing related one.

Also if i run the script a second i get the exact same output as F-Xu above, Terraform doesn’t think it has already created the trail so its attempting to create it again.