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: allow to set idempotency prefix #3515

Closed
2 tasks done
leandrodamascena opened this issue Jan 23, 2025 · 5 comments · Fixed by #3532
Closed
2 tasks done

Feature request: allow to set idempotency prefix #3515

leandrodamascena opened this issue Jan 23, 2025 · 5 comments · Fixed by #3532
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

@leandrodamascena
Copy link
Contributor

leandrodamascena commented Jan 23, 2025

Use case

Original issue: aws-powertools/powertools-lambda-python#5897

The prefix to the Idempotency key is hardcoded to force a fully qualified name based on the location of the function that uses the decorator. There is no simple way of modifying this behavior in order to make that prefix some static value.

https://github.com/aws-powertools/powertools-lambda-typescript/blob/main/packages/idempotency/src/persistence/BasePersistenceLayer.ts#L55

This makes the Idempotency susceptible to future code refactoring. To prevent duplicate handling you can either

Solution/User Experience

A solution is to provide a way to override the key prefix on the makeIdempotent function. An optional argument like keyPrefix that allows a user to provided a fix string. This would allow freedom of refactoring (or even have multiple functions that are controlled by the same idempotency key space).

Considerations:

  • This should only control the prefix. The standard behavior of appending # would stay intact

Alternative solutions

Python implementation + documentation: https://github.com/aws-powertools/powertools-lambda-python/pull/5898 

Acknowledgment

Future readers

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

@leandrodamascena leandrodamascena added feature-request This item refers to a feature request for an existing or new utility triage This item has not been triaged by a maintainer, please wait labels Jan 23, 2025
@leandrodamascena leandrodamascena added idempotency This item relates to the Idempotency Utility and removed triage This item has not been triaged by a maintainer, please wait labels Jan 23, 2025
@dreamorosi dreamorosi changed the title Feature request: Idempotency prefix is susceptible to refactoring Feature request: allow to set Idempotency prefix Jan 23, 2025
@dreamorosi dreamorosi changed the title Feature request: allow to set Idempotency prefix Feature request: allow to set idempotency prefix Jan 23, 2025
@dreamorosi dreamorosi added confirmed The scope is clear, ready for implementation help-wanted We would really appreciate some support from community for this one labels Jan 23, 2025
@dreamorosi
Copy link
Contributor

Thank you for creating the issue Leo.

Note

Below are some more information for the implementation in this side of Powertools for AWS. If anyone is interested in picking this up please leave a comment to claim the issue. A maintainer will assign the issue to you and help you with reviewing your PR. Likewise, if you have any questions before starting, feel free to leave a comment below.

As part of this work we should add a new keyPrefix parameter that customers can use to customize the Idempotency prefix. This is the prefix of the id attribute used to store the Idempotency record in the persistence store.

Normally this prefix is created automatically by the utility using the AWS Lambda function name (aka the name of the resource) and the name of the function in your code being made idempotent (i.e. handler). The logic for how the prefix is constructed is here and an example could be payment-function.handler when making idempotent the handler function within a Lambda function named payment-production

With this change, customers should be able to specify their own prefix. When doing this, the customer-provided prefix will be used instead of the one generated by the utility. Customers should be able to do this when decorating a function, making it idempotent via Middy.js middleware, or higher-order function. Below some examples of how the DX would look like after the change.

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

const processPayment = makeIdempotent(
  async (paymentProps) => {
    // ... process your payload

    return {
      message: 'success',
      statusCode: 200,
    };
  },
  {
    persistenceStore,
    keyPrefix: 'some_custom_value', // <-- new parameter
  }
);

export const handler = async (event) => {
  // ... do something with the event
  return processPayment({
    customerId: event.customerId,
    amount: event.amount
  });
};

// OR


const config = new IdempotencyConfig({});

class MyLambda implements LambdaInterface {
  @idempotent({
    persistenceStore,
    config,
    keyPrefix: 'some_custom_value', // <-- new parameter
   })
  public async handler(_event, _context) {
    // ... process your event
    return {
      message: 'success',
      statusCode: 200,
    };
  }
}

const defaultLambda = new MyLambda();
export const handler = defaultLambda.handler.bind(defaultLambda);

// OR

export const handler = middy(
  async (event) => {
    // ... process your event
).use(
  makeHandlerIdempotent({
    persistenceStore,
    keyPrefix: 'some_custom_value', // <-- new parameter
  })
);

To do this, we should follow this high level plan:

  • Add a new optional keyPrefix option to the IdempotencyOptions
  • Add the same option to the BasePersistenceLayerOptions type
  • Handle the new keyPrefix option in the IdempotencyHandler constructor, and pass it down to the .configure() call here
  • Modify the logic here so that the keyPrefix is used whenever present and set to the this.idempotencyKeyPrefix property
  • Write unit tests for the new feature, ensuring coverage stays 100%
  • Write documentation, you can look at the one in the Python implementation from the PR linked in the OP

Note that there might be other parts of the code to touch that I might have forgotten, make sure to take a closer look at the current implementation before starting.

@shdq
Copy link
Contributor

shdq commented Jan 24, 2025

Hi, I'd like to help with this issue. Could you please assign it to me?

@dreamorosi dreamorosi moved this from Backlog to Working on it in Powertools for AWS Lambda (TypeScript) Jan 24, 2025
@dreamorosi
Copy link
Contributor

Hi, I'd like to help with this issue. Could you please assign it to me?

Absolutely! And nice to see you back here, looking forward to work with you on the PR!

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.

@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 Feb 17, 2025
Copy link
Contributor

This is now released under v2.15.0 version!

@github-actions github-actions bot 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 Feb 25, 2025
@dreamorosi dreamorosi moved this from Coming soon to Shipped in Powertools for AWS Lambda (TypeScript) Feb 25, 2025
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
Development

Successfully merging a pull request may close this issue.

3 participants