Terraform-provider-aws: AWS Lambda function invocation as data source (like CloudFormation custom resource)

Created on 21 Nov 2017  ยท  3Comments  ยท  Source: hashicorp/terraform-provider-aws

Hiya,

CloudFormation has support for custom resources in templates - functionality that is a limited subset of what Terraform supports in the form of providers' data sources. One of the supported receivers for CFN custom resources is a Lambda function. When specified, CloudFormation _invokes_ the Lambda function and passes properties to the function as a request body. The response body is then available to the rest of the template as attributes.

It would be great to have similar functionality in Terraform. We can almost do this using the External Data Source escape hatch, by doing something like:

data "external" "example" {
  program = ["bash", "-c", "xargs -0 -I '{}' aws lambda invoke --function-name LAMBDA_ARN --payload '{}' /dev/stdout"]

  query = {
    id = "abc123"
  }
}

But this is fiddly to type, requires the AWS CLI to be installed and credentials are a bit of a pain to manage. It would be great if this had first-class support.

What do you think? If there's general approval of the idea - or it needs some tweaks - I'd be happy to submit a PR.

new-data-source serviclambda

Most helpful comment

@bflad this still does not do what "Custom Resources" do ...
does Terraform run aws_lambda_invocation only at creation ?
does Terraform pass Action Type to the Lambda like "CREATE", "UPDATE", "DELETE" so the lambda knows what to do? or can I use something like ${terraform.actiontype or any other variable that holds the current terraform action ("apply", "destroy" ...) ?

All 3 comments

A new aws_lambda_invocation data source was previously released in version 1.19.0 of the AWS provider and has been available in all releases since. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

@bflad this still does not do what "Custom Resources" do ...
does Terraform run aws_lambda_invocation only at creation ?
does Terraform pass Action Type to the Lambda like "CREATE", "UPDATE", "DELETE" so the lambda knows what to do? or can I use something like ${terraform.actiontype or any other variable that holds the current terraform action ("apply", "destroy" ...) ?

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 feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

Was this page helpful?
0 / 5 - 0 ratings