Skip to content

Idempotency Getting Started Documentation #377

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

Closed
weallwegot opened this issue Mar 31, 2021 · 3 comments
Closed

Idempotency Getting Started Documentation #377

weallwegot opened this issue Mar 31, 2021 · 3 comments
Assignees
Labels
documentation Improvements or additions to documentation
Milestone

Comments

@weallwegot
Copy link

What were you initially searching for in the docs?
I want to use the idempotency power tool utility to help prevent lambdas from multiple executions

Is this related to an existing part of the documentation? Please share a link
https://awslabs.github.io/aws-lambda-powertools-python/utilities/idempotency/

Describe how we could make it clearer
Currently there is a template.yml provided to set up the DynamoDB table required for the example, but it would be great to also have just screenshots of how one would set up DynamoDB from the console as well. I'm having an issue configuring my table correctly and i cant pinpoint if I've done it wrong or its an issue elsewhere.

Could also be helpful to note if anything needs to be done differently if you are setting up with a lambda that exists in a VPC, for instance do i need an endpoint?

If you have a proposed update, please share it here

Proposed update would be another tab next to the template.yml that shows the equivalent configuration for the console.

@weallwegot weallwegot added the documentation Improvements or additions to documentation label Mar 31, 2021
@michaelbrewer
Copy link
Contributor

@cakepietoast @heitorlessa hopefully a good case for an article / repo for idempotent feature.

@michaelbrewer
Copy link
Contributor

@weallwegot here is an example using CDK to provision the table. I can put up a full Gist for this

from aws_cdk import core as cdk, aws_lambda, aws_dynamodb
from aws_cdk.aws_lambda_python import PythonFunction


class CdkStack(cdk.Stack):
    def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        table = aws_dynamodb.Table(
            scope=self,
            id="idemTable",
            table_name="idem-dev",
            partition_key=aws_dynamodb.Attribute(name="id", type=aws_dynamodb.AttributeType.STRING),
            time_to_live_attribute="expiration",
            billing_mode=aws_dynamodb.BillingMode.PAY_PER_REQUEST,
        )

        func = PythonFunction(
            scope=self,
            id="idemFunction",
            entry="idem/",
            function_name="idem-dev",
            runtime=aws_lambda.Runtime.PYTHON_3_8,
            environment={"LOGGING": "DEBUG", "IDEM_TABLE": table.table_name},
        )
        table.grant_read_write_data(func)

The actual python lambda in idem/index.py:

import json
import os
import uuid

from aws_lambda_powertools import Logger
from aws_lambda_powertools.utilities.idempotency import idempotent, DynamoDBPersistenceLayer, IdempotencyConfig

logger = Logger()

@idempotent(
    persistence_store=DynamoDBPersistenceLayer(table_name=os.environ["IDEM_TABLE"]),
    config=IdempotencyConfig(
        event_key_jmespath="headers.idempotentKey",
        raise_on_no_idempotency_key=True,
        use_local_cache=True,
    ),
)
def handler(event, context):
    logger.info(event)
    return {
        "statusCode": 200,
        "headers": {"Content-type": "application/json"},
        "body": json.dumps({"message": "Hello", "uuid": str(uuid.uuid4())}),
    }

@heitorlessa
Copy link
Contributor

hey @weallwegot Thanks for helping us make the docs better!

I've added a table explaining the two configuration and their values that we expect if you're not overriding the default behaviour - Thanks to your suggestion, this should help anyone not using Infrastructure as Code to quickly create one in the Console.

I didn't add a Console screenshot as this might change and misguide customers, for example if the TTL attribute can be created in a different part of the console.

Docs: https://awslabs.github.io/aws-lambda-powertools-python/develop/utilities/idempotency/#required-resources

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
Development

No branches or pull requests

3 participants