Invocation class
Manages an AWS Lambda Function invocation. Use this resource to invoke a Lambda function with the RequestResponse invocation type.
> Note: By default this resource only invokes the function when the arguments call for a create or replace. 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 aws.lambda.Invocation data source. To invoke the Lambda function when the Pulumi resource is updated and deleted, see the CRUD Lifecycle Management example below.
> Note: If you get a KMSAccessDeniedException: Lambda was unable to decrypt the environment variables because KMS access was denied error when invoking a Lambda function 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. (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 Invocation
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as std from "@pulumi/std";
// Lambda function to invoke
const example = new aws.lambda.Function("example", {
code: new pulumi.asset.FileArchive("function.zip"),
name: "data_processor",
role: lambdaRole.arn,
handler: "index.handler",
runtime: aws.lambda.Runtime.Python3d12,
});
// Invoke the function once during resource creation
const exampleInvocation = new aws.lambda.Invocation("example", {
functionName: example.name,
input: JSON.stringify({
operation: "initialize",
config: {
environment: "production",
debug: false,
},
}),
});
export const initializationResult = std.jsondecodeOutput({
input: exampleInvocation.result,
}).apply(invoke => invoke.result?.status);
import pulumi
import json
import pulumi_aws as aws
import pulumi_std as std
# Lambda function to invoke
example = aws.lambda_.Function("example",
code=pulumi.FileArchive("function.zip"),
name="data_processor",
role=lambda_role["arn"],
handler="index.handler",
runtime=aws.lambda_.Runtime.PYTHON3D12)
# Invoke the function once during resource creation
example_invocation = aws.lambda_.Invocation("example",
function_name=example.name,
input=json.dumps({
"operation": "initialize",
"config": {
"environment": "production",
"debug": False,
},
}))
pulumi.export("initializationResult", std.jsondecode_output(input=example_invocation.result).result["status"])
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using Pulumi;
using Aws = Pulumi.Aws;
using Std = Pulumi.Std;
return await Deployment.RunAsync(() =>
{
// Lambda function to invoke
var example = new Aws.Lambda.Function("example", new()
{
Code = new FileArchive("function.zip"),
Name = "data_processor",
Role = lambdaRole.Arn,
Handler = "index.handler",
Runtime = Aws.Lambda.Runtime.Python3d12,
});
// Invoke the function once during resource creation
var exampleInvocation = new Aws.Lambda.Invocation("example", new()
{
FunctionName = example.Name,
Input = JsonSerializer.Serialize(new Dictionary<string, object?>
{
["operation"] = "initialize",
["config"] = new Dictionary<string, object?>
{
["environment"] = "production",
["debug"] = false,
},
}),
});
return new Dictionary<string, object?>
{
["initializationResult"] = Std.Jsondecode.Invoke(new()
{
Input = exampleInvocation.Result,
}).Apply(invoke => invoke.Result?.Status),
};
});
package main
import (
"encoding/json"
"github.com/pulumi/pulumi-aws/sdk/v7/go/aws/lambda"
"github.com/pulumi/pulumi-std/sdk/go/std"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
// Lambda function to invoke
example, err := lambda.NewFunction(ctx, "example", &lambda.FunctionArgs{
Code: pulumi.NewFileArchive("function.zip"),
Name: pulumi.String("data_processor"),
Role: pulumi.Any(lambdaRole.Arn),
Handler: pulumi.String("index.handler"),
Runtime: pulumi.String(lambda.RuntimePython3d12),
})
if err != nil {
return err
}
tmpJSON0, err := json.Marshal(map[string]interface{}{
"operation": "initialize",
"config": map[string]interface{}{
"environment": "production",
"debug": false,
},
})
if err != nil {
return err
}
json0 := string(tmpJSON0)
// Invoke the function once during resource creation
exampleInvocation, err := lambda.NewInvocation(ctx, "example", &lambda.InvocationArgs{
FunctionName: example.Name,
Input: pulumi.String(json0),
})
if err != nil {
return err
}
ctx.Export("initializationResult", std.JsondecodeOutput(ctx, std.JsondecodeOutputArgs{
Input: exampleInvocation.Result,
}, nil).ApplyT(func(invoke std.JsondecodeResult) (*interface{}, error) {
val := invoke.Result.Status
return &val, nil
}).(pulumi.Interface{}PtrOutput))
return nil
})
}
pulumi {
required_providers {
aws = {
source = "pulumi/aws"
}
std = {
source = "pulumi/std"
}
}
}
# Lambda function to invoke
resource "aws_lambda_function" "example" {
code = fileArchive("function.zip")
name = "data_processor"
role = lambdaRole.arn
handler = "index.handler"
runtime = "python3.12"
}
# Invoke the function once during resource creation
resource "aws_lambda_invocation" "example" {
function_name = aws_lambda_function.example.name
input = jsonencode({
"operation" = "initialize"
"config" = {
"environment" = "production"
"debug" = false
}
})
}
# Use the result in other resources
output "initializationResult" {
value = jsondecode(aws_lambda_invocation.example.result)["status"]
}
package generated_program;
import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.aws.lambda.Function;
import com.pulumi.aws.lambda.FunctionArgs;
import com.pulumi.aws.lambda.Invocation;
import com.pulumi.aws.lambda.InvocationArgs;
import com.pulumi.std.StdFunctions;
import com.pulumi.std.inputs.JsondecodeArgs;
import com.pulumi.asset.FileArchive;
import static com.pulumi.codegen.internal.Serialization.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
public class App {
public static void main(String[] args) {
Pulumi.run(App::stack);
}
public static void stack(Context ctx) {
// Lambda function to invoke
var example = new Function("example", FunctionArgs.builder()
.code(new FileArchive("function.zip"))
.name("data_processor")
.role(lambdaRole.arn())
.handler("index.handler")
.runtime("python3.12")
.build());
// Invoke the function once during resource creation
var exampleInvocation = new Invocation("exampleInvocation", InvocationArgs.builder()
.functionName(example.name())
.input(serializeJson(
jsonObject(
jsonProperty("operation", "initialize"),
jsonProperty("config", jsonObject(
jsonProperty("environment", "production"),
jsonProperty("debug", false)
))
)))
.build());
ctx.export("initializationResult", StdFunctions.jsondecode(JsondecodeArgs.builder()
.input(exampleInvocation.result())
.build()).applyValue(_invoke -> _invoke.result().status()));
}
}
resources:
# Lambda function to invoke
example:
type: aws:lambda:Function
properties:
code:
fn::fileArchive: function.zip
name: data_processor
role: ${lambdaRole.arn}
handler: index.handler
runtime: python3.12
# Invoke the function once during resource creation
exampleInvocation:
type: aws:lambda:Invocation
name: example
properties:
functionName: ${example.name}
input:
fn::toJSON:
operation: initialize
config:
environment: production
debug: false
outputs:
# Use the result in other resources
initializationResult:
fn::invoke:
function: std:jsondecode
arguments:
input: ${exampleInvocation.result}
return: result.status
Dynamic Invocation with Triggers
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as std from "@pulumi/std";
const example = new aws.lambda.Invocation("example", {
functionName: exampleAwsLambdaFunction.functionName,
triggers: {
function_version: exampleAwsLambdaFunction.version,
config_hash: std.sha256Output({
input: JSON.stringify({
environment: environment,
timestamp: std.timestamp({}).then(invoke => invoke.result),
}),
}).result,
},
input: JSON.stringify({
operation: "process_data",
environment: environment,
batch_id: batchId.result,
}),
});
import pulumi
import json
import pulumi_aws as aws
import pulumi_std as std
example = aws.lambda_.Invocation("example",
function_name=example_aws_lambda_function["functionName"],
triggers={
"function_version": example_aws_lambda_function["version"],
"config_hash": std.sha256_output(input=json.dumps({
"environment": environment,
"timestamp": std.timestamp().result,
})).result,
},
input=json.dumps({
"operation": "process_data",
"environment": environment,
"batch_id": batch_id["result"],
}))
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using Pulumi;
using Aws = Pulumi.Aws;
using Std = Pulumi.Std;
return await Deployment.RunAsync(() =>
{
var example = new Aws.Lambda.Invocation("example", new()
{
FunctionName = exampleAwsLambdaFunction.FunctionName,
Triggers =
{
{ "function_version", exampleAwsLambdaFunction.Version },
{ "config_hash", Std.Sha256.Invoke(new()
{
Input = JsonSerializer.Serialize(new Dictionary<string, object?>
{
["environment"] = environment,
["timestamp"] = Std.Timestamp.Invoke().Apply(invoke => invoke.Result),
}),
}).Apply(invoke => invoke.Result) },
},
Input = JsonSerializer.Serialize(new Dictionary<string, object?>
{
["operation"] = "process_data",
["environment"] = environment,
["batch_id"] = batchId.Result,
}),
});
});
package main
import (
"encoding/json"
"github.com/pulumi/pulumi-aws/sdk/v7/go/aws/lambda"
"github.com/pulumi/pulumi-std/sdk/go/std"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
tmpJSON0, err := json.Marshal(map[string]interface{}{
"environment": environment,
"timestamp": std.Timestamp(ctx, &std.TimestampArgs{}, nil).Result,
})
if err != nil {
return err
}
json0 := string(tmpJSON0)
tmpJSON1, err := json.Marshal(map[string]interface{}{
"operation": "process_data",
"environment": environment,
"batch_id": batchId.Result,
})
if err != nil {
return err
}
json1 := string(tmpJSON1)
_, err = lambda.NewInvocation(ctx, "example", &lambda.InvocationArgs{
FunctionName: pulumi.Any(exampleAwsLambdaFunction.FunctionName),
Triggers: pulumi.StringMap{
"function_version": pulumi.Any(exampleAwsLambdaFunction.Version),
"config_hash": std.Sha256Output(ctx, std.Sha256OutputArgs{
Input: json0,
}, nil).Result(),
},
Input: pulumi.String(json1),
})
if err != nil {
return err
}
return nil
})
}
pulumi {
required_providers {
aws = {
source = "pulumi/aws"
}
std = {
source = "pulumi/std"
}
}
}
resource "aws_lambda_invocation" "example" {
function_name = exampleAwsLambdaFunction.functionName
triggers = {
"function_version" = exampleAwsLambdaFunction.version
"config_hash" = sha256(jsonencode({
"environment" = environment
"timestamp" = timestamp()
}))
}
input = jsonencode({
"operation" = "process_data"
"environment" = environment
"batch_id" = batchId.result
})
}
package generated_program;
import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.aws.lambda.Invocation;
import com.pulumi.aws.lambda.InvocationArgs;
import com.pulumi.std.StdFunctions;
import com.pulumi.std.inputs.TimestampArgs;
import com.pulumi.std.inputs.Sha256Args;
import static com.pulumi.codegen.internal.Serialization.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
public class App {
public static void main(String[] args) {
Pulumi.run(App::stack);
}
public static void stack(Context ctx) {
var example = new Invocation("example", InvocationArgs.builder()
.functionName(exampleAwsLambdaFunction.functionName())
.triggers(Map.ofEntries(
Map.entry("function_version", exampleAwsLambdaFunction.version()),
Map.entry("config_hash", StdFunctions.sha256(Sha256Args.builder()
.input(serializeJson(
jsonObject(
jsonProperty("environment", environment),
jsonProperty("timestamp", StdFunctions.timestamp(TimestampArgs.builder()
.build()).result())
)))
.build()).applyValue(_invoke -> _invoke.result()))
))
.input(serializeJson(
jsonObject(
jsonProperty("operation", "process_data"),
jsonProperty("environment", environment),
jsonProperty("batch_id", batchId.result())
)))
.build());
}
}
resources:
example:
type: aws:lambda:Invocation
properties:
functionName: ${exampleAwsLambdaFunction.functionName}
triggers:
function_version: ${exampleAwsLambdaFunction.version}
config_hash:
fn::invoke:
function: std:sha256
arguments:
input:
fn::toJSON:
environment: ${environment}
timestamp:
fn::invoke:
function: std:timestamp
arguments: {}
return: result
return: result
input:
fn::toJSON:
operation: process_data
environment: ${environment}
batch_id: ${batchId.result}
CRUD Lifecycle Management
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
const example = new aws.lambda.Invocation("example", {
functionName: exampleAwsLambdaFunction.functionName,
input: JSON.stringify({
resource_name: "database_setup",
database_url: exampleAwsDbInstance.endpoint,
credentials: {
username: dbUsername,
password: dbPassword,
},
}),
lifecycleScope: "CRUD",
});
import pulumi
import json
import pulumi_aws as aws
example = aws.lambda_.Invocation("example",
function_name=example_aws_lambda_function["functionName"],
input=json.dumps({
"resource_name": "database_setup",
"database_url": example_aws_db_instance["endpoint"],
"credentials": {
"username": db_username,
"password": db_password,
},
}),
lifecycle_scope="CRUD")
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using Pulumi;
using Aws = Pulumi.Aws;
return await Deployment.RunAsync(() =>
{
var example = new Aws.Lambda.Invocation("example", new()
{
FunctionName = exampleAwsLambdaFunction.FunctionName,
Input = JsonSerializer.Serialize(new Dictionary<string, object?>
{
["resource_name"] = "database_setup",
["database_url"] = exampleAwsDbInstance.Endpoint,
["credentials"] = new Dictionary<string, object?>
{
["username"] = dbUsername,
["password"] = dbPassword,
},
}),
LifecycleScope = "CRUD",
});
});
package main
import (
"encoding/json"
"github.com/pulumi/pulumi-aws/sdk/v7/go/aws/lambda"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
tmpJSON0, err := json.Marshal(map[string]interface{}{
"resource_name": "database_setup",
"database_url": exampleAwsDbInstance.Endpoint,
"credentials": map[string]interface{}{
"username": dbUsername,
"password": dbPassword,
},
})
if err != nil {
return err
}
json0 := string(tmpJSON0)
_, err = lambda.NewInvocation(ctx, "example", &lambda.InvocationArgs{
FunctionName: pulumi.Any(exampleAwsLambdaFunction.FunctionName),
Input: pulumi.String(json0),
LifecycleScope: pulumi.String("CRUD"),
})
if err != nil {
return err
}
return nil
})
}
pulumi {
required_providers {
aws = {
source = "pulumi/aws"
}
}
}
resource "aws_lambda_invocation" "example" {
function_name = exampleAwsLambdaFunction.functionName
input = jsonencode({
"resource_name" = "database_setup"
"database_url" = exampleAwsDbInstance.endpoint
"credentials" = {
"username" = dbUsername
"password" = dbPassword
}
})
lifecycle_scope = "CRUD"
}
package generated_program;
import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.aws.lambda.Invocation;
import com.pulumi.aws.lambda.InvocationArgs;
import static com.pulumi.codegen.internal.Serialization.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
public class App {
public static void main(String[] args) {
Pulumi.run(App::stack);
}
public static void stack(Context ctx) {
var example = new Invocation("example", InvocationArgs.builder()
.functionName(exampleAwsLambdaFunction.functionName())
.input(serializeJson(
jsonObject(
jsonProperty("resource_name", "database_setup"),
jsonProperty("database_url", exampleAwsDbInstance.endpoint()),
jsonProperty("credentials", jsonObject(
jsonProperty("username", dbUsername),
jsonProperty("password", dbPassword)
))
)))
.lifecycleScope("CRUD")
.build());
}
}
resources:
example:
type: aws:lambda:Invocation
properties:
functionName: ${exampleAwsLambdaFunction.functionName}
input:
fn::toJSON:
resource_name: database_setup
database_url: ${exampleAwsDbInstance.endpoint}
credentials:
username: ${dbUsername}
password: ${dbPassword}
lifecycleScope: CRUD
> Note: lifecycleScope = "CRUD" will inject a key tf in the input event to pass lifecycle information! This allows the Lambda function to handle different lifecycle transitions uniquely. If you need to use a key tf in your own input JSON, the default key name can be overridden with the pulumiKey argument.
The lifecycle key gets added with subkeys:
action- Action Pulumi performs on the resource. Values arecreate,update, ordelete.prevInput- Input JSON payload from the previous invocation. This can be used to handle update and delete events.
When the resource from the CRUD example above is created, the Lambda will receive the following JSON payload:
{
"resource_name": "database_setup",
"database_url": "mydb.cluster-xyz.us-west-2.rds.amazonaws.com:5432",
"credentials": {
"username": "admin",
"password": "secret123"
},
"tf": {
"action": "create",
"prev_input": null
}
}
If the databaseUrl changes, the Lambda will be invoked again with:
{
"resource_name": "database_setup",
"database_url": "mydb-new.cluster-abc.us-west-2.rds.amazonaws.com:5432",
"credentials": {
"username": "admin",
"password": "secret123"
},
"tf": {
"action": "update",
"prev_input": {
"resource_name": "database_setup",
"database_url": "mydb.cluster-xyz.us-west-2.rds.amazonaws.com:5432",
"credentials": {
"username": "admin",
"password": "secret123"
}
}
}
}
When the invocation resource is removed, the final invocation will have:
{
"resource_name": "database_setup",
"database_url": "mydb-new.cluster-abc.us-west-2.rds.amazonaws.com:5432",
"credentials": {
"username": "admin",
"password": "secret123"
},
"tf": {
"action": "delete",
"prev_input": {
"resource_name": "database_setup",
"database_url": "mydb-new.cluster-abc.us-west-2.rds.amazonaws.com:5432",
"credentials": {
"username": "admin",
"password": "secret123"
}
}
}
}
Import
Using pulumi import, import Lambda Invocation using the function_name,qualifier,result_hash. For example:
$ pulumi import aws:lambda/invocation:Invocation test_lambda my_test_lambda_function,$LATEST,b326b5062b2f0e69046810717534cb09
Because it is not possible to retrieve previous invocations, during the next update Pulumi will update the resource calling again the function.
To compute the resultHash, it is necessary to hash it with the standard md5 hash function.
Constructors
- Invocation(String name, {InvocationArgs? args, CustomResourceOptions? options})
-
Creates a new Invocation.
nameThe Pulumi resource name.argsArguments used to configure this Invocation. The set of arguments for Invocation.optionsResource options controlling this resource's behavior. - Invocation.reference(String urn)
- Creates a typed reference to an existing Invocation resource.
Properties
-
childResources
→ Set<
Resource> -
finalinherited
-
completionSources
↔ Map<
String, IOutputCompletionSource> -
latefinalinherited
-
functionName
↔ Output<
String> -
Name of the Lambda function.
latefinal
- hashCode → int
-
The hash code for this object.
no setterinherited
-
id
↔ Output<
String> -
getter/setter pairinherited
-
input
↔ Output<
String> -
JSON payload to the Lambda function.
latefinal
- isCustom → bool
-
Returns whether this resource is provider-managed.
no setterinherited
- isProtected → bool
-
Returns whether this resource is protected from deletion.
no setterinherited
- isRemote → bool
-
Whether this resource is registered as remote.
no setterinherited
- isResourceReference → bool
-
Whether this instance represents a resource value returned over RPC.
finalinherited
-
lifecycleScope
↔ Output<
String?> -
Lifecycle scope of the resource to manage. Valid values are
CREATE_ONLYandCRUD. Defaults toCREATE_ONLY.CREATE_ONLYwill invoke the function only on creation or replacement.CRUDwill invoke the function on each lifecycle event, and augment the input JSON payload with additional lifecycle information.latefinal -
qualifier
↔ Output<
String?> -
Qualifier (i.e., version) of the Lambda function. Defaults to
$LATEST.latefinal -
region
↔ Output<
String> -
Region where this resource will be managed. Defaults to the Region set in the provider configuration.
latefinal
-
resourceTransforms
→ List<
ResourceTransform> -
Inherited/explicit async transforms.
no setterinherited
-
result
↔ Output<
String> -
String result of the Lambda function invocation.
latefinal
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
tenantId
↔ Output<
String?> -
Tenant Id to serve invocations from specified tenant.
latefinal
-
terraformKey
↔ Output<
String?> -
latefinal
-
transformations
→ List<
ResourceTransformation> -
Inherited/explicit legacy transformations.
no setterinherited
-
triggers
↔ Output<
Map< String, String> ?> -
Map of arbitrary keys and values that, when changed, will trigger a re-invocation.
latefinal
-
urn
↔ Output<
String> -
latefinalinherited
Methods
-
failId(
Object error) → void -
Completes this resource ID with an error when registration fails.
inherited
-
failOutputs(
Object error) → void -
Completes all output properties with
error.inherited -
failUrn(
Object error) → void -
Completes this resource URN with an error when registration fails.
inherited
-
getProvider(
String moduleMember) → ProviderResource? -
Returns provider for
moduleMember's package, if configured.inherited -
getResourceName(
) → String -
Returns this resource's logical name.
inherited
-
getResourceType(
) → String -
Returns this resource's Pulumi type token.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
registerOutput<
T> (String propertyName, {Object? decoder(Object?)?, bool isSecret = false}) → Output< T> -
Registers a dynamic output property for this resource.
inherited
-
resolveId(
String? value, {required bool isKnown}) → void -
Resolves the provider-assigned ID for this resource.
inherited
-
resolveOutputs(
Struct outputs) → void -
Resolves all output properties from a monitor response payload.
inherited
-
resolveUrn(
String value) → void -
Resolves this resource's URN once assigned by the engine.
inherited
-
serializeProperties(
Map< String, dynamic> properties) → Future<Struct> -
Serializes resource properties for RPC transmission.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
get(
String name, Input< String> id, {InvocationState? state, CustomResourceOptions? options}) → Invocation -
Gets an existing Invocation resource's state with the given
nameandid.