Resource: awsLambdaInvocation
Use this resource to invoke a lambda function. The lambda function is invoked with the RequestResponse invocation type.
\~> NOTE: This resource only invokes the function when the arguments call for a create or update. In other words, after an initial invocation on apply, if the arguments do not change, a subsequent apply does not invoke the function again. To dynamically invoke the function, see the triggers
example below. To always invoke a function on each apply, see the awsLambdaInvocation
data source.
\~> NOTE: If you get a kmsAccessDeniedException:LambdaWasUnableToDecryptTheEnvironmentVariablesBecauseKmsAccessWasDenied
error when invoking an awsLambdaFunction
with environment variables, the IAM role associated with the function may have been deleted and recreated after the function was created. You can fix the problem two ways: 1) updating the function's role to another role and then updating it back again to the recreated role, or 2) by using Terraform to taint
the function and apply
your configuration again to recreate the function. (When you create a function, Lambda grants permissions on the KMS key to the function's IAM role. If the IAM role is recreated, the grant is no longer valid. Changing the function's role or recreating the function causes Lambda to update the grant.)
Example Usage
Basic Example
import * as cdktf from "cdktf";
/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as aws from "./.gen/providers/aws";
const awsLambdaInvocationExample = new aws.lambdaInvocation.LambdaInvocation(
this,
"example",
{
functionName: "${aws_lambda_function.lambda_function_test.function_name}",
input: '${jsonencode({\n key1 = "value1"\n key2 = "value2"\n })}',
}
);
new cdktf.TerraformOutput(this, "result_entry", {
value: `\${jsondecode(${awsLambdaInvocationExample.result})["key1"]}`,
});
Dynamic Invocation Example Using Triggers
/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as aws from "./.gen/providers/aws";
new aws.lambdaInvocation.LambdaInvocation(this, "example", {
functionName: "${aws_lambda_function.lambda_function_test.function_name}",
input: '${jsonencode({\n key1 = "value1"\n key2 = "value2"\n })}',
triggers: {
redeployment:
"${sha1(jsonencode([\n aws_lambda_function.example.environment\n ]))}",
},
});
Argument Reference
The following arguments are required:
functionName
- (Required) Name of the lambda function.input
- (Required) JSON payload to the lambda function.
The following arguments are optional:
qualifier
- (Optional) Qualifier (i.e., version) of the lambda function. Defaults to$latest
.triggers
- (Optional) Map of arbitrary keys and values that, when changed, will trigger a re-invocation. To force a re-invocation without changing these keys/values, use theterraformTaint
command.
Attributes Reference
In addition to all arguments above, the following attributes are exported:
result
- String result of the lambda function invocation.