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

Handle Container Creation Failure in local invoke, local start-api and local start-lambda #6719

Merged
merged 16 commits into from
Mar 7, 2024

Conversation

bentvelj
Copy link
Contributor

@bentvelj bentvelj commented Feb 20, 2024

Re-raising this PR to handoff to another developer, since the previous PR was from a personal fork.

Validate function MemorySize to be an integer within range [128, 10240] as per docs.

Also had to fix a bunch of unit tests that previously had fake MemorySize.

Which issue(s) does this change fix?

#6510

Why is this change necessary?

Customers were permitted to build Lambda functions with invalid MemorySize, causing late/unexpected failure

How does it address the issue?

  • during __init__ of InvokeContext, run lint and display any lint errors as warning
  • Catch any Docker API error while creating container and reraise as DockerContainerCreationFailedException. Each local command can handle the error differently:
    • local invoke will surface this error and exit
    • local start-api and local start-lambda will display a warning but would not exit the process

What side effects does this change have?

N/A

Mandatory Checklist

PRs will only be reviewed after checklist is complete

  • Add input/output type hints to new functions/methods
  • Write design document if needed (Do I need to write a design document?)
  • Write/update unit tests
  • Write/update integration tests
  • Write/update functional tests if needed
  • make pr passes
  • make update-reproducible-reqs if dependencies were changed
  • Write documentation

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@bentvelj bentvelj requested a review from a team as a code owner February 20, 2024 19:15
@github-actions github-actions bot added area/local/invoke sam local invoke command pr/internal labels Feb 20, 2024
@bentvelj bentvelj changed the title Refactor to run sam validate before any sam local logic Add MemorySize validation for function build Feb 20, 2024
@@ -219,6 +225,10 @@ def __enter__(self) -> "InvokeContext":

self._stacks = self._get_stacks()

if self._ctx:
LOG.info("Validating template %s\n", self._template_file)
run_sam_validate(ctx=self._ctx, template=self._template_file, lint=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this going to fail the command if validation fails?

Copy link
Contributor

Choose a reason for hiding this comment

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

seems like it could raise User Exception. Does it make sense to not be able to run invoke if a template is invalid?

@hawflau hawflau marked this pull request as draft February 26, 2024 16:37
@hawflau hawflau changed the title Add MemorySize validation for function build Handle Container Creation Failure in local invoke, local start-api and local start-lambda Feb 27, 2024
@hawflau hawflau marked this pull request as ready for review February 27, 2024 23:00
Comment on lines 160 to 161
ctx dict
Optional. Command invoke context used to invoke commands before invoking `sam local` logic
Copy link
Contributor

Choose a reason for hiding this comment

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

If we are only using to grab the debug flag, maybe we can pass that field instead of the whole Context object?

@@ -220,6 +226,11 @@ def __enter__(self) -> "InvokeContext":

self._stacks = self._get_stacks()

if self._ctx:
_, matches_output = get_lint_matches(self._template_file, self._ctx.debug, self._aws_region)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we wrap this call with try/except since there might be some exceptions which is thrown by linter logic? (I saw we are checking valid region configuration for instance).

Copy link
Contributor

@mndeveci mndeveci left a comment

Choose a reason for hiding this comment

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

Thanks for addressing previous comments! Left an unblocking comment trying to make sure we are catching possible exceptions from this new method call.

_, matches_output = get_lint_matches(self._template_file, self._verbose, self._aws_region)
if matches_output:
LOG.warning("Lint Error found, containter creation might fail: %s", matches_output)
except UserException as e:
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we make sure everything is wrapped as UserException so there won't be a new exception type which might be thrown from get_lint_matches?

Copy link
Contributor

Choose a reason for hiding this comment

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

errors within cfn-lint seem to be caught and wrapped as CfnLintExitException which is handled in this method. I think it should be fine.

@hawflau hawflau added this pull request to the merge queue Mar 7, 2024
Merged via the queue into develop with commit 9bcbc04 Mar 7, 2024
55 checks passed
sidhujus added a commit that referenced this pull request Mar 7, 2024
…tart-api` and `local start-lambda` (#6719)"

This reverts commit 9bcbc04.
hawflau added a commit that referenced this pull request Mar 9, 2024
…tart-api` and `local start-lambda` (#6719)"

This reverts commit 9bcbc04.
github-merge-queue bot pushed a commit that referenced this pull request Mar 9, 2024
* Revert "Fix bug in PR#6719 (#6794)"

This reverts commit 39240d0.

* Revert "Handle Container Creation Failure in `local invoke`, `local start-api` and `local start-lambda` (#6719)"

This reverts commit 9bcbc04.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/local/invoke sam local invoke command pr/internal
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants