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

r/aws_kms key: add configurable timeout #34112

Merged
merged 3 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
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
Loading