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

Add destroy invocation to aws_lambda_invocation resource #23555

Closed
KyleKotowick opened this issue Mar 7, 2022 · 6 comments · Fixed by #29367
Closed

Add destroy invocation to aws_lambda_invocation resource #23555

KyleKotowick opened this issue Mar 7, 2022 · 6 comments · Fixed by #29367
Labels
enhancement Requests to existing resources that expand the functionality or scope. service/lambda Issues and PRs that pertain to the lambda service.
Milestone

Comments

@KyleKotowick
Copy link
Contributor

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Description

The aws_lambda_invocation resource is quite new, and is an excellent addition. However, it does not offer the ability to invoke a Lambda upon destruction of the resource, which limits its usefulness.

For example, I'd like to use this resource to automatically create database users when I create RDS clusters in a private VPC that cannot be accessed from the machine running Terraform. I can now do that by creating a Lambda in that VPC that can create users, and then using this resource to invoke that Lambda once for each user I'd like to create.

But then, how can I delete the database users if I no longer need them? Ideally, I'd be able to set a destruction configuration block which would allow me to do that, similar to the local-exec provisioner with when = destroy. If this configuration block is provided, the Lambda would be invoked again upon destruction with the associated configuration.

New or Affected Resource(s)

Potential Terraform Configuration

resource "aws_lambda_invocation" "example" {
  function_name = aws_lambda_function.db_user_manager.function_name
  qualifier = "$LATEST"

  input = jsonencode({
    db_host = "db.host.com"
    db_port = "5432"
    username = local.username 
    action = "mydbuser"
  })

  destroy {
    function_name = self.function_name
    qualifier = self.qualifier
    input = jsonencode({
      db_host = self.input .db_host
      db_port = self.input .db_port
      username = self.input .username 
      action = "DESTROY"
   })
  }
}
@KyleKotowick KyleKotowick added the enhancement Requests to existing resources that expand the functionality or scope. label Mar 7, 2022
@github-actions github-actions bot added needs-triage Waiting for first response or review from a maintainer. service/lambda Issues and PRs that pertain to the lambda service. labels Mar 7, 2022
@justinretzolk justinretzolk removed the needs-triage Waiting for first response or review from a maintainer. label Mar 7, 2022
@ugurzongur
Copy link

We have recently developed a provider to address this, along with some other issues, if you'd like to check: https://registry.terraform.io/providers/thetradedesk/lambdabased

@pvbouwel
Copy link
Contributor

I've opened #29367 which would trigger the Lambda function on every resource state transition. It will manipulate the JSON payload to provide details to the Lambda function. It will add a key tf which holds the action [create, update, delete] and the prev_input (null for a create otherwise the event from the previous invocation.
So the lambda handler can decide which processing to do based on event.tf.action.
In case of an update you can also do the required changes since tf.prev_input.username and username in the JSON body will allow you to create an alter user SQL statement to implement the required changes.

This way there is no need to provision multiple lambda functions as a single function can handle each event accordingly.

@skorfmann
Copy link
Contributor

We have recently developed a provider to address this, along with some other issues, if you'd like to check: https://registry.terraform.io/providers/thetradedesk/lambdabased

That's quite nice, thanks for sharing!

Would love to see the something like the finalizer in the aws provider resource as well.

@kaushal02
Copy link

We have recently developed a provider to address this, along with some other issues, if you'd like to check: https://registry.terraform.io/providers/thetradedesk/lambdabased

This seems to have been moved to a private repo now.

@github-actions github-actions bot added this to the v5.1.0 milestone May 30, 2023
@github-actions
Copy link

github-actions bot commented Jun 1, 2023

This functionality has been released in v5.1.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!

@github-actions
Copy link

github-actions bot commented Jul 2, 2023

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement Requests to existing resources that expand the functionality or scope. service/lambda Issues and PRs that pertain to the lambda service.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants