Skip to content

Commit 567a524

Browse files
committed
Merge branch 'master' into enh/multipleCollections
# Conflicts: # README.md
2 parents 8399dc2 + 70692dd commit 567a524

File tree

4 files changed

+74
-9
lines changed

4 files changed

+74
-9
lines changed

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55
Terraform module that creates a generic lambda function that runs newman tests against a postman collection.
66

7-
This lambda function is intended for use
8-
with [CodeDeploy's lifecycle hooks](https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file-structure-hooks.html)
9-
. This lambda function will attempt to run the [newman](https://www.npmjs.com/package/newman) CLI to run your Postman
10-
collection as a test. This lambda function will tell CodeDeploy if the tests pass or fail.
7+
This lambda function is intended for use with [CodeDeploy's lifecycle hooks](https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file-structure-hooks.html).
8+
This lambda function will attempt to run the [newman](https://www.npmjs.com/package/newman) CLI to run your Postman collection as a test.
9+
This lambda function will tell CodeDeploy if the tests pass or fail.
1110

1211
#### [New to Terraform Modules at BYU?](https://github.com/byu-oit/terraform-documentation)
1312

@@ -47,11 +46,11 @@ module "fargate_api" {
4746
source = "github.com/byu-oit/terraform-aws-fargate-api?ref=" # latest version
4847
# .. all other variables
4948
codedeploy_lifecycle_hooks = {
50-
BeforeInstall = null
51-
AfterInstall = null
49+
BeforeInstall = null
50+
AfterInstall = null
5251
AfterAllowTestTraffic = module.postman_test_lambda.lambda_function.function_name
53-
BeforeAllowTraffic = null
54-
AfterAllowTraffic = null
52+
BeforeAllowTraffic = null
53+
AfterAllowTraffic = null
5554
}
5655
}
5756
```
@@ -66,7 +65,7 @@ module "lambda_api" {
6665
# .. all other variables
6766
codedeploy_lifecycle_hooks = {
6867
BeforeAllowTraffic = module.postman_test_lambda.lambda_function.function_name
69-
AfterAllowTraffic = null
68+
AfterAllowTraffic = null
7069
}
7170
}
7271
```
@@ -117,6 +116,8 @@ module "postman_test_lambda" {
117116
| tags | map(string) | A map of AWS Tags to attach to each resource created | {} |
118117
| timeout | number | The max number of seconds the lambda will run for without stopping. | 30 |
119118
| memory_size | number | The size of the memory of the lambda | 128 |
119+
| vpc_id | string | The id of the VPC the lambda will be behind if VPC configuration is desired. (must be provided with lambda_vpc_subnet_ids) | null |
120+
| vpc_subnet_ids | list(string) | A list of subnet ids the lambda will be put in if VPC configuration is desired. (must be provided with vpc_id) | [] |
120121

121122
### postman_collection
122123
Object defining the collection and environment to run.
@@ -131,6 +132,7 @@ Object defining the collection and environment to run.
131132
| lambda_iam_role | [object](https://www.terraform.io/docs/providers/aws/r/iam_role.html#attributes-reference) | Created IAM role for the `lambda_function` |
132133
| postman_files_bucket | [object](https://www.terraform.io/docs/providers/aws/r/s3_bucket.html#attributes-reference) | Created S3 Bucket where local postman files are uploaded |
133134
| cloudwatch_log_group | [object](https://www.terraform.io/docs/providers/aws/r/cloudwatch_log_group.html#attributes-reference) | Created CloudWatch Log Group for the postman lambda logs |
135+
| lambda_security_group | [object](https://www.terraform.io/docs/providers/aws/r/security_group.html#attributes-reference) | Created security group for the lambda's VPC configuration. |
134136

135137
## Contributing
136138

main.tf

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ locals {
2929
POSTMAN_API_KEY = var.postman_api_key
3030
}
3131
lambda_function_name = "${var.app_name}-postman-tests"
32+
using_vpc_config = length(var.vpc_subnet_ids) > 0
3233
}
3334

3435
# -----------------------------------------------------------------------------
@@ -187,6 +188,11 @@ resource "aws_lambda_function" "test_lambda" {
187188
depends_on = [
188189
aws_cloudwatch_log_group.lambda_logs,
189190
]
191+
192+
vpc_config {
193+
subnet_ids = var.vpc_subnet_ids
194+
security_group_ids = local.using_vpc_config ? [aws_security_group.lambda_vpc_sg[0].id] : []
195+
}
190196
}
191197

192198
resource "aws_iam_role" "test_lambda" {
@@ -238,6 +244,47 @@ resource "aws_iam_role_policy" "test_lambda" {
238244
EOF
239245
}
240246

247+
# Role that allows lambda to create vpc config
248+
resource "aws_iam_role_policy" "lambda_vpc_policy" {
249+
count = local.using_vpc_config ? 1 : 0
250+
251+
name = "${var.app_name}-postman-tests-vpc-policy"
252+
role = aws_iam_role.test_lambda.name
253+
254+
policy = <<EOF
255+
{
256+
"Version": "2012-10-17",
257+
"Statement": [
258+
{
259+
"Action": [
260+
"ec2:CreateNetworkInterface",
261+
"ec2:DescribeNetworkInterfaces",
262+
"ec2:DeleteNetworkInterface"
263+
],
264+
"Effect": "Allow",
265+
"Resource": "*"
266+
}
267+
]
268+
}
269+
EOF
270+
}
271+
272+
# A bare minimum security group for doing vpc config.
273+
resource "aws_security_group" "lambda_vpc_sg" {
274+
count = local.using_vpc_config ? 1 : 0
275+
276+
name = "${var.app_name}-postman-tests"
277+
description = "Bare minimum security group for lambda."
278+
vpc_id = var.vpc_id
279+
280+
egress {
281+
from_port = 0
282+
to_port = 0
283+
protocol = "-1"
284+
cidr_blocks = ["0.0.0.0/0"]
285+
}
286+
}
287+
241288
resource "aws_cloudwatch_log_group" "lambda_logs" {
242289
name = "/aws/lambda/${local.lambda_function_name}"
243290
retention_in_days = var.log_retention_in_days

outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ output "postman_files_bucket" {
1313
output "cloudwatch_log_group" {
1414
value = aws_cloudwatch_log_group.lambda_logs
1515
}
16+
17+
output "lambda_security_group" {
18+
value = local.using_vpc_config ? aws_security_group.lambda_vpc_sg[0] : null
19+
}

variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,15 @@ variable "timeout" {
5151
description = "the amount of time the lambda is allowed to run for"
5252
default = 30
5353
}
54+
55+
variable "vpc_subnet_ids" {
56+
type = list(string)
57+
description = "Subnet ids that the lambda should be in."
58+
default = []
59+
}
60+
61+
variable "vpc_id" {
62+
type = string
63+
description = "ID for the lambda's VPC"
64+
default = null
65+
}

0 commit comments

Comments
 (0)