Skip to content

Commit

Permalink
Merge pull request #34112 from hashicorp/f-kms_key_timeout
Browse files Browse the repository at this point in the history
r/aws_kms key: add configurable timeout
  • Loading branch information
johnsonaj authored Oct 25, 2023
2 parents 473a9ba + 74522df commit c3acc6b
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .changelog/34112.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_kms_key: Add configurable timeouts
```
2 changes: 1 addition & 1 deletion internal/service/kms/external_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func resourceExternalKeyCreate(ctx context.Context, d *schema.ResourceData, meta
// KMS will report this error until it can validate the policy itself.
// They acknowledge this here:
// http://docs.aws.amazon.com/kms/latest/APIReference/API_CreateKey.html
output, err := WaitIAMPropagation(ctx, func() (*kms.CreateKeyOutput, error) {
output, err := WaitIAMPropagation(ctx, propagationTimeout, func() (*kms.CreateKeyOutput, error) {
return conn.CreateKeyWithContext(ctx, input)
})

Expand Down
7 changes: 6 additions & 1 deletion internal/service/kms/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"fmt"
"log"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/kms"
Expand Down Expand Up @@ -39,6 +40,10 @@ func ResourceKey() *schema.Resource {
StateContext: schema.ImportStatePassthroughContext,
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(2 * time.Minute),
},

CustomizeDiff: verify.SetTagsDiff,

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -157,7 +162,7 @@ func resourceKeyCreate(ctx context.Context, d *schema.ResourceData, meta interfa
// The KMS service's awareness of principals is limited by "eventual consistency".
// They acknowledge this here:
// http://docs.aws.amazon.com/kms/latest/APIReference/API_CreateKey.html
output, err := WaitIAMPropagation(ctx, func() (*kms.CreateKeyOutput, error) {
output, err := WaitIAMPropagation(ctx, d.Timeout(schema.TimeoutCreate), func() (*kms.CreateKeyOutput, error) {
return conn.CreateKeyWithContext(ctx, input)
})

Expand Down
2 changes: 1 addition & 1 deletion internal/service/kms/replica_external_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func resourceReplicaExternalKeyCreate(ctx context.Context, d *schema.ResourceDat

replicateConn := kms.New(session)

output, err := WaitIAMPropagation(ctx, func() (*kms.ReplicateKeyOutput, error) {
output, err := WaitIAMPropagation(ctx, propagationTimeout, func() (*kms.ReplicateKeyOutput, error) {
return replicateConn.ReplicateKeyWithContext(ctx, input)
})

Expand Down
2 changes: 1 addition & 1 deletion internal/service/kms/replica_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func resourceReplicaKeyCreate(ctx context.Context, d *schema.ResourceData, meta

replicateConn := kms.New(session)

output, err := WaitIAMPropagation(ctx, func() (*kms.ReplicateKeyOutput, error) {
output, err := WaitIAMPropagation(ctx, propagationTimeout, func() (*kms.ReplicateKeyOutput, error) {
return replicateConn.ReplicateKeyWithContext(ctx, input)
})

Expand Down
4 changes: 2 additions & 2 deletions internal/service/kms/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ const (

// WaitIAMPropagation retries the specified function if the returned error indicates an IAM eventual consistency issue.
// If the retries time out the specified function is called one last time.
func WaitIAMPropagation[T any](ctx context.Context, f func() (T, error)) (T, error) {
outputRaw, err := tfresource.RetryWhenAWSErrCodeEquals(ctx, propagationTimeout, func() (interface{}, error) {
func WaitIAMPropagation[T any](ctx context.Context, timeout time.Duration, f func() (T, error)) (T, error) {
outputRaw, err := tfresource.RetryWhenAWSErrCodeEquals(ctx, timeout, func() (interface{}, error) {
return f()
},
kms.ErrCodeMalformedPolicyDocumentException)
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/kms_key.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ This resource exports the following attributes in addition to the arguments abov
* `key_id` - The globally unique identifier for the key.
* `tags_all` - A map of tags assigned to the resource, including those inherited from the provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block).

## Timeouts

~> **Note:** There are a variety of default timeouts set internally. If you set a shorter custom timeout than one of the defaults, the custom timeout will not be respected as the longer of the custom or internal default will be used.

[Configuration options](https://developer.hashicorp.com/terraform/language/resources/syntax#operation-timeouts):

* `create` - (Default `2m`)

## Import

In Terraform v1.5.0 and later, use an [`import` block](https://developer.hashicorp.com/terraform/language/import) to import KMS Keys using the `id`. For example:
Expand Down

0 comments on commit c3acc6b

Please sign in to comment.