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: idempotent function wrapper #1305

Closed
1 of 2 tasks
saragerion opened this issue Feb 17, 2023 · 2 comments · Fixed by #1540
Closed
1 of 2 tasks

Feature request: idempotent function wrapper #1305

saragerion opened this issue Feb 17, 2023 · 2 comments · Fixed by #1540
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

@saragerion
Copy link
Contributor

saragerion commented Feb 17, 2023

Use Case

Users who can't or don't use class-based decorators or Middy should be able to make arbitrary functions idempotent by using a function wrapper. This wrapper, also known in the industry as higher-order function should take the function to be made idempotent as first argument and an object with options and configs as second one.

Solution/User Experience

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

const config = new IdempotencyConfig({...});
const ddbPersistenceLayer = new DynamoDBPersistenceLayer({...});

 /**
 * Function to process a single record
 */
function processRecord(record: any) {
  /* ...Function logic here... */
  return result;
} 

/**
 * Higher-order function wrapper to process a single record
 */
const processIdempotently = makeFunctionIdempotent(
  processRecord, 
 {
    persistenceStore: ddbPersistenceLayer,
    dataArgument: 'record', 
    config,
 }  
);

export const handler = async (_event: any, _context: any): Promise<void> => {
    
  const records = /*..Extract SQS/DDB stream record, etc..*/

  const results = [];
  for (const record of records) {
    results.push(processIdempotently(record));
  }
  /* ...Format and return result... */
}

The makeFunctionIdempotent function wrapper should be able wrap both async and sync functions. It should accept the wrapped function (aka the function to be made idempotent) as first argument, and an object with mandatory persistenceStore and dataArgument, as well as an optional config one. The former should be an instance of any class that extends BasePersistenceLayer, the second should be a string that represents the argument to be used for idempotency (in the wrapped function), while the latter should be an instance of the class IdempotencyConfig.

Following the Powertools for Python implementation, the wrapper should:

  • return early if the POWERTOOLS_IDEMPOTENCY_DISABLED env variable has a truthy value (using EnvironmentVariableService)
  • use the provided config object or instantiate a new one if none is passed
  • register the Lambda context into the config object (used to manage timeouts)
  • instantiate an IdempotencyHandler
  • call & return the IdempotencyHandler.handle() method

This last step will ensure that the IdempotencyHandler will perform all the actions needed to make the function idempotent.

Alternative solutions

No response

Acknowledgment

@saragerion saragerion added triage This item has not been triaged by a maintainer, please wait feature-request This item refers to a feature request for an existing or new utility labels Feb 17, 2023
@saragerion saragerion added this to the Idempotency - Beta release milestone Feb 17, 2023
@saragerion saragerion changed the title Feature request (idempotency): make function idempotent via decorator for events in sequence (happy path) Feature request (idempotency): make function idempotent via function wrapper for events in sequence (happy path) Feb 17, 2023
@saragerion saragerion added the idempotency This item relates to the Idempotency Utility label Feb 17, 2023
@dreamorosi dreamorosi added discussing The issue needs to be discussed, elaborated, or refined and removed triage This item has not been triaged by a maintainer, please wait labels Feb 17, 2023
@dreamorosi dreamorosi changed the title Feature request (idempotency): make function idempotent via function wrapper for events in sequence (happy path) Feature request: make function idempotent via function wrapper for events in sequence (happy path) Feb 17, 2023
@dreamorosi dreamorosi changed the title Feature request: make function idempotent via function wrapper for events in sequence (happy path) Feature request: idempotent function wrapper Mar 20, 2023
@dreamorosi dreamorosi added confirmed The scope is clear, ready for implementation blocked This item's progress is blocked by external dependency or reason and removed discussing The issue needs to be discussed, elaborated, or refined confirmed The scope is clear, ready for implementation labels Mar 20, 2023
@dreamorosi dreamorosi moved this from Backlog to Working on it in AWS Lambda Powertools for TypeScript Apr 24, 2023
@dreamorosi dreamorosi added confirmed The scope is clear, ready for implementation and removed blocked This item's progress is blocked by external dependency or reason labels Apr 24, 2023
@dreamorosi
Copy link
Contributor

As far as I can tell with #1540 we completed the feature.

Closing this for the time being.

@github-project-automation github-project-automation bot moved this from Working on it to Coming soon in Powertools for AWS Lambda (TypeScript) Jun 22, 2023
@github-project-automation github-project-automation bot moved this from Working on it to Coming soon in AWS Lambda Powertools for TypeScript Jun 22, 2023
@dreamorosi dreamorosi moved this from Coming soon to Shipped in Powertools for AWS Lambda (TypeScript) Jun 22, 2023
@dreamorosi dreamorosi added completed This item is complete and has been merged/shipped and removed confirmed The scope is clear, ready for implementation labels Jun 22, 2023
@github-actions
Copy link
Contributor

⚠️ COMMENT VISIBILITY WARNING ⚠️

Comments on closed issues 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.

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