Setting up Automated API testing with Postman Collection using Newman CLI, AWS Codebuild, CodePipeline, & S3
Tags: newman, postman, newmancli, automation
Published: 2022-11-12 15:25:40

In this post you will learn how to automate your own build of postman collections with aws codecommit, codepipeline, and codebuild.

**Newman** is a command-line collection runner for Postman.

**Postman** is an API platform for building and using APIs.

**CodeCommit** is a secure, highly scalable, managed source control service that hosts private Git repositories.

**CodeBuild** compiles your source code, runs unit tests, and produces artifacts that are ready to deploy.

**CodePipeline** is a continuous delivery service you can use to model, visualize, and automate the steps required to release your software.

## File Structure
```
── /CodeCommitRepo/         # root folder
  ├── /buildspec.yml        # source code for build commands
  └── /api.postman_collection.json # postman collection
```

## Overview
1. Export Postman Collection
2. Create Buildspec.yml
3. Create CodeCommit Repository and push files 
4. Create S3 bucket for output report
5. Create a build project in AWS CodeBuild
6. Create pipeline in CodePipeline and build all together
 
### 1. Export Postman Collection

Open your postman collection and select the more actions icon More actions icon next to the collection, then select Export.

![export postman1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ru6xwb5opg6bbiin6fww.png)

Select the format you'd like your collection to export as then select export to download your newly generated JSON file.


![export postman2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uzs0hc50s4g0hy1mv3d6.png)

### 2. Create Buildspec.yml
Copy the code below to `buildspec.yml`. Make sure to place your file along with your exported postman collection.

``` yml
version: 0.2

phases:
  pre_build:
    commands:
      - npm install -g newman
      - npm install -g newman-reporter-html

  build:
    commands:
      - newman run -r html,cli test_api.postman_collection.json --reporter-html-export report.html

artifacts:
  files:
    - report.html
```
This installs **newman** and **newman-reporter-html** dependencies for our postman cli. After prebuild, this will run cli command to run and generate html report from our postman collection `newman run -r html,cli {name}.postman_collection.json --reporter-html-export report.html`. 

### 3. Create CodeCommit Repository and push files
Open the CodeCommit console at https://console.aws.amazon.com/codesuite/codecommit/home then click create repository


![Code Commit1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t88d37kcyertx7bbbayq.png)


Enter Repository name and description (optional) then click create


![Code Commit2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wdpdf44q5hlcscb1sinv.png)


![Code Commit3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s4eks85bw3zgzd7s5htj.png)

Clone repository and push your buildspec and postman file. You can specify a certain branch but we will just be using master.

### 4. Create s3 bucket for report
This will serve as the output folder for our generated html report.

Open the S3 console at https://console.aws.amazon.com/s3/ then click Create bucket


![s3 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9omwb6uxe4aamjgls389.png)

Enter bucket name and click Create bucket
![s3 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8qblklwimvbcoc1xkwjr.png)

![s3 4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ks8iqewf7kqnytny6kbs.png)

![s3 5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bwkbihuelmytsvivylsf.png)

### 5. Create a build project in AWS CodeBuild
Open the AWS CodeBuild console at https://console.aws.amazon.com/codesuite/codebuild/home then click Create project

![codebuild1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e8438pye7mqaeqv0wfjg.png)

Fill in the following sections. Once complete, choose Create build project at the bottom of the page.

**Sections:**
Project configuration - Name and description
Source provider - AWS Codecommit, repo name
Environment - Ubuntu, Standard, aws/codebuild/standard:6.0
Buildspec
Batch configuration
Artifacts - s3, bucket name
Logs


![codebuild2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mzdlgdf9z6vt5c11f6qp.png)


![codebuild source](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/66qsrixtzvgpbmde2hs5.png)


![codebuild environment](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yn1frw4tsce9r4p9hkz0.png)


![codebuild buildspec](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fuc9thol7rfdfdmyxktu.png)


![codebuild artifacts](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x3mkapveqlq2fe48weqa.png)

### 6. Create pipeline in CodePipeline and build all together
We will now combine our process by using AWS CodePipeline to pull our source code in CodeCommit, run build with AWS CodeBuild and output generated html in S3

Open the CodePipeline console at http://console.aws.amazon.com/codesuite/codepipeline/home and choose Create Pipeline


![code pipeline1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0dqiwco3aiz8enuqvff3.png)

On the Step 1: Choose pipeline settings page; in Pipeline name, enter the name for your pipeline. 

Choose New service role to allow CodePipeline to create a new service role in IAM.

![code pipeline step1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2uh1aou2dw2lq5wfkxqt.png)

On the Step 2: Add source stage page; in Source provider, select AWS CodeCommit, repo name, and branch name.


![code pipeline step2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wbophwf04j526z2oyv5o.png)

On the Step 3: Add build stage page, select AWS Codebuild and project name


![code pipeline step3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/91qp104ft9wqqpurqebl.png)

On the Step 4: Add deploy stage page, Choose Skip deploy stage.

On the Step 5: Review, Review all changes then click create pipeline.

## Output
You should see Succeeded status after running pipeline. The build log shows from prebuild ,running command, and generating output.

![run pipeline1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zzq2eim8lpk76dcd6osv.png)


![run pipeline2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cx23hmk0osf4ko5ef8rp.png)


![run pipeline3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y2ygg1dv6wb3tdzfoxkm.png)

Go to your s3 bucket to view successful report

![s3 output1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2e4doshcnhd9te4crfa6.png)


![s3 output2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xypgpj91cv2rr0v1t44u.png)

To share this html report with others. You can use presigned URLs

### To generate a presigned URL using the AWS Management Console

- In the Buckets list, choose the name of the bucket that contains the object that you want a presigned URL for.
- In the Objects list, select the object that you want to create a presigned URL for.
- On the Actions menu, choose Share with a presigned URL.
![presigned url1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l3d6u9y36h8e1msrot7y.png)

- Specify how long you want the presigned URL to be valid.
![presigned url2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pvm01b10vzi2j8wzxv1r.png)

- Choose Create presigned URL.
- When a confirmation appears, the URL is automatically copied to your clipboard. You will see a button to copy the 
presigned URL if you need to copy it again.
![presigned url3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gavyw5kvn6xc2o8jtdvk.png)



**References:**
https://learning.postman.com/docs/running-collections/using-newman-cli/installing-running-newman/

https://www.npmjs.com/package/newman-reporter-html

https://unmesh.me/2017/06/10/api-testing-with-postman-collections-in-aws-codepipeline/