Skip to content

Commit

Permalink
Merge pull request #28 from byu-oit/enh/multipleCollections
Browse files Browse the repository at this point in the history
Enh/multiple collections
  • Loading branch information
yoshutch authored Mar 31, 2021
2 parents 70692dd + a38cd68 commit 8a17d34
Show file tree
Hide file tree
Showing 12 changed files with 297 additions and 154 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
pull_request:
branches: [master]
env:
tf_version: "0.12.26" # must match value in examples/ci/postman-api-example.tf
tf_version: "0.12.26" # must match value in examples/ci/advanced-example.tf
TF_IN_AUTOMATION: true

jobs:
Expand All @@ -24,7 +24,7 @@ jobs:
"tf_plan_extra_args":""
},
{
"tf_working_dir":"./examples/postman-api",
"tf_working_dir":"./examples/advanced",
"aws_key_name":"byu_oit_terraform_dev_key",
"aws_secret_name":"byu_oit_terraform_dev_secret",
"tf_plan_extra_args":"-var 'postman_api_key=fake_api_key'"
Expand Down
111 changes: 73 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
![Latest GitHub Release](https://img.shields.io/github/v/release/byu-oit/terraform-aws-postman-test-lambda?sort=semver)

# Terraform AWS Postman Test Lambda

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

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

## Usage
You can provide a postman collection and environment to be tested in one of two ways:
1. Provided in your github repo
```hcl
module "postman_test_lambda" {
source = "github.com/byu-oit/terraform-aws-postman-test-lambda?ref=v2.5.0"
app_name = "simple-example"
postman_collection_file = "terraform-aws-postman-test-lambda-example.postman_collection.json"
postman_environment_file = "terraform-aws-postman-test-lambda-env.postman_environment.json"
role_permissions_boundary_arn = data.aws_ssm_parameter.role_permissions_boundary_arn.value
}
```
2. Or from the Postman API
```hcl
module "postman_test_lambda" {
source = "github.com/byu-oit/terraform-aws-postman-test-lambda?ref=v2.4.0"
app_name = "simple-example"
postman_collection_name = "terraform-aws-postman-test-lambda-example"
postman_environment_name = "terraform-aws-postman-test-lambda-env"
postman_api_key = var.postman_api_key
role_permissions_boundary_arn = data.aws_ssm_parameter.role_permissions_boundary_arn.value

```hcl
module "postman_test_lambda" {
source = "github.com/byu-oit/terraform-aws-postman-test-lambda?ref=v3.0.0"
app_name = "simple-example"
postman_collections = [
{
collection = "terraform-aws-postman-test-lambda-example.postman_collection.json"
environment = "terraform-aws-postman-test-lambda-env.postman_environment.json"
}
```
Using this method allows you to not have to export your collection and commit the JSON file to your repo.
**Note:** The postman collection/environment must be viewable by the postman account tied to the API key you provide.
**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.
**DON'T** hard code your postman API key, treat it like all other secrets.
Then add your lambda function_name to the CodeDeploy lifecycle hook you want the postman tests to run on.
For instance, if you're using the [fargate-api module](https://github.com/byu-oit/terraform-aws-fargate-api):
]
role_permissions_boundary_arn = data.aws_ssm_parameter.role_permissions_boundary_arn.value
}
```

You can specify multiple collections and environments to run in the lambda function. The function will run the
collections in order.

You can run collections/environments from local json files or using the [Postman API](#using-the-postman-api).

**Note:** When [using the Postman API](#using-the-postman-api): the postman collections/environments must be viewable by
the postman account tied to the API key you provide.

**DON'T** hard code your postman API key, treat it like all other secrets.

Then add your lambda function_name to the CodeDeploy lifecycle hook you want the postman tests to run on. For instance,
if you're using the [fargate-api module](https://github.com/byu-oit/terraform-aws-fargate-api):

```hcl
# ... postman-test-lambda module
Expand All @@ -57,7 +54,9 @@ module "fargate_api" {
}
}
```

Or if you're using the [lambda-api module](https://github.com/byu-oit/terraform-aws-lambda-api):

```hcl
# ... postman-test-lambda module
Expand All @@ -71,19 +70,47 @@ module "lambda_api" {
}
```

### Using the Postman API

If you don't want to export your postman collections/environments into json files in order to run tests you can use the
Postman API. Using the Postman API allows you to keep your postman collections/environments in Postman and not have to
worry about keeping json files up to date.

In order to use the Postman API to retrieve the collections/environments you will need to provide the `postman_api_key`.
You can [generate an API key](https://learning.postman.com/docs/developer/intro-api/) from a Postman account.
**PLEASE DON'T** hardcode the api key into your github repo.

Provide the collection and environment IDs instead of the name of each. You can find the ID on the v8 Postman Client by
selecting your collection/environment and clicking on the info icon.

```hcl
module "postman_test_lambda" {
source = "github.com/byu-oit/terraform-aws-postman-test-lambda?ref=v3.0.0"
app_name = "from-postman-api-example"
postman_collections = [
{
collection = "1117094-d4bd5a5f-c37c-4fe9-8723-3c3e8b1e2015" # terraform-aws-postman-test-lambda-example collection from postman TF Modules and HW Examples workspace
environment = "1117094-95627910-aeb0-4aed-b959-7e2034e2f6ce" # terraform-aws-postman-test-lambda-env environment from postman TF Modules and HW Examples workspace
}
]
postman_api_key = var.postman_api_key
role_permissions_boundary_arn = data.aws_ssm_parameter.role_permissions_boundary_arn.value
}
```

## Requirements

* Terraform version 0.12.16 or greater
* _Postman JSON collections/environments files (optional)_ if you want export them to JSON files and include them in your project repo
* _Postman API (optional)_ if you want to download Postman collections/environments from Postman instead of providing the json files in your repo

## Inputs

| Name | Type | Description | Default |
| ----------------------------- | ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
| app_name | string | Application name to prefix your postman test lambda function's name | |
| postman_collection_file | string | Path to the postman collection JSON file relative from terraform dir (must be provided with postman_environment_file) | null |
| postman_environment_file | string | Path to the postman environment JSON file relative from terraform dir (must be provided with postman_collection_file) | null |
| 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 |
| 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 |
| 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 |
| 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 |
| postman_collections | list([object](#postman_collection))| List of postman collections and environments. See [postman_collection](#postman_collection) | |
| 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 |
| role_permissions_boundary_arn | string | ARN of the IAM Role permissions boundary to place on each IAM role created | |
| log_retention_in_days | number | CloudWatch log group retention in days | 7 |
| tags | map(string) | A map of AWS Tags to attach to each resource created | {} |
Expand All @@ -92,7 +119,13 @@ module "lambda_api" {
| 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 |
| 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) | [] |

### postman_collection
Object defining the collection and environment to run.
* **`collection`** - (Required) path to local collection json file or Postman collection ID
* **`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)

## Outputs

| Name | Type | Description |
| --------------- | ------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------- |
| 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` |
Expand All @@ -102,8 +135,10 @@ module "lambda_api" {
| 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. |

## Contributing

To contribute to this terraform module make a feature branch and create a Pull Request to the `master` branch.

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

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.
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.
42 changes: 42 additions & 0 deletions examples/advanced/advanced-example.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
provider "aws" {
version = "~> 3.0"
region = "us-west-2"
}

variable "postman_api_key" {
type = string
}

data aws_ssm_parameter role_permissions_boundary_arn {
name = "/acs/iam/iamRolePermissionBoundary"
}
data "aws_ssm_parameter" "vpc_name" {
name = "/acs/vpc/vpc-name"
}
data "aws_ssm_parameter" "vpc_id" {
name = "/acs/vpc/${data.aws_ssm_parameter.vpc_name.value}"
}
data "aws_ssm_parameter" "subnet_id" {
name = "/acs/vpc/${data.aws_ssm_parameter.vpc_name.value}-private-a"
}

module "postman_test_lambda" {
source = "../../"
app_name = "advanced-example"
postman_collections = [
{
collection = "1117094-d4bd5a5f-c37c-4fe9-8723-3c3e8b1e2015" # terraform-aws-postman-test-lambda-example collection from postman TF Modules and HW Examples workspace
environment = "1117094-95627910-aeb0-4aed-b959-7e2034e2f6ce" # terraform-aws-postman-test-lambda-env environment from postman TF Modules and HW Examples workspace
},
{
collection = "terraform-aws-postman-test-lambda-example.postman_collection.json"
environment = "terraform-aws-postman-test-lambda-env.postman_environment.json"
}
]
postman_api_key = var.postman_api_key
role_permissions_boundary_arn = data.aws_ssm_parameter.role_permissions_boundary_arn.value
vpc_id = data.aws_ssm_parameter.vpc_id.value
vpc_subnet_ids = [data.aws_ssm_parameter.subnet_id.value]
memory_size = 528
timeout = 120
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"id": "49389826-c016-4ce5-b91d-5d70df654292",
"name": "terraform-aws-postman-test-lambda-env",
"values": [
{
"key": "foo",
"value": "bar",
"enabled": true
}
],
"_postman_variable_scope": "environment",
"_postman_exported_at": "2020-07-17T18:36:40.432Z",
"_postman_exported_using": "Postman/7.28.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"info": {
"_postman_id": "f49e7563-d035-4164-baa9-0e77af3c6221",
"name": "terraform-aws-postman-test-lambda-example",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Get",
"event": [
{
"listen": "test",
"script": {
"id": "24c58423-6eb9-4ce5-9d28-b579702c91ef",
"exec": [
"pm.test(\"Status test\", function () {\r",
" pm.response.to.have.status(200);\r",
"});"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "https://postman-echo.com/get",
"protocol": "https",
"host": [
"postman-echo",
"com"
],
"path": [
"get"
]
}
},
"response": []
}
],
"event": [
{
"listen": "prerequest",
"script": {
"id": "9b9fbc0f-a0c8-4154-93bb-2af32f6e4e66",
"type": "text/javascript",
"exec": [
""
]
}
},
{
"listen": "test",
"script": {
"id": "67ead005-33ee-4e55-8469-2d94edbf5072",
"type": "text/javascript",
"exec": [
""
]
}
}
],
"protocolProfileBehavior": {}
}
21 changes: 0 additions & 21 deletions examples/postman-api/postman-api-example.tf

This file was deleted.

12 changes: 8 additions & 4 deletions examples/simple/simple-example.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ data aws_ssm_parameter role_permissions_boundary_arn {
}

module "postman_test_lambda" {
source = "../../"
app_name = "simple-example"
postman_collection_file = "terraform-aws-postman-test-lambda-example.postman_collection.json"
postman_environment_file = "terraform-aws-postman-test-lambda-env.postman_environment.json"
source = "../../"
app_name = "simple-example"
postman_collections = [
{
collection = "terraform-aws-postman-test-lambda-example.postman_collection.json"
environment = null
}
]
role_permissions_boundary_arn = data.aws_ssm_parameter.role_permissions_boundary_arn.value
}
Binary file modified lambda/dist/function.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion lambda/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postman-test-lambda",
"version": "1.0.0",
"version": "2.0.0",
"description": "Lambda function that runs postman collection tests",
"repository": {
"type": "git",
Expand Down
Loading

0 comments on commit 8a17d34

Please sign in to comment.