Terraform-provider-aws: execute aws_lambda_invocation only once

Created on 5 Jun 2018  路  7Comments  路  Source: hashicorp/terraform-provider-aws

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 "me too" comments, 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

After creating an aws_lambda_function I want directly to trigger it for a first initialization. Currently I'm doing this using a provisioner and and some shell commands, which are invoking the the lambda with the AWS CLI.

I've seen there is now an aws_lambda_invocation data source, which does exactly the same like my provisioner. In my case, the disadvantage of a data source is, that the lambda is executed every time I do
a "terraform plan" or "terraform apply", which is not needed and increases the execution time (and AWS costs).

It would be nice to be able to trigger a lambda function only once

New or Affected Resource(s)

Option 1
Create:

resource "aws_lambda_invocation" "invoke"

Option 2
Update:

data "aws_lambda_invocation" "invoke"

Potential Terraform Configuration

I would prefer the first option, to create a new ressource type. It would look like the data source:

resource "aws_lambda_invocation" "example" {
  function_name = "${aws_lambda_function.lambda_function_test.function_name}"

  input = <<JSON
  {
    "key1": "value1",
    "key2": "value2"
  }
  JSON
}

As an alternative an optional attribute in the data source can manage the behavior:

data "aws_lambda_invocation" "example" {
  function_name = "${aws_lambda_function.lambda_function_test.function_name}"
  trigger_once  = true

  input = <<JSON
  {
    "key1": "value1",
    "key2": "value2"
  }
  JSON
}

References

No references

enhancement serviclambda

All 7 comments

One further idea on that:
An additional attribute in the resource like e.g. "hash" (stored only in the state) can be you used for forcing a "recreation" of the invocation resource, which will trigger the invocation call each time, a certain depending resource was changed

Is this becoming kinda issue for us. Is there a way to not run data during apply ?

Can I work on this? I think first option should be the solution.

Temporarily, I created this community provider. https://registry.terraform.io/providers/dedunumax/awslambda/latest But you might need Terraform 0.13 to try that.

As a workaround, I did the following:

# run lambda during apply only
data aws_lambda_invocation run {
  function_name = "my-lambda"

  input = <<JSON
{
  "APPLY_DATE": "${formatdate("DD MMM YYYY hh:mm ZZZ", timestamp())}",
 ...
}
JSON
}

Any news about this issue ?
What's the recommended way until it's fixed ?

I have made a PR to fix this. But it is not merged yet.

Was this page helpful?
0 / 5 - 0 ratings