Skip to content

Commit ffa3b59

Browse files
authored
feat: Added support for policy_jsons (list of strings) (claranet#58)
1 parent 0e83a2b commit ffa3b59

File tree

6 files changed

+78
-11
lines changed

6 files changed

+78
-11
lines changed

.pre-commit-config.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
repos:
22
- repo: git://github.com/antonbabenko/pre-commit-terraform
3-
rev: v1.31.0
3+
rev: v1.39.0
44
hooks:
55
- id: terraform_fmt
66
- id: terraform_docs
77
- repo: git://github.com/pre-commit/pre-commit-hooks
8-
rev: v3.1.0
8+
rev: v3.2.0
99
hooks:
1010
- id: check-merge-conflict

README.md

+15-7
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,13 @@ module "vpc" {
240240

241241
## Additional IAM policies for Lambda Functions
242242

243-
There are 4 supported ways to attach IAM policies to IAM role used by Lambda Function:
243+
There are 5 supported ways to attach IAM policies to IAM role used by Lambda Function:
244244

245-
1. `policy_json` - set as JSON string or heredoc, when `attach_policy_json = true`.
245+
1. `policy_json` - JSON string or heredoc, when `attach_policy_json = true`.
246+
1. `policy_jsons` - List of JSON strings or heredoc, when `attach_policy_jsons = true` and `number_of_policy_jsons > 0`.
246247
1. `policy` - ARN of existing IAM policy, when `attach_policy = true`.
247-
1. `policies` - List of ARNs of existing IAM policies, when `attach_policies = true`.
248-
1. `policy_statements` - Map of maps to define IAM statements which will be generated as IAM policy. Requires `attach_policy_statements = true`. See examples/complete for more information.
248+
1. `policies` - List of ARNs of existing IAM policies, when `attach_policies = true` and `number_of_policies > 0`.
249+
1. `policy_statements` - Map of maps to define IAM statements which will be generated as IAM policy. Requires `attach_policy_statements = true`. See `examples/complete` for more information.
249250

250251

251252
## Lambda Permissions for allowed triggers
@@ -557,15 +558,19 @@ Q4: What does this error mean - `"We currently do not support adding policies fo
557558
|------|---------|
558559
| terraform | >= 0.12.6, < 0.14 |
559560
| aws | >= 2.67, < 4.0 |
561+
| external | ~> 1 |
562+
| local | ~> 1 |
563+
| null | ~> 2 |
564+
| random | ~> 2 |
560565

561566
## Providers
562567

563568
| Name | Version |
564569
|------|---------|
565570
| aws | >= 2.67, < 4.0 |
566-
| external | n/a |
567-
| local | n/a |
568-
| null | n/a |
571+
| external | ~> 1 |
572+
| local | ~> 1 |
573+
| null | ~> 2 |
569574

570575
## Inputs
571576

@@ -580,6 +585,7 @@ Q4: What does this error mean - `"We currently do not support adding policies fo
580585
| attach\_policies | Controls whether list of policies should be added to IAM role for Lambda Function | `bool` | `false` | no |
581586
| attach\_policy | Controls whether policy should be added to IAM role for Lambda Function | `bool` | `false` | no |
582587
| attach\_policy\_json | Controls whether policy\_json should be added to IAM role for Lambda Function | `bool` | `false` | no |
588+
| attach\_policy\_jsons | Controls whether policy\_jsons should be added to IAM role for Lambda Function | `bool` | `false` | no |
583589
| attach\_policy\_statements | Controls whether policy\_statements should be added to IAM role for Lambda Function | `bool` | `false` | no |
584590
| attach\_tracing\_policy | Controls whether X-Ray tracing policy should be added to IAM role for Lambda Function | `bool` | `false` | no |
585591
| build\_in\_docker | Whether to build dependencies in Docker | `bool` | `false` | no |
@@ -623,9 +629,11 @@ Q4: What does this error mean - `"We currently do not support adding policies fo
623629
| maximum\_retry\_attempts | Maximum number of times to retry when the function returns an error. Valid values between 0 and 2. Defaults to 2. | `number` | `null` | no |
624630
| memory\_size | Amount of memory in MB your Lambda Function can use at runtime. Valid value between 128 MB to 3008 MB, in 64 MB increments. | `number` | `128` | no |
625631
| number\_of\_policies | Number of policies to attach to IAM role for Lambda Function | `number` | `0` | no |
632+
| number\_of\_policy\_jsons | Number of policies JSON to attach to IAM role for Lambda Function | `number` | `0` | no |
626633
| policies | List of policy statements ARN to attach to Lambda Function role | `list(string)` | `[]` | no |
627634
| policy | An additional policy document ARN to attach to the Lambda Function role | `string` | `null` | no |
628635
| policy\_json | An additional policy document as JSON to attach to the Lambda Function role | `string` | `null` | no |
636+
| policy\_jsons | List of additional policy documents as JSON to attach to Lambda Function role | `list(string)` | `[]` | no |
629637
| policy\_statements | Map of dynamic policy statements to attach to Lambda Function role | `any` | `{}` | no |
630638
| provisioned\_concurrent\_executions | Amount of capacity to allocate. Set to 1 or greater to enable, or set to 0 to disable provisioned concurrency. | `number` | `-1` | no |
631639
| publish | Whether to publish creation/change as new Lambda Function Version. | `bool` | `false` | no |

examples/complete/main.tf

+19-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,24 @@ module "lambda_function" {
7676
}
7777
EOF
7878

79+
attach_policy_jsons = true
80+
policy_jsons = [<<EOF
81+
{
82+
"Version": "2012-10-17",
83+
"Statement": [
84+
{
85+
"Effect": "Allow",
86+
"Action": [
87+
"xray:*"
88+
],
89+
"Resource": ["*"]
90+
}
91+
]
92+
}
93+
EOF
94+
]
95+
number_of_policy_jsons = 1
96+
7997
attach_policy = true
8098
policy = "arn:aws:iam::aws:policy/AWSXRayDaemonWriteAccess"
8199

@@ -206,7 +224,7 @@ module "lambda_with_provisioned_concurrency" {
206224

207225
hash_extra = "hash-extra-lambda-provisioned"
208226

209-
provisioned_concurrent_executions = 2
227+
provisioned_concurrent_executions = -1 # 2
210228
}
211229

212230
###########

iam.tf

+19
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,25 @@ resource "aws_iam_policy_attachment" "additional_json" {
219219
policy_arn = aws_iam_policy.additional_json[0].arn
220220
}
221221

222+
#####################################
223+
# Additional policies (list of JSON)
224+
#####################################
225+
226+
resource "aws_iam_policy" "additional_jsons" {
227+
count = local.create_role && var.attach_policy_jsons ? var.number_of_policy_jsons : 0
228+
229+
name = "${var.function_name}-${count.index}"
230+
policy = var.policy_jsons[count.index]
231+
}
232+
233+
resource "aws_iam_policy_attachment" "additional_jsons" {
234+
count = local.create_role && var.attach_policy_jsons ? var.number_of_policy_jsons : 0
235+
236+
name = "${var.function_name}-${count.index}"
237+
roles = [aws_iam_role.lambda[0].name]
238+
policy_arn = aws_iam_policy.additional_jsons[count.index].arn
239+
}
240+
222241
###########################
223242
# ARN of additional policy
224243
###########################

variables.tf

+18
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,12 @@ variable "attach_policy_json" {
359359
default = false
360360
}
361361

362+
variable "attach_policy_jsons" {
363+
description = "Controls whether policy_jsons should be added to IAM role for Lambda Function"
364+
type = bool
365+
default = false
366+
}
367+
362368
variable "attach_policy" {
363369
description = "Controls whether policy should be added to IAM role for Lambda Function"
364370
type = bool
@@ -371,6 +377,12 @@ variable "attach_policies" {
371377
default = false
372378
}
373379

380+
variable "number_of_policy_jsons" {
381+
description = "Number of policies JSON to attach to IAM role for Lambda Function"
382+
type = number
383+
default = 0
384+
}
385+
374386
variable "number_of_policies" {
375387
description = "Number of policies to attach to IAM role for Lambda Function"
376388
type = number
@@ -395,6 +407,12 @@ variable "policy_json" {
395407
default = null
396408
}
397409

410+
variable "policy_jsons" {
411+
description = "List of additional policy documents as JSON to attach to Lambda Function role"
412+
type = list(string)
413+
default = []
414+
}
415+
398416
variable "policy" {
399417
description = "An additional policy document ARN to attach to the Lambda Function role"
400418
type = string

versions.tf

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ terraform {
22
required_version = ">= 0.12.6, < 0.14"
33

44
required_providers {
5-
aws = ">= 2.67, < 4.0"
5+
aws = ">= 2.67, < 4.0"
6+
external = "~> 1"
7+
local = "~> 1"
8+
random = "~> 2"
9+
null = "~> 2"
610
}
711
}

0 commit comments

Comments
 (0)