Skip to content

Commit 8a17d34

Browse files
authored
Merge pull request #28 from byu-oit/enh/multipleCollections
Enh/multiple collections
2 parents 70692dd + a38cd68 commit 8a17d34

12 files changed

+297
-154
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44
pull_request:
55
branches: [master]
66
env:
7-
tf_version: "0.12.26" # must match value in examples/ci/postman-api-example.tf
7+
tf_version: "0.12.26" # must match value in examples/ci/advanced-example.tf
88
TF_IN_AUTOMATION: true
99

1010
jobs:
@@ -24,7 +24,7 @@ jobs:
2424
"tf_plan_extra_args":""
2525
},
2626
{
27-
"tf_working_dir":"./examples/postman-api",
27+
"tf_working_dir":"./examples/advanced",
2828
"aws_key_name":"byu_oit_terraform_dev_key",
2929
"aws_secret_name":"byu_oit_terraform_dev_secret",
3030
"tf_plan_extra_args":"-var 'postman_api_key=fake_api_key'"

README.md

Lines changed: 73 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
![Latest GitHub Release](https://img.shields.io/github/v/release/byu-oit/terraform-aws-postman-test-lambda?sort=semver)
22

33
# Terraform AWS Postman Test Lambda
4+
45
Terraform module that creates a generic lambda function that runs newman tests against a postman collection.
56

67
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).
@@ -10,38 +11,34 @@ This lambda function will tell CodeDeploy if the tests pass or fail.
1011
#### [New to Terraform Modules at BYU?](https://github.com/byu-oit/terraform-documentation)
1112

1213
## Usage
13-
You can provide a postman collection and environment to be tested in one of two ways:
14-
1. Provided in your github repo
15-
```hcl
16-
module "postman_test_lambda" {
17-
source = "github.com/byu-oit/terraform-aws-postman-test-lambda?ref=v2.5.0"
18-
app_name = "simple-example"
19-
postman_collection_file = "terraform-aws-postman-test-lambda-example.postman_collection.json"
20-
postman_environment_file = "terraform-aws-postman-test-lambda-env.postman_environment.json"
21-
role_permissions_boundary_arn = data.aws_ssm_parameter.role_permissions_boundary_arn.value
22-
}
23-
```
24-
2. Or from the Postman API
25-
```hcl
26-
module "postman_test_lambda" {
27-
source = "github.com/byu-oit/terraform-aws-postman-test-lambda?ref=v2.4.0"
28-
app_name = "simple-example"
29-
postman_collection_name = "terraform-aws-postman-test-lambda-example"
30-
postman_environment_name = "terraform-aws-postman-test-lambda-env"
31-
postman_api_key = var.postman_api_key
32-
role_permissions_boundary_arn = data.aws_ssm_parameter.role_permissions_boundary_arn.value
14+
15+
```hcl
16+
module "postman_test_lambda" {
17+
source = "github.com/byu-oit/terraform-aws-postman-test-lambda?ref=v3.0.0"
18+
app_name = "simple-example"
19+
postman_collections = [
20+
{
21+
collection = "terraform-aws-postman-test-lambda-example.postman_collection.json"
22+
environment = "terraform-aws-postman-test-lambda-env.postman_environment.json"
3323
}
34-
```
35-
Using this method allows you to not have to export your collection and commit the JSON file to your repo.
36-
37-
**Note:** The postman collection/environment must be viewable by the postman account tied to the API key you provide.
38-
39-
**Note 2:** Make sure your postman collection/environment names are unique, otherwise you will get an error if the postman API finds more than 1 collection/environment with the same name.
40-
41-
**DON'T** hard code your postman API key, treat it like all other secrets.
42-
43-
Then add your lambda function_name to the CodeDeploy lifecycle hook you want the postman tests to run on.
44-
For instance, if you're using the [fargate-api module](https://github.com/byu-oit/terraform-aws-fargate-api):
24+
]
25+
role_permissions_boundary_arn = data.aws_ssm_parameter.role_permissions_boundary_arn.value
26+
}
27+
```
28+
29+
You can specify multiple collections and environments to run in the lambda function. The function will run the
30+
collections in order.
31+
32+
You can run collections/environments from local json files or using the [Postman API](#using-the-postman-api).
33+
34+
**Note:** When [using the Postman API](#using-the-postman-api): the postman collections/environments must be viewable by
35+
the postman account tied to the API key you provide.
36+
37+
**DON'T** hard code your postman API key, treat it like all other secrets.
38+
39+
Then add your lambda function_name to the CodeDeploy lifecycle hook you want the postman tests to run on. For instance,
40+
if you're using the [fargate-api module](https://github.com/byu-oit/terraform-aws-fargate-api):
41+
4542
```hcl
4643
# ... postman-test-lambda module
4744
@@ -57,7 +54,9 @@ module "fargate_api" {
5754
}
5855
}
5956
```
57+
6058
Or if you're using the [lambda-api module](https://github.com/byu-oit/terraform-aws-lambda-api):
59+
6160
```hcl
6261
# ... postman-test-lambda module
6362
@@ -71,19 +70,47 @@ module "lambda_api" {
7170
}
7271
```
7372

73+
### Using the Postman API
74+
75+
If you don't want to export your postman collections/environments into json files in order to run tests you can use the
76+
Postman API. Using the Postman API allows you to keep your postman collections/environments in Postman and not have to
77+
worry about keeping json files up to date.
78+
79+
In order to use the Postman API to retrieve the collections/environments you will need to provide the `postman_api_key`.
80+
You can [generate an API key](https://learning.postman.com/docs/developer/intro-api/) from a Postman account.
81+
**PLEASE DON'T** hardcode the api key into your github repo.
82+
83+
Provide the collection and environment IDs instead of the name of each. You can find the ID on the v8 Postman Client by
84+
selecting your collection/environment and clicking on the info icon.
85+
86+
```hcl
87+
module "postman_test_lambda" {
88+
source = "github.com/byu-oit/terraform-aws-postman-test-lambda?ref=v3.0.0"
89+
app_name = "from-postman-api-example"
90+
postman_collections = [
91+
{
92+
collection = "1117094-d4bd5a5f-c37c-4fe9-8723-3c3e8b1e2015" # terraform-aws-postman-test-lambda-example collection from postman TF Modules and HW Examples workspace
93+
environment = "1117094-95627910-aeb0-4aed-b959-7e2034e2f6ce" # terraform-aws-postman-test-lambda-env environment from postman TF Modules and HW Examples workspace
94+
}
95+
]
96+
postman_api_key = var.postman_api_key
97+
role_permissions_boundary_arn = data.aws_ssm_parameter.role_permissions_boundary_arn.value
98+
}
99+
```
100+
74101
## Requirements
102+
75103
* Terraform version 0.12.16 or greater
104+
* _Postman JSON collections/environments files (optional)_ if you want export them to JSON files and include them in your project repo
105+
* _Postman API (optional)_ if you want to download Postman collections/environments from Postman instead of providing the json files in your repo
76106

77107
## Inputs
108+
78109
| Name | Type | Description | Default |
79110
| ----------------------------- | ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
80111
| app_name | string | Application name to prefix your postman test lambda function's name | |
81-
| postman_collection_file | string | Path to the postman collection JSON file relative from terraform dir (must be provided with postman_environment_file) | null |
82-
| postman_environment_file | string | Path to the postman environment JSON file relative from terraform dir (must be provided with postman_collection_file) | null |
83-
| postman_files_bucket_name | string | S3 Bucket name for the S3 Bucket this module will upload the postman_collection_file and postman_environment_file to | <app_name>-postman-files |
84-
| postman_collection_name | string | Name of Postman collection to download from Postman API (must be provided with postman_api_key and postman_environment_name) | null |
85-
| postman_environment_name | string | Name of Postman environment to download from Postman API (must be provided with postman_api_key and postman_collection_name) | null |
86-
| postman_api_key | string | postman API key to download collection and environment from Postman API (must be provided with postman_collection_name and postman_environment_name) | null |
112+
| postman_collections | list([object](#postman_collection))| List of postman collections and environments. See [postman_collection](#postman_collection) | |
113+
| postman_api_key | string | Postman API key to download collections/environments from Postman API (must be provided if you provide any postman IDs in `postman_collection` variable) | null |
87114
| role_permissions_boundary_arn | string | ARN of the IAM Role permissions boundary to place on each IAM role created | |
88115
| log_retention_in_days | number | CloudWatch log group retention in days | 7 |
89116
| tags | map(string) | A map of AWS Tags to attach to each resource created | {} |
@@ -92,7 +119,13 @@ module "lambda_api" {
92119
| 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 |
93120
| 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) | [] |
94121

122+
### postman_collection
123+
Object defining the collection and environment to run.
124+
* **`collection`** - (Required) path to local collection json file or Postman collection ID
125+
* **`environment`** - (Optional) path to local environment json file or Postman environment ID (can be set to `null` if you don't want an environment on your postman collection)
126+
95127
## Outputs
128+
96129
| Name | Type | Description |
97130
| --------------- | ------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------- |
98131
| lambda_function | [object](https://www.terraform.io/docs/providers/aws/r/lambda_function.html#attributes-reference) | Created lambda function that runs newman to test the `postman_collection` |
@@ -102,8 +135,10 @@ module "lambda_api" {
102135
| 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. |
103136

104137
## Contributing
138+
105139
To contribute to this terraform module make a feature branch and create a Pull Request to the `master` branch.
106140

107141
This terraform module bakes in the lambda function code in the committed [function.zip](lambda/dist/function.zip) file.
108142

109-
If you change the [index.js](lambda/src/index.js) file then you'll need to run `npm run package` and commit the [function.zip](lambda/dist/function.zip) file.
143+
If you change the [index.js](lambda/src/index.js) file then you'll need to run `npm run package` and commit
144+
the [function.zip](lambda/dist/function.zip) file.

examples/advanced/advanced-example.tf

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
provider "aws" {
2+
version = "~> 3.0"
3+
region = "us-west-2"
4+
}
5+
6+
variable "postman_api_key" {
7+
type = string
8+
}
9+
10+
data aws_ssm_parameter role_permissions_boundary_arn {
11+
name = "/acs/iam/iamRolePermissionBoundary"
12+
}
13+
data "aws_ssm_parameter" "vpc_name" {
14+
name = "/acs/vpc/vpc-name"
15+
}
16+
data "aws_ssm_parameter" "vpc_id" {
17+
name = "/acs/vpc/${data.aws_ssm_parameter.vpc_name.value}"
18+
}
19+
data "aws_ssm_parameter" "subnet_id" {
20+
name = "/acs/vpc/${data.aws_ssm_parameter.vpc_name.value}-private-a"
21+
}
22+
23+
module "postman_test_lambda" {
24+
source = "../../"
25+
app_name = "advanced-example"
26+
postman_collections = [
27+
{
28+
collection = "1117094-d4bd5a5f-c37c-4fe9-8723-3c3e8b1e2015" # terraform-aws-postman-test-lambda-example collection from postman TF Modules and HW Examples workspace
29+
environment = "1117094-95627910-aeb0-4aed-b959-7e2034e2f6ce" # terraform-aws-postman-test-lambda-env environment from postman TF Modules and HW Examples workspace
30+
},
31+
{
32+
collection = "terraform-aws-postman-test-lambda-example.postman_collection.json"
33+
environment = "terraform-aws-postman-test-lambda-env.postman_environment.json"
34+
}
35+
]
36+
postman_api_key = var.postman_api_key
37+
role_permissions_boundary_arn = data.aws_ssm_parameter.role_permissions_boundary_arn.value
38+
vpc_id = data.aws_ssm_parameter.vpc_id.value
39+
vpc_subnet_ids = [data.aws_ssm_parameter.subnet_id.value]
40+
memory_size = 528
41+
timeout = 120
42+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"id": "49389826-c016-4ce5-b91d-5d70df654292",
3+
"name": "terraform-aws-postman-test-lambda-env",
4+
"values": [
5+
{
6+
"key": "foo",
7+
"value": "bar",
8+
"enabled": true
9+
}
10+
],
11+
"_postman_variable_scope": "environment",
12+
"_postman_exported_at": "2020-07-17T18:36:40.432Z",
13+
"_postman_exported_using": "Postman/7.28.0"
14+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{
2+
"info": {
3+
"_postman_id": "f49e7563-d035-4164-baa9-0e77af3c6221",
4+
"name": "terraform-aws-postman-test-lambda-example",
5+
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
6+
},
7+
"item": [
8+
{
9+
"name": "Get",
10+
"event": [
11+
{
12+
"listen": "test",
13+
"script": {
14+
"id": "24c58423-6eb9-4ce5-9d28-b579702c91ef",
15+
"exec": [
16+
"pm.test(\"Status test\", function () {\r",
17+
" pm.response.to.have.status(200);\r",
18+
"});"
19+
],
20+
"type": "text/javascript"
21+
}
22+
}
23+
],
24+
"request": {
25+
"method": "GET",
26+
"header": [],
27+
"url": {
28+
"raw": "https://postman-echo.com/get",
29+
"protocol": "https",
30+
"host": [
31+
"postman-echo",
32+
"com"
33+
],
34+
"path": [
35+
"get"
36+
]
37+
}
38+
},
39+
"response": []
40+
}
41+
],
42+
"event": [
43+
{
44+
"listen": "prerequest",
45+
"script": {
46+
"id": "9b9fbc0f-a0c8-4154-93bb-2af32f6e4e66",
47+
"type": "text/javascript",
48+
"exec": [
49+
""
50+
]
51+
}
52+
},
53+
{
54+
"listen": "test",
55+
"script": {
56+
"id": "67ead005-33ee-4e55-8469-2d94edbf5072",
57+
"type": "text/javascript",
58+
"exec": [
59+
""
60+
]
61+
}
62+
}
63+
],
64+
"protocolProfileBehavior": {}
65+
}

examples/postman-api/postman-api-example.tf

Lines changed: 0 additions & 21 deletions
This file was deleted.

examples/simple/simple-example.tf

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ data aws_ssm_parameter role_permissions_boundary_arn {
88
}
99

1010
module "postman_test_lambda" {
11-
source = "../../"
12-
app_name = "simple-example"
13-
postman_collection_file = "terraform-aws-postman-test-lambda-example.postman_collection.json"
14-
postman_environment_file = "terraform-aws-postman-test-lambda-env.postman_environment.json"
11+
source = "../../"
12+
app_name = "simple-example"
13+
postman_collections = [
14+
{
15+
collection = "terraform-aws-postman-test-lambda-example.postman_collection.json"
16+
environment = null
17+
}
18+
]
1519
role_permissions_boundary_arn = data.aws_ssm_parameter.role_permissions_boundary_arn.value
1620
}

lambda/dist/function.zip

60.7 KB
Binary file not shown.

lambda/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postman-test-lambda",
3-
"version": "1.0.0",
3+
"version": "2.0.0",
44
"description": "Lambda function that runs postman collection tests",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)