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

memory and timeout vars for lambda@edge #330

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

mihaiplesa
Copy link

@mihaiplesa mihaiplesa commented Jan 11, 2025

what

Allow to configure memory size and timeout for Lambda@Edge module.

why

These fields are not configurable now.

references

Resolves #331

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features
    • Added configurable memory size and timeout settings for Lambda@Edge functions.
    • Introduced new input parameters to control Lambda function resource allocation and execution time.
    • Default memory set to 128 MB and default timeout set to 3 seconds.

@mihaiplesa mihaiplesa requested review from a team as code owners January 11, 2025 09:36
Copy link

coderabbitai bot commented Jan 11, 2025

📝 Walkthrough

Walkthrough

The changes introduce two new configuration parameters, memory_size and timeout, to the Lambda@Edge module across multiple files. These parameters allow users to specify the memory allocation and maximum execution time for Lambda functions. The modifications are consistently implemented in the Terraform configuration, module variables, main resource definition, and module documentation, providing more granular control over Lambda function resources.

Changes

File Change Summary
examples/complete/lambda-at-edge.tf Added memory_size = 128 and timeout = 3 to module configuration
modules/lambda@edge/README.md Added documentation for new input variables memory_size and timeout
modules/lambda@edge/main.tf Updated aws_lambda_function resource to include memory_size and timeout parameters
modules/lambda@edge/variables.tf Introduced new variables memory_size and timeout with default values

Sequence Diagram

sequenceDiagram
    participant User as Terraform User
    participant Module as Lambda@Edge Module
    participant AWS as AWS Lambda

    User->>Module: Configure memory_size and timeout
    Module->>AWS: Create Lambda Function
    AWS-->>Module: Function Created
    Module-->>User: Deployment Complete
Loading

Assessment against linked issues

Objective Addressed Explanation
Be able to set memory size and timeout. (#331)
Improve customization of Lambda@Edge module. (#331)

Poem

🐰 Hopping through code with glee and might,
Lambda functions now shine so bright!
Memory measured, timeout set,
Configuration's a rabbit's pet!
Terraform magic, cloud so clear! 🚀


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@mergify mergify bot added the triage Needs triage label Jan 11, 2025
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 4

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c95097f and a2d3444.

📒 Files selected for processing (4)
  • examples/complete/lambda-at-edge.tf (1 hunks)
  • modules/lambda@edge/README.md (1 hunks)
  • modules/lambda@edge/main.tf (1 hunks)
  • modules/lambda@edge/variables.tf (1 hunks)
🧰 Additional context used
🪛 Markdownlint (0.37.0)
modules/lambda@edge/README.md

74-74: Element: a
Inline HTML

(MD033, no-inline-html)


75-75: Element: a
Inline HTML

(MD033, no-inline-html)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Summary
🔇 Additional comments (1)
modules/lambda@edge/main.tf (1)

85-86: LGTM! Variables are properly integrated

The memory_size and timeout variables are correctly added to the aws_lambda_function resource.

Comment on lines 72 to 76
type = number
description = "The amount of time the Lambda Function has to run in seconds."
default = 3
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add validation for Lambda@Edge timeout constraints

Lambda@Edge functions have stricter timeout limits compared to regular Lambda functions. The timeout varies based on the event type.

 variable "timeout" {
   type        = number
   description = "The amount of time the Lambda Function has to run in seconds."
   default     = 3
+  validation {
+    condition     = var.timeout >= 1 && var.timeout <= 30
+    error_message = "Lambda@Edge timeout must be between 1 and 30 seconds."
+  }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
variable "timeout" {
type = number
description = "The amount of time the Lambda Function has to run in seconds."
default = 3
}
variable "timeout" {
type = number
description = "The amount of time the Lambda Function has to run in seconds."
default = 3
validation {
condition = var.timeout >= 1 && var.timeout <= 30
error_message = "Lambda@Edge timeout must be between 1 and 30 seconds."
}
}

Comment on lines 66 to 70
type = number
description = "Amount of memory in MB the Lambda Function can use at runtime."
default = 128
}
Copy link

Choose a reason for hiding this comment

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

🧹 Nitpick (assertive)

Consider adding validation for Lambda@Edge memory constraints

Lambda@Edge functions have specific memory constraints. Consider adding a validation block to ensure the value stays within Lambda@Edge limits (128MB to 10240MB).

 variable "memory_size" {
   type        = number
   description = "Amount of memory in MB the Lambda Function can use at runtime."
   default     = 128
+  validation {
+    condition     = var.memory_size >= 128 && var.memory_size <= 10240
+    error_message = "Lambda@Edge memory_size must be between 128 MB and 10240 MB."
+  }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
variable "memory_size" {
type = number
description = "Amount of memory in MB the Lambda Function can use at runtime."
default = 128
}
variable "memory_size" {
type = number
description = "Amount of memory in MB the Lambda Function can use at runtime."
default = 128
validation {
condition = var.memory_size >= 128 && var.memory_size <= 10240
error_message = "Lambda@Edge memory_size must be between 128 MB and 10240 MB."
}
}

Comment on lines 90 to 91
memory_size = 128
timeout = 3
Copy link

Choose a reason for hiding this comment

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

🧹 Nitpick (assertive)

Consider documenting memory and timeout choices

While the values match the defaults, it would be helpful to document why these specific values were chosen for the example, especially since different Lambda@Edge event types might have different requirements.

+  # Minimum memory size for Lambda@Edge functions
   memory_size = 128
+  # Conservative timeout for viewer-request/response functions
   timeout     = 3
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
memory_size = 128
timeout = 3
# Minimum memory size for Lambda@Edge functions
memory_size = 128
# Conservative timeout for viewer-request/response functions
timeout = 3

Comment on lines 74 to 75
| <a name="input_memory_size"></a> [memory\_size](#input\_memory\_size) | Amount of memory in MB the Lambda Function can use at runtime. | `number` | `128` | no |
| <a name="input_timeout"></a> [timeout](#input\_timeout) | The amount of time the Lambda Function has to run in seconds. | `number` | `3` | no |
Copy link

Choose a reason for hiding this comment

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

🧹 Nitpick (assertive)

Enhance documentation with Lambda@Edge specific details

The documentation should include information about Lambda@Edge specific constraints and recommendations for different event types.

-| <a name="input_memory_size"></a> [memory_size](#input_memory_size) | Amount of memory in MB the Lambda Function can use at runtime. | `number` | `128` | no |
-| <a name="input_timeout"></a> [timeout](#input_timeout) | The amount of time the Lambda Function has to run in seconds. | `number` | `3` | no |
+| <a name="input_memory_size"></a> [memory_size](#input_memory_size) | Amount of memory in MB the Lambda Function can use at runtime. Must be between 128 MB and 10240 MB for Lambda@Edge. | `number` | `128` | no |
+| <a name="input_timeout"></a> [timeout](#input_timeout) | The amount of time the Lambda Function has to run in seconds. Must be between 1 and 30 seconds for Lambda@Edge. Note that viewer-request and viewer-response functions have a 5-second timeout limit, while origin-request and origin-response functions have a 30-second limit. | `number` | `3` | no |
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
| <a name="input_memory_size"></a> [memory\_size](#input\_memory\_size) | Amount of memory in MB the Lambda Function can use at runtime. | `number` | `128` | no |
| <a name="input_timeout"></a> [timeout](#input\_timeout) | The amount of time the Lambda Function has to run in seconds. | `number` | `3` | no |
| <a name="input_memory_size"></a> [memory_size](#input_memory_size) | Amount of memory in MB the Lambda Function can use at runtime. Must be between 128 MB and 10240 MB for Lambda@Edge. | `number` | `128` | no |
| <a name="input_timeout"></a> [timeout](#input_timeout) | The amount of time the Lambda Function has to run in seconds. Must be between 1 and 30 seconds for Lambda@Edge. Note that viewer-request and viewer-response functions have a 5-second timeout limit, while origin-request and origin-response functions have a 30-second limit. | `number` | `3` | no |
🧰 Tools
🪛 Markdownlint (0.37.0)

74-74: Element: a
Inline HTML

(MD033, no-inline-html)


75-75: Element: a
Inline HTML

(MD033, no-inline-html)

@mihaiplesa
Copy link
Author

@coderabbitai full review

Copy link

coderabbitai bot commented Jan 11, 2025

✅ Actions performed

Full review triggered.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

♻️ Duplicate comments (2)
examples/complete/lambda-at-edge.tf (1)

35-36: 🧹 Nitpick (assertive)

Consider function-specific configurations.

While the current configuration works, consider adjusting memory and timeout based on function type:

  • Viewer functions (request/response): Lower timeout (max 5s)
  • Origin functions (request/response): Higher timeout if needed (max 30s)

Example configuration:

 viewer_request = {
   ...
   memory_size  = 128
-  timeout      = 3
+  timeout      = 5  # Maximum allowed for viewer functions
   ...
 }
 origin_response = {
   ...
   memory_size  = 128
-  timeout      = 3
+  timeout      = 30  # Maximum allowed for origin functions
   ...
 }

Also applies to: 45-46, 54-55, 88-89

modules/lambda@edge/README.md (1)

76-76: 🧹 Nitpick (assertive)

Enhance documentation with Lambda@Edge constraints.

Consider adding Lambda@Edge specific constraints to the documentation:

 | <a name="input_functions"></a> [functions](#input_functions) | Lambda@Edge functions to create.<br><br>The key of this map is the name label of the Lambda@Edge function.<br><br>`source.filename` and `source.content` dictate the name and content of the files that will make up the Lambda function<br>source, respectively.<br><br>`runtime`, `handler`, `memory_size`, and `timeout` correspond to the attributes of the same name in the [lambda_function](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function)<br>resource.<br><br>`event_type` and `include_body` correspond to the attributes of the same name in the [Lambda Function association block<br>of the cloudfront_distribution](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_distribution#lambda-function-association)<br>resource. |
+<br><br>Lambda@Edge specific constraints:<br>
+- `memory_size`: Must be between 128 MB and 10240 MB<br>
+- `timeout`: Must be between 1 and 30 seconds. Note that viewer-request and viewer-response functions have a 5-second timeout limit, while origin-request and origin-response functions have a 30-second limit. |
🧰 Tools
🪛 Markdownlint (0.37.0)

76-76: Element: a
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: pre
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c95097f and 27acd86.

📒 Files selected for processing (6)
  • README.md (1 hunks)
  • README.yaml (1 hunks)
  • examples/complete/lambda-at-edge.tf (3 hunks)
  • modules/lambda@edge/README.md (1 hunks)
  • modules/lambda@edge/main.tf (1 hunks)
  • modules/lambda@edge/variables.tf (1 hunks)
🧰 Additional context used
🪛 Markdownlint (0.37.0)
modules/lambda@edge/README.md

76-76: Element: a
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: pre
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)


76-76: Element: br
Inline HTML

(MD033, no-inline-html)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Summary
🔇 Additional comments (3)
modules/lambda@edge/variables.tf (1)

32-33: LGTM! Consider implementing the suggested validations.

The addition of memory_size and timeout as optional parameters is correct. However, as suggested in previous review comments, consider adding validation blocks to enforce Lambda@Edge specific constraints:

  • Memory: 128MB to 10240MB
  • Timeout: 1-30s (with 5s limit for viewer functions)
modules/lambda@edge/main.tf (1)

81-82: LGTM! Conservative default values.

The implementation correctly sets conservative default values:

  • memory_size = 128 (minimum allowed)
  • timeout = 3 (safe for all function types)
README.yaml (1)

358-359: LGTM! Example is consistent.

The example configuration is consistent with the implementation and uses safe default values.

Comment on lines +347 to +348
memory_size = 128
timeout = 3
Copy link

Choose a reason for hiding this comment

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

🧹 Nitpick (assertive)

LGTM! Consider enhancing the documentation.

The addition of memory_size and timeout parameters is valuable for controlling Lambda@Edge function resources. The default values are appropriate for most use cases.

Consider adding documentation notes about:

  • Maximum allowed values for memory and timeout
  • How these parameters affect pricing
  • Different timeout limits based on event types (viewer-facing events have 5s limit, origin-facing events have 30s limit)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage Needs triage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Memory size and timeout are not passed to Lambda@Edge
1 participant