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

BAU: reduce lamba policies #5604

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

BAU: reduce lamba policies #5604

wants to merge 3 commits into from

Conversation

whi-tw
Copy link
Contributor

@whi-tw whi-tw commented Nov 29, 2024

What

Previously, we created many policies, and then attached them all to the
individual roles.

This meant that we often bumped up against the 20 policy limit for a
role. It also took a long time to run the terraform apply, as it had to
check MANY policies for changes.

This change introduces a replacement for the lambda-role module, which
takes in a map of policy documents lists and combines them into a
reduced number of policies specific to the role.

eg.

module "lambda-role-policy-reduction" {
  source = "./modules/lambda-role-policy-reduction"

  role_name = "my-role"
  environment = "dev"
  default_tags = var.default_tags
  vpc_arn = var.vpc_arn

  policy_documents_to_attach = {
    "dynamodb" = [
      data.aws_iam_policy_document.dynamodb_write_policy.json,
      data.aws_iam_policy_document.dynamodb_read_policy.json,
    ],
    "s3" = [
      data.aws_iam_policy_document.s3_list_policy.json,
      data.aws_iam_policy_document.s3_read_policy.json,
      data.aws_iam_policy_document.s3_write_policy.json,
    ],
    "sqs" = [
      data.aws_iam_policy_document.sqs_policy.json,
    ],
    "redis" = [
      data.aws_iam_policy_document.redis_read_policy.json,
      data.aws_iam_policy_document.redis_write_policy.json,
    ],
  }
}

This would create and attach 5 policies:

  1. base: Contains the logging, networking and xray policies that are
    common to all roles.
  2. dynamodb: Contains the dynamodb policies
  3. s3: Contains the s3 policy
  4. sqs: Contains the sqs policy
  5. redis: Contains the redis policy

previously, this would have created and attached 11 policies to the role
ie. 1 more than the limit.

How to review

  • Code Review

Before Merging

  • remove POC implementation

@whi-tw whi-tw requested review from a team as code owners November 29, 2024 14:46
@whi-tw whi-tw force-pushed the BAU/reduce-lambda-policies branch from 4aedca9 to be0a2cc Compare November 29, 2024 16:16
Copy link

Java Tests Skipped

No Java files were changed in this pull request. Java tests will be skipped1.

Any Java files that are changed in a subsequent commit will trigger the Java tests.

Footnotes

  1. These tests will still show as passing in the PR status check, but will not actually have run.

Previously, we created many policies, and then attached them all to the
individual roles.

This meant that we often bumped up against the 20 policy limit for a
role. It also took a long time to run the terraform apply, as it had to
check MANY policies for changes.

This change introduces a replacement for the lambda-role module, which
takes in a map of policy documents lists and combines them into a
reduced number of policies specific to the role.

eg.

```hcl
module "lambda-role-policy-reduction" {
  source = "./modules/lambda-role-policy-reduction"

  role_name = "my-role"
  environment = "dev"
  default_tags = var.default_tags
  vpc_arn = var.vpc_arn

  policy_documents_to_attach = {
    "dynamodb" = [
      data.aws_iam_policy_document.dynamodb_write_policy.json,
      data.aws_iam_policy_document.dynamodb_read_policy.json,
    ],
    "s3" = [
      data.aws_iam_policy_document.s3_list_policy.json,
      data.aws_iam_policy_document.s3_read_policy.json,
      data.aws_iam_policy_document.s3_write_policy.json,
    ],
    "sqs" = [
      data.aws_iam_policy_document.sqs_policy.json,
    ],
    "redis" = [
      data.aws_iam_policy_document.redis_read_policy.json,
      data.aws_iam_policy_document.redis_write_policy.json,
    ],
  }
}
```
This would create and attach 5 policies:
1. base: Contains the logging, networking and xray policies that are
   common to all roles.
2. dynamodb: Contains the dynamodb policies
3. s3: Contains the s3 policy
4. sqs: Contains the sqs policy
5. redis: Contains the redis policy

previously, this would have created and attached 11 policies to the role
ie. 1 more than the limit.
This is a proof of concept to show how the new lambda role module is
used.
@whi-tw whi-tw force-pushed the BAU/reduce-lambda-policies branch from be0a2cc to 5a85b5b Compare November 29, 2024 16:24
@whi-tw whi-tw added the DO NOT MERGE Should not be merged. label Dec 3, 2024
error_message = "policy_documents_to_attach cannot contain a key named 'base'"
}
validation {
condition = length(keys(var.policy_documents_to_attach)) <= 20
Copy link
Contributor

Choose a reason for hiding this comment

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

Off by 1 here. We already have 1 policy defined, base

Comment on lines +52 to +57
base = concat([
data.aws_iam_policy_document.logging_policy_document.json,
data.aws_iam_policy_document.endpoint_xray_policy.json,
],
var.vpc_arn == "" ? [] : [data.aws_iam_policy_document.networking_policy_document[0].json]),
}, var.policy_documents_to_attach)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do the order of these policies matter? i.e. if the json object keys are not returned in the same order, will that cause AWS/terraform to fail to deploy the change (see previous conversations about changes to policy ordering messing up a deployment)?


data "aws_iam_policy_document" "iam-policy-document" {
for_each = local.policy_documents_to_attach
source_policy_documents = each.value
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to worry about this?

Statements defined in source_policy_documents must have unique sids

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ethanmills thanks for taking a look - I was going to reach out for input.

Yeah, that's a super valid point I hadn't considered. If we go ahead with this approach, I'll add a validation step in to ensure uniqueness.

@ethanmills
Copy link
Member

What does the plan look like if I add a policy doc to e.g. the sqs list? Is it a modify in place or a delete and replace?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DO NOT MERGE Should not be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants