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

Update deployment-support.yaml to fix UnboundLocalError #669

Merged
merged 2 commits into from
Dec 10, 2024

Conversation

MustaphaU
Copy link
Contributor

@MustaphaU MustaphaU commented Dec 1, 2024

Issue #, if available:
#668

Description of changes:
This PR updates the index.handler function for the LoadDataLambdaFunction to ensure no undefined variables are referenced. A new variable, error, is introduced to track the most recent exception during retries. The logger.error statement has been updated to reference error instead of e, ensuring the function handles errors without encountering an UnboundLocalError. Also, since URLError has no code attribute, only the reason attribute is used.

relevant section with updates

while retries < max_retries:
      try:
          with urlopen(request) as response:
              logger.info(f"Product Service init method success: {response.read()}")
              # exit while, success.
              break
      except URLError as e:
          error = e  #e is assigned to a variable
          retries += 1
          print(f"Request failed. Retrying in 30 seconds... (Attempt {retries}/{max_retries})")
          print(f"Error: {e}")
          time.sleep(30)

#
if retries >= max_retries:
    logger.error(f"Error calling product service init: {error.reason}") 
    response_status = cfnresponse.FAILED
    response_data['Message'] = f"Resource {event['RequestType']} failed: {error}" 

Description of testing performed to validate your changes (required if pull request includes CloudFormation or source code changes):

  1. Deployed the CloudFormation template with the updated Lambda function.
  2. Confirmed that exceptions are logged correctly without triggering an UnboundLocalError

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Copy link
Contributor

@BastLeblanc BastLeblanc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks for the fix

@BastLeblanc BastLeblanc merged commit 9fd995a into aws-samples:master Dec 10, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Shipped
Development

Successfully merging this pull request may close these issues.

2 participants