Class Invocation


  • public class Invocation
    extends com.pulumi.resources.CustomResource
    Manages an AWS Lambda Function invocation. Use this resource to invoke a Lambda function with the [RequestResponse](https://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html#API_Invoke_RequestSyntax) 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
     
     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()));
         }
     }
     
     
    ### Dynamic Invocation with Triggers
     
     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());
     
         }
     }
     
     
    ### CRUD Lifecycle Management
     
     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());
     
         }
     }
     
     
    > **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 are `create`, `update`, or `delete`. * `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: If the `databaseUrl` changes, the Lambda will be invoked again with: When the invocation resource is removed, the final invocation will have: ## Import Using `pulumi import`, import Lambda Invocation using the `function_name,qualifier,result_hash`. For example: ```sh $ 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.
    • Nested Class Summary

      • Nested classes/interfaces inherited from class com.pulumi.resources.CustomResource

        com.pulumi.resources.CustomResource.CustomResourceInternal
      • Nested classes/interfaces inherited from class com.pulumi.resources.Resource

        com.pulumi.resources.Resource.LazyField<T extends java.lang.Object>, com.pulumi.resources.Resource.LazyFields, com.pulumi.resources.Resource.ResourceInternal
    • Field Summary

      • Fields inherited from class com.pulumi.resources.Resource

        childResources, remote
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      com.pulumi.core.Output<java.lang.String> functionName()  
      static Invocation get​(java.lang.String name, com.pulumi.core.Output<java.lang.String> id, InvocationState state, com.pulumi.resources.CustomResourceOptions options)
      Get an existing Host resource's state with the given name, ID, and optional extra properties used to qualify the lookup.
      com.pulumi.core.Output<java.lang.String> input()  
      com.pulumi.core.Output<java.util.Optional<java.lang.String>> lifecycleScope()  
      com.pulumi.core.Output<java.util.Optional<java.lang.String>> qualifier()  
      com.pulumi.core.Output<java.lang.String> region()  
      com.pulumi.core.Output<java.lang.String> result()  
      com.pulumi.core.Output<java.util.Optional<java.lang.String>> tenantId()  
      com.pulumi.core.Output<java.util.Optional<java.lang.String>> terraformKey()  
      com.pulumi.core.Output<java.util.Optional<java.util.Map<java.lang.String,​java.lang.String>>> triggers()  
      • Methods inherited from class com.pulumi.resources.CustomResource

        getId, id, idFuture
      • Methods inherited from class com.pulumi.resources.Resource

        getChildResources, getResourceName, getResourceType, getUrn, pulumiChildResources, pulumiResourceName, pulumiResourceType, urn
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Invocation

        public Invocation​(java.lang.String name)
        Parameters:
        name - The _unique_ name of the resulting resource.
      • Invocation

        public Invocation​(java.lang.String name,
                          InvocationArgs args)
        Parameters:
        name - The _unique_ name of the resulting resource.
        args - The arguments to use to populate this resource's properties.
      • Invocation

        public Invocation​(java.lang.String name,
                          InvocationArgs args,
                          @Nullable
                          com.pulumi.resources.CustomResourceOptions options)
        Parameters:
        name - The _unique_ name of the resulting resource.
        args - The arguments to use to populate this resource's properties.
        options - A bag of options that control this resource's behavior.
    • Method Detail

      • functionName

        public com.pulumi.core.Output<java.lang.String> functionName()
        Returns:
        Name of the Lambda function.
      • input

        public com.pulumi.core.Output<java.lang.String> input()
        Returns:
        JSON payload to the Lambda function. The following arguments are optional:
      • lifecycleScope

        public com.pulumi.core.Output<java.util.Optional<java.lang.String>> lifecycleScope()
        Returns:
        Lifecycle scope of the resource to manage. Valid values are `CREATE_ONLY` and `CRUD`. Defaults to `CREATE_ONLY`. `CREATE_ONLY` will invoke the function only on creation or replacement. `CRUD` will invoke the function on each lifecycle event, and augment the input JSON payload with additional lifecycle information.
      • qualifier

        public com.pulumi.core.Output<java.util.Optional<java.lang.String>> qualifier()
        Returns:
        Qualifier (i.e., version) of the Lambda function. Defaults to `$LATEST`.
      • region

        public com.pulumi.core.Output<java.lang.String> region()
        Returns:
        Region where this resource will be [managed](https://docs.aws.amazon.com/general/latest/gr/rande.html#regional-endpoints). Defaults to the Region set in the provider configuration.
      • result

        public com.pulumi.core.Output<java.lang.String> result()
        Returns:
        String result of the Lambda function invocation.
      • tenantId

        public com.pulumi.core.Output<java.util.Optional<java.lang.String>> tenantId()
        Returns:
        Tenant ID to serve invocations from specified tenant.
      • terraformKey

        public com.pulumi.core.Output<java.util.Optional<java.lang.String>> terraformKey()
      • triggers

        public com.pulumi.core.Output<java.util.Optional<java.util.Map<java.lang.String,​java.lang.String>>> triggers()
        Returns:
        Map of arbitrary keys and values that, when changed, will trigger a re-invocation.
      • get

        public static Invocation get​(java.lang.String name,
                                     com.pulumi.core.Output<java.lang.String> id,
                                     @Nullable
                                     InvocationState state,
                                     @Nullable
                                     com.pulumi.resources.CustomResourceOptions options)
        Get an existing Host resource's state with the given name, ID, and optional extra properties used to qualify the lookup.
        Parameters:
        name - The _unique_ name of the resulting resource.
        id - The _unique_ provider ID of the resource to lookup.
        state -
        options - Optional settings to control the behavior of the CustomResource.