Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: ability to specify JMESPath custom functions for Idempotency #2368

Closed
1 of 2 tasks
dreamorosi opened this issue Apr 16, 2024 · 12 comments · Fixed by #3150
Closed
1 of 2 tasks

Feature request: ability to specify JMESPath custom functions for Idempotency #2368

dreamorosi opened this issue Apr 16, 2024 · 12 comments · Fixed by #3150
Assignees
Labels
completed This item is complete and has been merged/shipped feature-request This item refers to a feature request for an existing or new utility idempotency This item relates to the Idempotency Utility

Comments

@dreamorosi
Copy link
Contributor

Use case

When working with the JMESPath expressions, the Idempotency utility uses some custom functions (i.e. powertools_json()) that extend the JMESPath built-in functions. This allows customers to work with some complex types that are common when working with AWS payloads.

The underlying JMESPath utility used by Idempotency however allows for further customization by allowing customers to set additional custom functions in addition or instead of the Powertools-provided ones.

It would be great if customers were able to pass their own subclass of Functions or PowertoolsFunctions when using the Idempotency utility.

Solution/User Experience

Currently the Idempotency utility creates its own instance of the PowertoolsFunctions class when instantiating the IdempotencyConfig class. This allows the various components of the utility to reuse it across the implementation.

The setting could be exposed to customers by adding a new option to the IdempotencyConfig class/object:

import { makeIdempotent, IdempotencyConfig } from '@aws-lambda-powertools/idempotency';
import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb';

const persistenceStore = new DynamoDBPersistenceLayer({
  tableName: 'idempotencyTableName',
});

class MyFancyFunctions extends PowertoolsFunctions {
  @Functions.signature({
    argumentsSpecs: [['string']],
  })
  public funcMyFancyFunction(value: string): JSONValue {
    return JSON.parse(value);
  }
}

export const handler = makeIdempotent(async () => true, {
  persistenceStore,
  config: new IdempotencyConfig({
    eventKeyJmespath: 'my_fancy_function(body).["user", "productId"]',
    jmesPathOptions: new MyFancyFunctions(), // passed as part of the idempotency configs
  }),
});

Alternative solutions

No response

Acknowledgment

Future readers

Please react with 👍 and your use case to help us understand customer demand.

@dreamorosi dreamorosi added idempotency This item relates to the Idempotency Utility feature-request This item refers to a feature request for an existing or new utility discussing The issue needs to be discussed, elaborated, or refined labels Apr 16, 2024
@leandrodamascena
Copy link
Contributor

Hello @dreamorosi! I think this might cover some user cases where the customer might need to decode a compressed string and use as the idempotent key, or want to add some information to the jmespath_key, we never know all the use cases. We support the customer in adding additional functions in Python.

from dataclasses import dataclass

from aws_lambda_powertools.utilities.idempotency import (
    DynamoDBPersistenceLayer,
    IdempotencyConfig,
    idempotent_function,
)
from aws_lambda_powertools.utilities.typing import LambdaContext

from jmespath.functions import signature

from aws_lambda_powertools.utilities.jmespath_utils import (
    PowertoolsFunctions
)

class CustomFunctions(PowertoolsFunctions):
    # only decode if value is a string
    # see supported data types: https://jmespath.org/specification.html#built-in-functions
    @signature({"types": ["string"]})
    def _func_custom_transform(self, payload: str):
        print("Custom function invoked")
        return "customfunction"


custom_jmespath_options = {"custom_functions": CustomFunctions()}

dynamodb = DynamoDBPersistenceLayer(table_name="ddbidempotency")
config = IdempotencyConfig(event_key_jmespath="custom_transform(order_id)", jmespath_options=custom_jmespath_options)  # see Choosing a payload subset section

@dataclass
class OrderItem:
    sku: str
    description: str

@dataclass
class Order:
    item: OrderItem
    order_id: str

@idempotent_function(data_keyword_argument="order", config=config, persistence_store=dynamodb)
def process_order(order: Order):
    return f"processed order {order.order_id}"

def lambda_handler(event: dict, context: LambdaContext):
    config.register_lambda_context(context)  # see Lambda timeouts section
    order_item = OrderItem(sku="fake", description="sample")
    order = Order(item=order_item, order_id="testid")

    # `order` parameter must be called as a keyword argument to work
    process_order(order=order)

Thanks

@dreamorosi
Copy link
Contributor Author

Thank you Leo, we'll add this to the backlog.

I'm also adding the help-wanted label to signal that this issue is open for contribution. If anyone wants to pick this up please leave a comment and feel free to ask any question.

@dreamorosi dreamorosi added help-wanted We would really appreciate some support from community for this one confirmed The scope is clear, ready for implementation and removed discussing The issue needs to be discussed, elaborated, or refined labels Apr 17, 2024
@arnabrahman
Copy link
Contributor

@dreamorosi please assign me on this.

@github-actions github-actions bot added pending-release This item has been merged and will be released soon and removed help-wanted We would really appreciate some support from community for this one confirmed The scope is clear, ready for implementation labels Oct 4, 2024
@pratyaksh123
Copy link

Hi, do you have any timeline on when this can be released? I was waiting for such a feature to use custom function for idempotencyKey

@arnabrahman
Copy link
Contributor

@pratyaksh123 There is a planned release today, so it should be part of that release. But not 100% sure, though.

@pratyaksh123
Copy link

thanks, for the PR @arnabrahman

@dreamorosi
Copy link
Contributor Author

Hey hi, yes we're planning on releasing in the next few hours, unless something unexpected comes up during the pre-release checks.

Once it's released there will be a comment under this issue.

@pratyaksh123
Copy link

Is there a way I can use two parameters in a function call for idempotency ? Like on my post request I want both url and payload to be considered for idempotency, how can I do that? Because right now I can only see one dataIndexArgument that can be specified

@dreamorosi
Copy link
Contributor Author

@pratyaksh123 Can you share an example of the payload and one of how the function signature looks like?

@pratyaksh123
Copy link

I can't share it fully as its internal company code, but its like this

post(url, payload, config):
...

Here the issue I am facing is that in some of my post calls the payload is exactly the same but url is different, but with powetools idempotency I can only specify one dataIndexArgument , but I need both url and payload to be considered for idempotency key, is there a way to do that ?

@dreamorosi
Copy link
Contributor Author

import type {
  AxiosInstance,
  AxiosRequestConfig,
  RawAxiosRequestHeaders,
} from 'axios';
import type { Context } from 'aws-lambda';
import {
  makeIdempotent,
  IdempotencyConfig,
} from '@aws-lambda-powertools/idempotency';
import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb';
import axios from 'axios';
import { Logger } from '@aws-lambda-powertools/logger';

const logger = new Logger({ logLevel: 'debug' });
const persistenceStore = new DynamoDBPersistenceLayer({
  tableName: 'idempotency-table-standard',
});
const IdempConfig = new IdempotencyConfig({});

class MyError extends Error {
  constructor({ name, message }: { name: string; message: string }) {
    super(message);
    this.name = name;
  }
}

class MyFunction {
  private readonly client: AxiosInstance;

  constructor() {
    this.client = axios.create();
  }

  async configureAuthHeader(
    headers: RawAxiosRequestHeaders
  ): Promise<RawAxiosRequestHeaders> {
    // Your implementation goes here
    return headers;
  }

  async post<T, R>(
    url: string,
    payload: T,
    config?: AxiosRequestConfig
  ): Promise<R> {
    const headers = await this.configureAuthHeader(
      config?.headers as RawAxiosRequestHeaders
    );
    const idempotentPost = makeIdempotent(
      async ({ url, payload }) => {
        try {
          const response = await this.client.post(url, payload, {
            ...config,
            headers,
          });
          logger.debug('Post operation completed successfully', {
            data: response.data,
          });
          return await response.data;
        } catch (error) {
          logger.error('Failed to complete post operation', { error });
          throw new MyError({
            name: 'API_ERROR',
            message: 'Failed to complete post operation',
          });
        }
      },
      {
        persistenceStore: persistenceStore,
        config: IdempConfig,
      }
    );

    return await idempotentPost({ url, payload });
  }

  async handler(event: unknown, context: Context) {
    IdempConfig.registerLambdaContext(context); // Important to call this in your handler to avoid timeout issues

    const data = await this.post<Record<string, string>, { id: number }>(
      'https://jsonplaceholder.typicode.com/posts',
      { message: 'Hello, World!' }
    );
    return {
      statusCode: 200,
      body: JSON.stringify(data),
    };
  }
}

const MyFunctionInstance = new MyFunction();
const handler = MyFunctionInstance.handler.bind(MyFunctionInstance);

Copy link
Contributor

⚠️ COMMENT VISIBILITY WARNING ⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

@dreamorosi dreamorosi added completed This item is complete and has been merged/shipped and removed pending-release This item has been merged and will be released soon labels Oct 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
completed This item is complete and has been merged/shipped feature-request This item refers to a feature request for an existing or new utility idempotency This item relates to the Idempotency Utility
Projects
4 participants