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
Option 1
Create:
resource "aws_lambda_invocation" "invoke"
Option 2
Update:
data "aws_lambda_invocation" "invoke"
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
}
No references
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.