Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use trail arn instead of name to get organization trails from a delegated account #30758

Merged
merged 18 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/30758.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_cloudtrail: Use Trail ARN instead of name to get organization trails from a delegated account.
```
29 changes: 22 additions & 7 deletions internal/service/cloudtrail/cloudtrail.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func resourceCloudTrailCreate(ctx context.Context, d *schema.ResourceData, meta

log.Printf("[DEBUG] CloudTrail created: %s", t)

d.SetId(aws.StringValue(t.Name))
d.SetId(aws.StringValue(t.TrailARN))

// AWS CloudTrail sets newly-created trails to false.
if v, ok := d.GetOk("enable_logging"); ok && v.(bool) {
Expand Down Expand Up @@ -355,16 +355,31 @@ func resourceCloudTrailRead(ctx context.Context, d *schema.ResourceData, meta in
aws.String(d.Id()),
},
}
resp, err := conn.DescribeTrailsWithContext(ctx, &input)
if err != nil {
return create.DiagError(names.CloudTrail, create.ErrActionReading, ResNameTrail, d.Id(), errors.New("not found after creation"))
}

// CloudTrail does not return a NotFound error in the event that the Trail
// you're looking for is not found. Instead, it's simply not in the list.
var resp *cloudtrail.DescribeTrailsOutput
err := retry.RetryContext(ctx, propagationTimeout, func() *retry.RetryError {
var err error
resp, err = conn.DescribeTrailsWithContext(ctx, &input)
if err != nil {
return retry.NonRetryableError(err)
} else if d.IsNewResource() && len(resp.TrailList) == 0 {
err := errors.New("not found after creation")
return retry.RetryableError(err)
}
return nil
})
if tfresource.TimedOut(err) {
resp, err = conn.DescribeTrailsWithContext(ctx, &input)
}
if err != nil {
return sdkdiag.AppendErrorf(diags, "getting CloudTrail (%s): %s", d.Id(), err)
}

var trail *cloudtrail.Trail
for _, c := range resp.TrailList {
if d.Id() == aws.StringValue(c.Name) {
if d.Id() == aws.StringValue(c.TrailARN) || d.Id() == aws.StringValue(c.Name) {
trail = c
}
}
Expand Down Expand Up @@ -401,7 +416,7 @@ func resourceCloudTrailRead(ctx context.Context, d *schema.ResourceData, meta in
d.Set("arn", arn)
d.Set("home_region", trail.HomeRegion)

logstatus, err := getLoggingStatus(ctx, conn, trail.Name)
logstatus, err := getLoggingStatus(ctx, conn, trail.TrailARN)
if err != nil {
return create.DiagError(names.CloudTrail, create.ErrActionReading, ResNameTrail, d.Id(), err)
}
Expand Down
10 changes: 9 additions & 1 deletion website/docs/r/cloudtrail.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,16 @@ In addition to all arguments above, the following attributes are exported:

## Import

Cloudtrails can be imported using the `name`, e.g.,
Cloudtrails can be imported using the `name` or `arn`, e.g.,

```
$ terraform import aws_cloudtrail.sample my-sample-trail
```

or

```
$ terraform import aws_cloudtrail.sample arn:aws:cloudtrail:us-east-1:123456789012:trail/my-sample-trail
```

If importing an organization trail from a delegated account, the `arn` must be used.