hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.74k stars 9.1k forks source link

[Enhancement]: Add retry support for aws_lambda_invocation (resource and data source) #38082

Open theipster opened 2 months ago

theipster commented 2 months ago

Description

The purpose of both the aws_lambda_invocation resource and data source are to invoke a Lambda synchronously (i.e. using the RequestResponse invocation type). Furthermore, for synchronous invocations, it is the responsibility of the invoker (i.e. the aws_lambda_invocation resource/data source) to handle retries as appropriate.

This enhancement request is to enable practitioners to configure retry parameters for aws_lambda_invocation.

Use cases

In some (many?) real-world scenarios, infrastructure requirements do not change on a frequent basis. Therefore, when a Terraform workload depends on invoking a Lambda function, it may not be efficient or cost-effective to permanently configure provisioned concurrency on the Lambda function. Unfortunately, this means on the rare occasions that the Terraform workload does need to be run, there is a risk of a cold start, which could result in an aws_lambda_invocation timeout and ultimately a failure of the entire Terraform run - all of which could have easily been contained and handled by the aws_lambda_invocation by retrying the request.

Implementation suggestions

For inspiration, the hashicorp/http provider's http data source supports a retry configuration block.

data "http" "foo" {
  ...

  retry {
    attempts = 2

    max_delay_ms = 2000
    min_delay_ms = 1000
  }
}

Affected Resource(s) and/or Data Source(s)

Potential Terraform Configuration

resource "aws_lambda_invocation" "foo" {
  ...

  retry {
    attempts = 2

    max_delay_ms = 2000
    min_delay_ms = 1000
  }
}

data "aws_lambda_invocation" "foo" {
  ...

  retry {
    attempts = 2

    max_delay_ms = 2000
    min_delay_ms = 1000
  }
}

References

Would you like to implement a fix?

Yes

github-actions[bot] commented 2 months ago

Community Note

Voting for Prioritization

Volunteering to Work on This Issue

jufemaiz commented 2 months ago

Oh I am here for this proposal, but for a slightly different reason.

To be clear, the issue we are having is with an updated lambda function returning a valid update but not quite ready for invocation causing failures in the application of a plan due to:


│ Error: invoking Lambda Function (FUNCTION_NAME): operation error Lambda: Invoke, https response error StatusCode: 409, RequestID: UUID, api error CodeArtifactUserPendingException: ERROR: Lambda is initializing your function. It will be ready to invoke shortly.
--
  | │
  | │   with module.MODULE_NAME.aws_lambda_invocation.RESOURCE_NAME[0],
  | │   on .terraform/modules/MODULE/FILENAME.tf line LINE_NUMBER, in resource "aws_lambda_invocation" "RESOURCE_NAME":
  | │   LINE_NUMBER: resource "aws_lambda_invocation" "RESOURCE_NAME" {
  | │
  | ╵