Skip to content
This repository was archived by the owner on Jun 8, 2022. It is now read-only.

Commit f9ff6ee

Browse files
New feature - Additional trust relashionships (#59)
* Adding cloudwatch event role for trust relationship lambda * Adding assume_roles * Adding few comments and finalizing * Removing useless comments * Updating README.md + adding tests * Update variables.tf * Update iam.tf * Update README.md * Update main.tf
1 parent ace2bc9 commit f9ff6ee

File tree

5 files changed

+122
-1
lines changed

5 files changed

+122
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ module "lambda" {
3838
// Specify a file or directory for the source code.
3939
source_path = "${path.module}/lambda.py"
4040

41+
// Add additional trusted entities for assuming roles (trust relationships).
42+
trusted_entities = ["events.amazonaws.com", "s3.amazonaws.com"]
43+
4144
// Attach a policy.
4245
policy = {
4346
json = data.aws_iam_policy_document.lambda.json
@@ -75,6 +78,7 @@ Inputs for this module are the same as the [aws_lambda_function](https://www.ter
7578
| cloudwatch\_logs | Set this to false to disable logging your Lambda output to CloudWatch Logs | `bool` | `true` | no |
7679
| lambda\_at\_edge | Set this to true if using Lambda@Edge, to enable publishing, limit the timeout, and allow edgelambda.amazonaws.com to invoke the function | `bool` | `false` | no |
7780
| policy | An additional policy to attach to the Lambda function role | `object({json=string})` | | no |
81+
| trusted\_entities | Additional trusted entities for the Lambda function. The lambda.amazonaws.com (and edgelambda.amazonaws.com if lambda\_at\_edge is true) is always set | `list(string)` | | no |
7882

7983
The following arguments from the [aws_lambda_function](https://www.terraform.io/docs/providers/aws/r/lambda_function.html) resource are not supported:
8084

iam.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ data "aws_iam_policy_document" "assume_role" {
77

88
principals {
99
type = "Service"
10-
identifiers = slice(list("lambda.amazonaws.com", "edgelambda.amazonaws.com"), 0, var.lambda_at_edge ? 2 : 1)
10+
identifiers = concat(slice(list("lambda.amazonaws.com", "edgelambda.amazonaws.com"), 0, var.lambda_at_edge ? 2 : 1), var.trusted_entities)
1111
}
1212
}
1313
}

tests/assume-roles/lambda.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def lambda_handler(event, context):
2+
if event['pass']:
3+
return True
4+
else:
5+
raise Exception('oh no')

tests/assume-roles/main.tf

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
terraform {
2+
backend "local" {
3+
path = "terraform.tfstate"
4+
}
5+
}
6+
7+
provider "aws" {
8+
region = "eu-west-1"
9+
}
10+
11+
resource "random_id" "name" {
12+
byte_length = 6
13+
prefix = "terraform-aws-lambda-policy-"
14+
}
15+
16+
resource "aws_sqs_queue" "test" {
17+
name = random_id.name.hex
18+
}
19+
20+
data "aws_iam_policy_document" "computed" {
21+
statement {
22+
effect = "Allow"
23+
24+
actions = [
25+
"sqs:SendMessage",
26+
]
27+
28+
resources = [
29+
aws_sqs_queue.test.arn,
30+
]
31+
}
32+
}
33+
34+
data "aws_iam_policy_document" "known" {
35+
statement {
36+
effect = "Deny"
37+
38+
actions = [
39+
"sqs:SendMessage",
40+
]
41+
42+
resources = [
43+
"*",
44+
]
45+
}
46+
}
47+
48+
module "lambda_with_computed_policy_add_trust_relationships" {
49+
source = "../../"
50+
51+
function_name = "${random_id.name.hex}-computed"
52+
description = "Test attaching policy with additional trust relationships in terraform-aws-lambda"
53+
handler = "lambda.lambda_handler"
54+
runtime = "python3.6"
55+
56+
source_path = "${path.module}/lambda.py"
57+
58+
trusted_entities = ["events.amazonaws.com"]
59+
60+
policy = {
61+
json = data.aws_iam_policy_document.computed.json
62+
}
63+
}
64+
65+
66+
module "lambda_with_known_policy_add_trust_relationships" {
67+
source = "../../"
68+
69+
function_name = "${random_id.name.hex}-known"
70+
description = "Test attaching policy with additional trust relationships in terraform-aws-lambda"
71+
handler = "lambda.lambda_handler"
72+
runtime = "python3.6"
73+
74+
source_path = "${path.module}/lambda.py"
75+
76+
trusted_entities = ["events.amazonaws.com"]
77+
78+
policy = {
79+
json = data.aws_iam_policy_document.known.json
80+
}
81+
}
82+
83+
84+
module "lambda_without_policy_add_trust_relationships" {
85+
source = "../../"
86+
87+
function_name = "${random_id.name.hex}-without"
88+
description = "Test attaching policy with additional trust relationships in terraform-aws-lambda"
89+
handler = "lambda.lambda_handler"
90+
runtime = "python3.6"
91+
92+
source_path = "${path.module}/lambda.py"
93+
94+
trusted_entities = ["events.amazonaws.com"]
95+
}
96+
97+
module "lambda_without_policy_without_added_trust_relationships" {
98+
source = "../../"
99+
100+
function_name = "${random_id.name.hex}-without"
101+
description = "Test attaching policy with additional trust relationships in terraform-aws-lambda"
102+
handler = "lambda.lambda_handler"
103+
runtime = "python3.6"
104+
105+
source_path = "${path.module}/lambda.py"
106+
}

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ variable "policy" {
5151
default = null
5252
}
5353

54+
variable "trusted_entities" {
55+
description = "Lambda function additional trusted entities for assuming roles (trust relationship)"
56+
type = list(string)
57+
default = []
58+
}
59+
5460
locals {
5561
publish = var.lambda_at_edge ? true : var.publish
5662
timeout = var.lambda_at_edge ? min(var.timeout, 5) : var.timeout

0 commit comments

Comments
 (0)