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

feat: Add support for custom Lambda@Edge policies #333

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

Conversation

jwadolowski
Copy link

@jwadolowski jwadolowski commented Feb 24, 2025

what

Execution role associated with Lambda@Edge comes with a hardcoded policy that enables write access to CloudWatch logs. This PR adds support for additional policies. It was implemented in a similar fashion to additional_bucket_policy from the parent module.

why

It's a fairly common situation that a Lambda@Edge function needs access to other AWS services/resources than CloudWatch logs. aws_lambda_function's role argument expects a single role ARN, therefore the only reasonable option is to append new policy statements to the IAM role created in scope of this module.

references

closes #261

Copy link

coderabbitai bot commented Feb 24, 2025

📝 Walkthrough

Walkthrough

This pull request introduces a new IAM policy document (s3_policy) that allows s3:GetObject and s3:PutObject actions on specified S3 resources. This policy is referenced in the origin_request function of the lambda_at_edge module through a new additional_policy attribute. The functions input parameter has been updated to require one of three mutually exclusive source options and now includes optional attributes for memory_size, timeout, and additional_policy. Changes in the module’s main and variables files enhance how these policies are managed and declared.

Changes

File(s) Summary of Changes
examples/complete/lambda-at-edge.tf Added a new data block aws_iam_policy_document "s3_policy" allowing s3:GetObject and s3:PutObject actions, and integrated this via a new additional_policy attribute in the origin_request function (with minor formatting adjustments).
modules/lambda@edge/{README.md, main.tf, variables.tf} Updated the functions input parameter to require one of source, source_dir, or source_zip; added optional attributes for memory_size (default 128), timeout (default 3), and additional_policy; and modified the role block to include each.value.additional_policy.

Possibly related PRs

  • memory and timeout vars for lambda@edge #330: Involves similar modifications in the lambda_at_edge module, including the addition and handling of new attributes like additional_policy alongside updates to function configuration.

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.
  • @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 Feb 24, 2025
@jwadolowski jwadolowski marked this pull request as ready for review February 24, 2025 18:14
@jwadolowski jwadolowski requested review from a team as code owners February 24, 2025 18:14
Attribute renamed from `policy_documents` to `additional_policy`. Add
`LambdaWriteCloudWatchLogs` SID to default CloudWatch policy to make
overrrides possible.
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: 0

🧹 Nitpick comments (1)
modules/lambda@edge/README.md (1)

76-80: Clarify and Enhance Documentation for New Function Attributes

The updated functions input block now includes the mutually exclusive source options (source, source_dir, source_zip) and introduces the additional_policy attribute. While the description is comprehensive, consider the following improvements:

  • Detail the additional_policy Format: Explicitly state that additional_policy should be provided as a valid JSON string representing IAM policy statements. Explain how the substitution of $${lambda_arn} works within these policies.
  • Reference Custom Policy Usage: Mention how overriding the default policy statement (by specifying a custom statement with the LambdaWriteCloudWatchLogs sid) affects the execution role.
  • Clarify Mutual Exclusivity: Although the mutually exclusive nature of source, source_dir, and source_zip is mentioned, a brief example or note clarifying the expected usage might help users avoid configuration mistakes.

These enhancements will improve readability and assist users in correctly configuring their Lambda@Edge functions with custom IAM policies.

🧰 Tools
🪛 LanguageTool

[grammar] ~77-~77: The verb ‘keep’ seems to be in the wrong form here.
Context: ... for unlimited length.
Set to null for keep the existing setting, which defaults to...

(FOR_VB)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between a433947 and b219f28.

📒 Files selected for processing (1)
  • modules/lambda@edge/README.md (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Summary

Dynamic Lambda ARN repalcements bring no value for Lambda IAM execution
roles, as typically you'd like to list other AWS resources/services
there, not the Lambda itself.
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: 0

🧹 Nitpick comments (1)
modules/lambda@edge/README.md (1)

76-77: Enhance Documentation for the Updated functions Input Object

The updated functions input object now includes new attributes (source_dir, source_zip, and additional_policy) to accommodate multiple source options and additional IAM policies. The description clearly explains that one of these source options must be specified and that if additional_policy is used, the variable $${lambda_arn} will be substituted.

Suggestion: Consider adding a brief example snippet demonstrating how to override the default LambdaWriteCloudWatchLogs policy via additional_policy. This extra context could help users understand the practical usage of the new feature.

🧰 Tools
🪛 LanguageTool

[grammar] ~77-~77: The verb ‘keep’ seems to be in the wrong form here.
Context: ... for unlimited length.
Set to null for keep the existing setting, which defaults to...

(FOR_VB)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between b219f28 and 489515e.

📒 Files selected for processing (2)
  • modules/lambda@edge/README.md (1 hunks)
  • modules/lambda@edge/variables.tf (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • modules/lambda@edge/variables.tf
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Summary

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: 0

🧹 Nitpick comments (1)
modules/lambda@edge/README.md (1)

76-77: Enhanced Input Definition for Lambda@Edge Functions

The updated functions input clearly documents the new, mutually exclusive options (source, source_dir, source_zip) and introduces the additional_policy field with a sensible default ("{}"). The detailed description guides users on how to provide the Lambda function source and extra IAM policies, aligning well with the PR objectives. For further clarity, consider adding a small example (or a reference to one) that illustrates how additional_policy can be used in practice.

🧰 Tools
🪛 LanguageTool

[grammar] ~77-~77: The verb ‘keep’ seems to be in the wrong form here.
Context: ... for unlimited length.
Set to null for keep the existing setting, which defaults to...

(FOR_VB)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 489515e and fb59c1d.

📒 Files selected for processing (1)
  • modules/lambda@edge/README.md (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Summary

@mihaiplesa
Copy link
Contributor

@goruha @Gowiem @jamengual can someone start the tests please? Thanks in advance.

@jamengual
Copy link

/terratest

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.

Ability to attach a custom iAM policy to lambdas
3 participants