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

Bug: parameters.get_parameters(.., max_age=24*60*60, force_fetch=True) does not work as expected anymore #5482

Closed
NastHoloviz opened this issue Oct 31, 2024 · 7 comments · Fixed by #5515
Assignees
Labels
bug Something isn't working parameters Parameters utility

Comments

@NastHoloviz
Copy link

NastHoloviz commented Oct 31, 2024

Expected Behaviour

There was the issue found that when you grab data with force_fetch=True from the code you still get old value from the cache instead.
You can find the example of the code of AWS Lambda lambda function in the code snippet as well, but I will explain here the details.
So here are some steps:

  1. Get parameters with parameters.get_parameters(.., max_age=24*60*60, force_fetch=False) - this will either get parameters data from API and put them into the cache or get parameters from cache if lambda was triggered before already (lambda function env memory).
  2. Update one of the parameters within same lambda function code (you can make sure later going to AWS Console that parameter was really updated).
  3. Get parameters with parameters.get_parameters(.., max_age=24*60*60, force_fetch=True) within same code of same lambda function and see that value for updated parameter is still the old one. So that means that force_fetch actually either did not get updated value from the API or did not update the cache.

In the context of one lambda function execution it might be useless, but if we get params in lambda and put them to the cache, in couple minutes update some of the parameters and then trigger lambda function again with force_fetch=True - this lambda function still keeps old value in the memory. This is the issue.
So expected behavior is that with force_fetch=True we will always get values updated from SSM.

Current Behaviour

  1. Get parameters with parameters.get_parameters(.., max_age=24*60*60, force_fetch=False) - this will either get parameters data from API and put them into the cache or get parameters from cache if lambda was triggered before already (lambda function env memory).
  2. Update one of the parameters within same lambda function code (you can make sure later going to AWS Console that parameter was really updated).
  3. Get parameters with parameters.get_parameters(.., max_age=24*60*60, force_fetch=True) within same code of same lambda function and see that value for updated parameter is still the old one. So that means that force_fetch actually either did not get updated value from the API or did not update the cache.

Image

Code snippet

@app.lambda_function()
def test_force_fetch(event: dict, context: object):
    """Lambda function to test force_fetch=True is not working."""
    # Get params from SSM, use cache here as well
    params = parameters.get_parameters(
        path="/path-to-param/",
        recursive=True,
        decrypt=True,
        max_age=24 * 60 * 60,  # 24 hours
        force_fetch=False,
    )
    config = {}
    for key, value in params.items():
        config[key] = json.loads(value)
    app.log.info("Test value: %s", config["TEST_VALUE"])

    # Update SSM parameter manually
    ssm = boto3.client("ssm")
    new_value = str(uuid.uuid4())
    app.log.info("Value to set in SSM: %s", new_value)
    ssm.put_parameter(
        Name="/path-to-param/TEST_VALUE",
        Value=json.dumps(new_value),
        Type="SecureString",
        Overwrite=True,
    )
    time.sleep(5)

    # Get params from SSM with force_fetch, use cache here as well
    params = parameters.get_parameters(
        path="/path-to-param/",
        recursive=True,
        decrypt=True,
        max_age=24 * 60 * 60,  # 24 hours
        force_fetch=True,
    )
    config = {}
    for key, value in params.items():
        config[key] = json.loads(value)
    app.log.info("Test value with force: %s", config["TEST_VALUE"])

Possible Solution

No response

Steps to Reproduce

Please see Code snippet and Expected Behaviour sections.

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.9

Packaging format used

PyPi

Debugging logs

Please see logs in attached screenshot.

@NastHoloviz NastHoloviz added bug Something isn't working triage Pending triage from maintainers labels Oct 31, 2024
Copy link

boring-cyborg bot commented Oct 31, 2024

Thanks for opening your first issue here! We'll come back to you as soon as we can.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

@leandrodamascena
Copy link
Contributor

Hey @NastHoloviz, thanks for reporting this! I'm taking a look in this issue right now.

@leandrodamascena leandrodamascena self-assigned this Nov 6, 2024
@leandrodamascena leandrodamascena added parameters Parameters utility and removed triage Pending triage from maintainers labels Nov 6, 2024
@leandrodamascena leandrodamascena moved this from Triage to Working on it in Powertools for AWS Lambda (Python) Nov 6, 2024
@leandrodamascena
Copy link
Contributor

Hi @NastHoloviz! Again, thanks a lot for reporting this, yes I can confirm this is a bug when using get_parameters, but using get_parameter it works as expected. I'm working on submitting a fix for this.

@leandrodamascena
Copy link
Contributor

Python shines brightly when it’s at its best, but it can be a real headache when it’s not. In the get_multiple function the order of parameter force_fetch and raise_on_transform_error is inverted, and since the function from the main class expects `**kwargs, it was failing. Sending a PR.

Copy link
Contributor

github-actions bot commented Nov 7, 2024

⚠️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 the pending-release Fix or implementation already in dev waiting to be released label Nov 7, 2024
Copy link
Contributor

This is now released under 3.3.0 version!

@github-actions github-actions bot removed the pending-release Fix or implementation already in dev waiting to be released label Nov 14, 2024
@NastHoloviz
Copy link
Author

Thanks for taking care of this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working parameters Parameters utility
Projects
Status: Coming soon
2 participants