├── .gitignore ├── 01api ├── petstore-api-buildspec.yml ├── petstore-api.yaml └── petstore-swagger.json ├── 02postman ├── PetStoreAPI.postman_collection.json ├── PetStoreAPIEnvironment.postman_environment.json └── update-postman-env-file.sh ├── 03codebuild └── postman-newman-buildspec.yml ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md └── petstore-api-pipeline.yaml /.gitignore: -------------------------------------------------------------------------------- 1 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider 2 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 3 | 4 | .idea 5 | *.iml 6 | *.ipr 7 | # File-based project format 8 | *.iws 9 | 10 | .DS_Store 11 | 12 | # aws cloudformation package output 13 | 01api/petstore-api-output.yaml 14 | 15 | # CMake 16 | cmake-build-*/ 17 | 18 | # IntelliJ 19 | out/ 20 | 21 | # mpeltonen/sbt-idea plugin 22 | .idea_modules/ 23 | 24 | # JIRA plugin 25 | atlassian-ide-plugin.xml 26 | 27 | # Crashlytics plugin (for Android Studio and IntelliJ) 28 | com_crashlytics_export_strings.xml 29 | crashlytics.properties 30 | crashlytics-build.properties 31 | fabric.properties -------------------------------------------------------------------------------- /01api/petstore-api-buildspec.yml: -------------------------------------------------------------------------------- 1 | version: 0.2 2 | phases: 3 | install: 4 | commands: 5 | - cd 01api 6 | - aws cloudformation package --template-file petstore-api.yaml 7 | --s3-bucket 8 | --s3-prefix api-code 9 | --output-template-file petstore-api-output.yaml 10 | artifacts: 11 | type: zip 12 | files: 13 | - 01api/petstore-api-output.yaml 14 | -------------------------------------------------------------------------------- /01api/petstore-api.yaml: -------------------------------------------------------------------------------- 1 | AWSTemplateFormatVersion: '2010-09-09' 2 | Transform: AWS::Serverless-2016-10-31 3 | Resources: 4 | PetStoreAPI: 5 | Type: AWS::Serverless::Api 6 | Properties: 7 | StageName: prod 8 | DefinitionUri: petstore-swagger.json 9 | 10 | Outputs: 11 | 12 | PetStoreAPI: 13 | Description: "Root API endpoint URL for Prod stage" 14 | Value: !Sub "https://${PetStoreAPI}.execute-api.${AWS::Region}.amazonaws.com/prod" 15 | Export: 16 | Name: PetStoreAPIRoot -------------------------------------------------------------------------------- /01api/petstore-swagger.json: -------------------------------------------------------------------------------- 1 | { 2 | "swagger": "2.0", 3 | "info": { 4 | "description": "Your first API with Amazon API Gateway. This is a sample API that integrates via HTTP with our demo Pet Store endpoints", 5 | "title": "PetStore" 6 | }, 7 | "schemes": [ 8 | "https" 9 | ], 10 | "paths": { 11 | "/": { 12 | "get": { 13 | "tags": [ 14 | "pets" 15 | ], 16 | "description": "PetStore HTML web page containing API usage information", 17 | "consumes": [ 18 | "application/json" 19 | ], 20 | "produces": [ 21 | "text/html" 22 | ], 23 | "responses": { 24 | "200": { 25 | "description": "Successful operation", 26 | "headers": { 27 | "Content-Type": { 28 | "type": "string", 29 | "description": "Media type of request" 30 | } 31 | } 32 | } 33 | }, 34 | "x-amazon-apigateway-integration": { 35 | "responses": { 36 | "default": { 37 | "statusCode": "200", 38 | "responseParameters": { 39 | "method.response.header.Content-Type": "'text/html'" 40 | }, 41 | "responseTemplates": { 42 | "text/html": "\n \n \n \n \n

Welcome to your Pet Store API

\n

\n You have successfully deployed your first API. You are seeing this HTML page because the GET method to the root resource of your API returns this content as a Mock integration.\n

\n

\n The Pet Store API contains the /pets and /pets/{petId} resources. By making a GET request to /pets you can retrieve a list of Pets in your API. If you are looking for a specific pet, for example the pet with ID 1, you can make a GET request to /pets/1.\n

\n

\n You can use a REST client such as Postman to test the POST methods in your API to create a new pet. Use the sample body below to send the POST request:\n

\n
\n{\n    \"type\" : \"cat\",\n    \"price\" : 123.11\n}\n        
\n \n" 43 | } 44 | } 45 | }, 46 | "passthroughBehavior": "when_no_match", 47 | "requestTemplates": { 48 | "application/json": "{\"statusCode\": 200}" 49 | }, 50 | "type": "mock" 51 | } 52 | } 53 | }, 54 | "/pets": { 55 | "get": { 56 | "tags": [ 57 | "pets" 58 | ], 59 | "summary": "List all pets", 60 | "produces": [ 61 | "application/json" 62 | ], 63 | "parameters": [ 64 | { 65 | "name": "type", 66 | "in": "query", 67 | "description": "The type of pet to retrieve", 68 | "required": false, 69 | "type": "string" 70 | }, 71 | { 72 | "name": "page", 73 | "in": "query", 74 | "description": "Page number of results to return.", 75 | "required": false, 76 | "type": "string" 77 | } 78 | ], 79 | "responses": { 80 | "200": { 81 | "description": "Successful operation", 82 | "schema": { 83 | "$ref": "#/definitions/Pets" 84 | }, 85 | "headers": { 86 | "Access-Control-Allow-Origin": { 87 | "type": "string", 88 | "description": "URI that may access the resource" 89 | } 90 | } 91 | } 92 | }, 93 | "x-amazon-apigateway-integration": { 94 | "responses": { 95 | "default": { 96 | "statusCode": "200", 97 | "responseParameters": { 98 | "method.response.header.Access-Control-Allow-Origin": "'*'" 99 | } 100 | } 101 | }, 102 | "requestParameters": { 103 | "integration.request.querystring.page": "method.request.querystring.page", 104 | "integration.request.querystring.type": "method.request.querystring.type" 105 | }, 106 | "uri": "http://petstore-demo-endpoint.execute-api.com/petstore/pets", 107 | "passthroughBehavior": "when_no_match", 108 | "httpMethod": "GET", 109 | "type": "http" 110 | } 111 | }, 112 | "post": { 113 | "tags": [ 114 | "pets" 115 | ], 116 | "operationId": "CreatePet", 117 | "summary": "Create a pet", 118 | "consumes": [ 119 | "application/json" 120 | ], 121 | "produces": [ 122 | "application/json" 123 | ], 124 | "parameters": [ 125 | { 126 | "in": "body", 127 | "name": "NewPet", 128 | "required": true, 129 | "schema": { 130 | "$ref": "#/definitions/NewPet" 131 | } 132 | } 133 | ], 134 | "responses": { 135 | "200": { 136 | "description": "Successful operation", 137 | "schema": { 138 | "$ref": "#/definitions/NewPetResponse" 139 | }, 140 | "headers": { 141 | "Access-Control-Allow-Origin": { 142 | "type": "string", 143 | "description": "URI that may access the resource" 144 | } 145 | } 146 | } 147 | }, 148 | "x-amazon-apigateway-integration": { 149 | "responses": { 150 | "default": { 151 | "statusCode": "200", 152 | "responseParameters": { 153 | "method.response.header.Access-Control-Allow-Origin": "'*'" 154 | } 155 | } 156 | }, 157 | "uri": "http://petstore-demo-endpoint.execute-api.com/petstore/pets", 158 | "passthroughBehavior": "when_no_match", 159 | "httpMethod": "POST", 160 | "type": "http" 161 | } 162 | }, 163 | "options": { 164 | "consumes": [ 165 | "application/json" 166 | ], 167 | "produces": [ 168 | "application/json" 169 | ], 170 | "responses": { 171 | "200": { 172 | "description": "Successful operation", 173 | "schema": { 174 | "$ref": "#/definitions/Empty" 175 | }, 176 | "headers": { 177 | "Access-Control-Allow-Origin": { 178 | "type": "string", 179 | "description": "URI that may access the resource" 180 | }, 181 | "Access-Control-Allow-Methods": { 182 | "type": "string", 183 | "description": "Method or methods allowed when accessing the resource" 184 | }, 185 | "Access-Control-Allow-Headers": { 186 | "type": "string", 187 | "description": "Used in response to a preflight request to indicate which HTTP headers can be used when making the request." 188 | } 189 | } 190 | } 191 | }, 192 | "x-amazon-apigateway-integration": { 193 | "responses": { 194 | "default": { 195 | "statusCode": "200", 196 | "responseParameters": { 197 | "method.response.header.Access-Control-Allow-Methods": "'POST,GET,OPTIONS'", 198 | "method.response.header.Access-Control-Allow-Headers": "'Content-Type,X-Amz-Date,Authorization,X-Api-Key'", 199 | "method.response.header.Access-Control-Allow-Origin": "'*'" 200 | } 201 | } 202 | }, 203 | "passthroughBehavior": "when_no_match", 204 | "requestTemplates": { 205 | "application/json": "{\"statusCode\": 200}" 206 | }, 207 | "type": "mock" 208 | } 209 | } 210 | }, 211 | "/pets/{petId}": { 212 | "get": { 213 | "tags": [ 214 | "pets" 215 | ], 216 | "summary": "Info for a specific pet", 217 | "operationId": "GetPet", 218 | "produces": [ 219 | "application/json" 220 | ], 221 | "parameters": [ 222 | { 223 | "name": "petId", 224 | "in": "path", 225 | "description": "The id of the pet to retrieve", 226 | "required": true, 227 | "type": "string" 228 | } 229 | ], 230 | "responses": { 231 | "200": { 232 | "description": "Successful operation", 233 | "schema": { 234 | "$ref": "#/definitions/Pet" 235 | }, 236 | "headers": { 237 | "Access-Control-Allow-Origin": { 238 | "type": "string", 239 | "description": "URI that may access the resource" 240 | } 241 | } 242 | } 243 | }, 244 | "x-amazon-apigateway-integration": { 245 | "responses": { 246 | "default": { 247 | "statusCode": "200", 248 | "responseParameters": { 249 | "method.response.header.Access-Control-Allow-Origin": "'*'" 250 | } 251 | } 252 | }, 253 | "requestParameters": { 254 | "integration.request.path.petId": "method.request.path.petId" 255 | }, 256 | "uri": "http://petstore-demo-endpoint.execute-api.com/petstore/pets/{petId}", 257 | "passthroughBehavior": "when_no_match", 258 | "httpMethod": "GET", 259 | "type": "http" 260 | } 261 | }, 262 | "options": { 263 | "consumes": [ 264 | "application/json" 265 | ], 266 | "produces": [ 267 | "application/json" 268 | ], 269 | "parameters": [ 270 | { 271 | "name": "petId", 272 | "in": "path", 273 | "description": "The id of the pet to retrieve", 274 | "required": true, 275 | "type": "string" 276 | } 277 | ], 278 | "responses": { 279 | "200": { 280 | "description": "Successful operation", 281 | "schema": { 282 | "$ref": "#/definitions/Empty" 283 | }, 284 | "headers": { 285 | "Access-Control-Allow-Origin": { 286 | "type": "string", 287 | "description": "URI that may access the resource" 288 | }, 289 | "Access-Control-Allow-Methods": { 290 | "type": "string", 291 | "description": "Method or methods allowed when accessing the resource" 292 | }, 293 | "Access-Control-Allow-Headers": { 294 | "type": "string", 295 | "description": "Used in response to a preflight request to indicate which HTTP headers can be used when making the request." 296 | } 297 | } 298 | } 299 | }, 300 | "x-amazon-apigateway-integration": { 301 | "responses": { 302 | "default": { 303 | "statusCode": "200", 304 | "responseParameters": { 305 | "method.response.header.Access-Control-Allow-Methods": "'GET,OPTIONS'", 306 | "method.response.header.Access-Control-Allow-Headers": "'Content-Type,X-Amz-Date,Authorization,X-Api-Key'", 307 | "method.response.header.Access-Control-Allow-Origin": "'*'" 308 | } 309 | } 310 | }, 311 | "passthroughBehavior": "when_no_match", 312 | "requestTemplates": { 313 | "application/json": "{\"statusCode\": 200}" 314 | }, 315 | "type": "mock" 316 | } 317 | } 318 | } 319 | }, 320 | "definitions": { 321 | "Pets": { 322 | "type": "array", 323 | "items": { 324 | "$ref": "#/definitions/Pet" 325 | } 326 | }, 327 | "Empty": { 328 | "type": "object" 329 | }, 330 | "NewPetResponse": { 331 | "type": "object", 332 | "properties": { 333 | "pet": { 334 | "$ref": "#/definitions/Pet" 335 | }, 336 | "message": { 337 | "type": "string" 338 | } 339 | } 340 | }, 341 | "Pet": { 342 | "type": "object", 343 | "properties": { 344 | "id": { 345 | "type": "integer" 346 | }, 347 | "type": { 348 | "type": "string" 349 | }, 350 | "price": { 351 | "type": "number" 352 | } 353 | } 354 | }, 355 | "NewPet": { 356 | "type": "object", 357 | "properties": { 358 | "type": { 359 | "$ref": "#/definitions/PetType" 360 | }, 361 | "price": { 362 | "type": "number" 363 | } 364 | } 365 | }, 366 | "PetType": { 367 | "type": "string", 368 | "enum": [ 369 | "dog", 370 | "cat", 371 | "fish", 372 | "bird", 373 | "gecko" 374 | ] 375 | } 376 | }, 377 | "x-amazon-apigateway-documentation": { 378 | "version": "v2.1", 379 | "createdDate": "2016-11-17T07:03:59Z", 380 | "documentationParts": [ 381 | { 382 | "location": { 383 | "type": "API" 384 | }, 385 | "properties": { 386 | "info": { 387 | "description": "Your first API with Amazon API Gateway. This is a sample API that integrates via HTTP with our demo Pet Store endpoints" 388 | } 389 | } 390 | }, 391 | { 392 | "location": { 393 | "type": "METHOD", 394 | "method": "GET" 395 | }, 396 | "properties": { 397 | "tags": [ 398 | "pets" 399 | ], 400 | "description": "PetStore HTML web page containing API usage information" 401 | } 402 | }, 403 | { 404 | "location": { 405 | "type": "METHOD", 406 | "path": "/pets/{petId}", 407 | "method": "GET" 408 | }, 409 | "properties": { 410 | "tags": [ 411 | "pets" 412 | ], 413 | "summary": "Info for a specific pet" 414 | } 415 | }, 416 | { 417 | "location": { 418 | "type": "METHOD", 419 | "path": "/pets", 420 | "method": "GET" 421 | }, 422 | "properties": { 423 | "tags": [ 424 | "pets" 425 | ], 426 | "summary": "List all pets" 427 | } 428 | }, 429 | { 430 | "location": { 431 | "type": "METHOD", 432 | "path": "/pets", 433 | "method": "POST" 434 | }, 435 | "properties": { 436 | "tags": [ 437 | "pets" 438 | ], 439 | "summary": "Create a pet" 440 | } 441 | }, 442 | { 443 | "location": { 444 | "type": "PATH_PARAMETER", 445 | "path": "/pets/{petId}", 446 | "method": "*", 447 | "name": "petId" 448 | }, 449 | "properties": { 450 | "description": "The id of the pet to retrieve" 451 | } 452 | }, 453 | { 454 | "location": { 455 | "type": "QUERY_PARAMETER", 456 | "path": "/pets", 457 | "method": "GET", 458 | "name": "page" 459 | }, 460 | "properties": { 461 | "description": "Page number of results to return." 462 | } 463 | }, 464 | { 465 | "location": { 466 | "type": "QUERY_PARAMETER", 467 | "path": "/pets", 468 | "method": "GET", 469 | "name": "type" 470 | }, 471 | "properties": { 472 | "description": "The type of pet to retrieve" 473 | } 474 | }, 475 | { 476 | "location": { 477 | "type": "REQUEST_BODY", 478 | "path": "/pets", 479 | "method": "POST" 480 | }, 481 | "properties": { 482 | "description": "Pet object that needs to be added to the store" 483 | } 484 | }, 485 | { 486 | "location": { 487 | "type": "RESPONSE", 488 | "method": "*", 489 | "statusCode": "200" 490 | }, 491 | "properties": { 492 | "description": "Successful operation" 493 | } 494 | }, 495 | { 496 | "location": { 497 | "type": "RESPONSE_HEADER", 498 | "method": "OPTIONS", 499 | "statusCode": "200", 500 | "name": "Access-Control-Allow-Headers" 501 | }, 502 | "properties": { 503 | "description": "Used in response to a preflight request to indicate which HTTP headers can be used when making the request." 504 | } 505 | }, 506 | { 507 | "location": { 508 | "type": "RESPONSE_HEADER", 509 | "method": "OPTIONS", 510 | "statusCode": "200", 511 | "name": "Access-Control-Allow-Methods" 512 | }, 513 | "properties": { 514 | "description": "Method or methods allowed when accessing the resource" 515 | } 516 | }, 517 | { 518 | "location": { 519 | "type": "RESPONSE_HEADER", 520 | "method": "*", 521 | "statusCode": "200", 522 | "name": "Access-Control-Allow-Origin" 523 | }, 524 | "properties": { 525 | "description": "URI that may access the resource" 526 | } 527 | }, 528 | { 529 | "location": { 530 | "type": "RESPONSE_HEADER", 531 | "method": "GET", 532 | "statusCode": "200", 533 | "name": "Content-Type" 534 | }, 535 | "properties": { 536 | "description": "Media type of request" 537 | } 538 | } 539 | ] 540 | } 541 | } 542 | -------------------------------------------------------------------------------- /02postman/PetStoreAPI.postman_collection.json: -------------------------------------------------------------------------------- 1 | { 2 | "info": { 3 | "_postman_id": "83e98cfc-83a9-46f8-916d-21d1a2b4e062", 4 | "name": "PetStoreAPI", 5 | "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" 6 | }, 7 | "item": [ 8 | { 9 | "name": "list pets", 10 | "event": [ 11 | { 12 | "listen": "test", 13 | "script": { 14 | "id": "97be1e6a-eb05-414a-930d-bd85cb3eb568", 15 | "exec": [ 16 | "/**", 17 | " * ", 18 | " */ ", 19 | "pm.test(\"Is valid response with json array in body\", function () {", 20 | " ", 21 | " // assert that the status code is 200", 22 | " pm.response.to.be.ok; // info, success, redirection, clientError, serverError, are other variants", 23 | " ", 24 | " // assert that the response has a valid JSON body", 25 | " pm.response.to.be.withBody;", 26 | " ", 27 | " pm.response.to.be.json; // this assertion also checks if a body exists, so the above check is not needed", 28 | " ", 29 | " //make sure we have a valid json array", 30 | " pm.expect(pm.response.json()).to.be.an('array').but.not.an('object');", 31 | " ", 32 | "});", 33 | "", 34 | "pm.test(\"Has Content-Type header\", function () {", 35 | " pm.expect(responseHeaders.hasOwnProperty(\"Content-Type\"));", 36 | "});", 37 | "", 38 | "pm.test(\"Is response time is less than 200ms\", function () {", 39 | " pm.expect(pm.response.responseTime).to.be.below(200);", 40 | "});", 41 | "", 42 | "", 43 | "", 44 | "//https://www.chaijs.com/api/bdd/#method_a", 45 | "//pm.test('is valid Array', () => pm.expect(pm.response.json()).to.be.an('array').but.not.an('object'))" 46 | ], 47 | "type": "text/javascript" 48 | } 49 | } 50 | ], 51 | "request": { 52 | "method": "GET", 53 | "header": [], 54 | "url": { 55 | "raw": "{{apigw-root}}/pets", 56 | "host": [ 57 | "{{apigw-root}}" 58 | ], 59 | "path": [ 60 | "pets" 61 | ] 62 | } 63 | }, 64 | "response": [] 65 | }, 66 | { 67 | "name": "add pet", 68 | "event": [ 69 | { 70 | "listen": "test", 71 | "script": { 72 | "id": "2987e653-1c08-4f37-a960-105116dc4df6", 73 | "exec": [ 74 | "/**", 75 | " * ", 76 | " */", 77 | "pm.test(\"Is valid response with json object in body\", function () {", 78 | " ", 79 | " // assert that the status code is 200", 80 | " pm.response.to.be.ok; // info, success, redirection, clientError, serverError, are other variants", 81 | " ", 82 | " // assert that the response has a valid JSON body", 83 | " pm.response.to.be.withBody;", 84 | " ", 85 | " pm.response.to.be.json; // this assertion also checks if a body exists, so the above check is not needed", 86 | " ", 87 | " //make sure we have a valid json array", 88 | " pm.expect(pm.response.json()).to.be.an('object').but.not.an('array');", 89 | " ", 90 | "});", 91 | "", 92 | "pm.test(\"Has Content-Type header\", function () {", 93 | " pm.expect(responseHeaders.hasOwnProperty(\"Content-Type\"));", 94 | "});", 95 | "", 96 | "pm.test(\"Is message response equal sucess\", function () {", 97 | " var jsonData = pm.response.json();", 98 | " pm.expect(jsonData.message).to.eql(\"success\");", 99 | "});", 100 | "", 101 | "pm.test(\"Is response time is less than 200ms\", function () {", 102 | " pm.expect(pm.response.responseTime).to.be.below(200);", 103 | "});" 104 | ], 105 | "type": "text/javascript" 106 | } 107 | } 108 | ], 109 | "request": { 110 | "method": "POST", 111 | "header": [ 112 | { 113 | "key": "Content-Type", 114 | "name": "Content-Type", 115 | "value": "application/json", 116 | "type": "text" 117 | } 118 | ], 119 | "body": { 120 | "mode": "raw", 121 | "raw": "{\n\t\"type\":\"Lizzard\",\n\t\"price\": 3.50\n}" 122 | }, 123 | "url": { 124 | "raw": "{{apigw-root}}/pets", 125 | "host": [ 126 | "{{apigw-root}}" 127 | ], 128 | "path": [ 129 | "pets" 130 | ] 131 | } 132 | }, 133 | "response": [] 134 | }, 135 | { 136 | "name": "get pet by id", 137 | "event": [ 138 | { 139 | "listen": "test", 140 | "script": { 141 | "id": "9bce0715-8ea7-4685-84e1-78adb75cfd74", 142 | "exec": [ 143 | "/**", 144 | " * ", 145 | " */", 146 | "pm.test(\"Is valid response with json object in body\", function () {", 147 | " ", 148 | " // assert that the status code is 200", 149 | " pm.response.to.be.ok; // info, success, redirection, clientError, serverError, are other variants", 150 | " ", 151 | " // assert that the response has a valid JSON body", 152 | " pm.response.to.be.withBody;", 153 | " ", 154 | " pm.response.to.be.json; // this assertion also checks if a body exists, so the above check is not needed", 155 | " ", 156 | " //make sure we have a valid json array", 157 | " pm.expect(pm.response.json()).to.be.an('object').but.not.an('array');", 158 | " ", 159 | "});", 160 | "", 161 | "pm.test(\"Has Content-Type header\", function () {", 162 | " pm.expect(responseHeaders.hasOwnProperty(\"Content-Type\"));", 163 | "});", 164 | "", 165 | "pm.test(\"Is response time is less than 200ms\", function () {", 166 | " pm.expect(pm.response.responseTime).to.be.below(200);", 167 | "});", 168 | "", 169 | "", 170 | "", 171 | "//https://www.chaijs.com/api/bdd/#method_a", 172 | "//pm.test('is valid Array', () => pm.expect(pm.response.json()).to.be.an('array').but.not.an('object'))", 173 | "" 174 | ], 175 | "type": "text/javascript" 176 | } 177 | } 178 | ], 179 | "request": { 180 | "method": "GET", 181 | "header": [], 182 | "url": { 183 | "raw": "{{apigw-root}}/pets/3", 184 | "host": [ 185 | "{{apigw-root}}" 186 | ], 187 | "path": [ 188 | "pets", 189 | "3" 190 | ] 191 | } 192 | }, 193 | "response": [] 194 | } 195 | ] 196 | } -------------------------------------------------------------------------------- /02postman/PetStoreAPIEnvironment.postman_environment.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": "e13e9e57-0ec0-4eb9-bc4d-d2a5adc9b4c4", 3 | "name": "PetStoreAPIEnvironment", 4 | "values": [ 5 | { 6 | "key": "apigw-root", 7 | "value": "https://g228ynt8yb.execute-api.us-east-1.amazonaws.com/prod", 8 | "enabled": true 9 | } 10 | ], 11 | "_postman_variable_scope": "environment", 12 | "_postman_exported_at": "2019-07-11T15:32:21.115Z", 13 | "_postman_exported_using": "Postman/7.2.2" 14 | } -------------------------------------------------------------------------------- /02postman/update-postman-env-file.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | #This shell script updates Postman environment file with the API Gateway URL created 4 | # via the api gateway deployment 5 | 6 | echo "Running update-postman-env-file.sh" 7 | 8 | api_gateway_url=`aws cloudformation describe-stacks \ 9 | --stack-name petstore-api-stack \ 10 | --query "Stacks[0].Outputs[*].{OutputValueValue:OutputValue}" --output text` 11 | 12 | echo "API Gateway URL:" ${api_gateway_url} 13 | 14 | jq -e --arg apigwurl "$api_gateway_url" '(.values[] | select(.key=="apigw-root") | .value) = $apigwurl' \ 15 | PetStoreAPIEnvironment.postman_environment.json > PetStoreAPIEnvironment.postman_environment.json.tmp \ 16 | && cp PetStoreAPIEnvironment.postman_environment.json.tmp PetStoreAPIEnvironment.postman_environment.json \ 17 | && rm PetStoreAPIEnvironment.postman_environment.json.tmp 18 | 19 | echo "Updated PetStoreAPIEnvironment.postman_environment.json" 20 | 21 | cat PetStoreAPIEnvironment.postman_environment.json -------------------------------------------------------------------------------- /03codebuild/postman-newman-buildspec.yml: -------------------------------------------------------------------------------- 1 | version: 0.2 2 | 3 | env: 4 | variables: 5 | key: "S3_BUCKET" 6 | 7 | phases: 8 | install: 9 | runtime-versions: 10 | nodejs: 10 11 | commands: #https://learning.getpostman.com/docs/postman/collection-runs/command-line-integration-with-newman/ 12 | - npm install -g newman 13 | - yum install -y jq 14 | 15 | pre_build: 16 | commands: 17 | - aws s3 cp "s3://${S3_BUCKET}/postman-env-files/PetStoreAPIEnvironment.postman_environment.json" ./02postman/ 18 | - aws s3 cp "s3://${S3_BUCKET}/postman-env-files/PetStoreAPI.postman_collection.json" ./02postman/ 19 | - cd ./02postman 20 | - ./update-postman-env-file.sh 21 | 22 | build: 23 | commands: 24 | - echo Build started on `date` from dir `pwd` 25 | - newman run PetStoreAPI.postman_collection.json --environment PetStoreAPIEnvironment.postman_environment.json -r junit 26 | 27 | reports: 28 | JUnitReports: # CodeBuild will create a report group called "SurefireReports". 29 | files: #Store all of the files 30 | - '**/*' 31 | base-directory: '02postman/newman' # Location of the reports 32 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | ## Code of Conduct 2 | This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). 3 | For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact 4 | opensource-codeofconduct@amazon.com with any additional questions or comments. 5 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing Guidelines 2 | 3 | Thank you for your interest in contributing to our project. Whether it's a bug report, new feature, correction, or additional 4 | documentation, we greatly value feedback and contributions from our community. 5 | 6 | Please read through this document before submitting any issues or pull requests to ensure we have all the necessary 7 | information to effectively respond to your bug report or contribution. 8 | 9 | 10 | ## Reporting Bugs/Feature Requests 11 | 12 | We welcome you to use the GitHub issue tracker to report bugs or suggest features. 13 | 14 | When filing an issue, please check existing open, or recently closed, issues to make sure somebody else hasn't already 15 | reported the issue. Please try to include as much information as you can. Details like these are incredibly useful: 16 | 17 | * A reproducible test case or series of steps 18 | * The version of our code being used 19 | * Any modifications you've made relevant to the bug 20 | * Anything unusual about your environment or deployment 21 | 22 | 23 | ## Contributing via Pull Requests 24 | Contributions via pull requests are much appreciated. Before sending us a pull request, please ensure that: 25 | 26 | 1. You are working against the latest source on the *master* branch. 27 | 2. You check existing open, and recently merged, pull requests to make sure someone else hasn't addressed the problem already. 28 | 3. You open an issue to discuss any significant work - we would hate for your time to be wasted. 29 | 30 | To send us a pull request, please: 31 | 32 | 1. Fork the repository. 33 | 2. Modify the source; please focus on the specific change you are contributing. If you also reformat all the code, it will be hard for us to focus on your change. 34 | 3. Ensure local tests pass. 35 | 4. Commit to your fork using clear commit messages. 36 | 5. Send us a pull request, answering any default questions in the pull request interface. 37 | 6. Pay attention to any automated CI failures reported in the pull request, and stay involved in the conversation. 38 | 39 | GitHub provides additional document on [forking a repository](https://help.github.com/articles/fork-a-repo/) and 40 | [creating a pull request](https://help.github.com/articles/creating-a-pull-request/). 41 | 42 | 43 | ## Finding contributions to work on 44 | Looking at the existing issues is a great way to find something to contribute on. As our projects, by default, use the default GitHub issue labels (enhancement/bug/duplicate/help wanted/invalid/question/wontfix), looking at any 'help wanted' issues is a great place to start. 45 | 46 | 47 | ## Code of Conduct 48 | This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). 49 | For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact 50 | opensource-codeofconduct@amazon.com with any additional questions or comments. 51 | 52 | 53 | ## Security issue notifications 54 | If you discover a potential security issue in this project we ask that you notify AWS/Amazon Security via our [vulnerability reporting page](http://aws.amazon.com/security/vulnerability-reporting/). Please do **not** create a public github issue. 55 | 56 | 57 | ## Licensing 58 | 59 | See the [LICENSE](LICENSE) file for our project's licensing. We will ask you to confirm the licensing of your contribution. 60 | 61 | We may ask you to sign a [Contributor License Agreement (CLA)](http://en.wikipedia.org/wiki/Contributor_License_Agreement) for larger changes. 62 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | this software and associated documentation files (the "Software"), to deal in 5 | the Software without restriction, including without limitation the rights to 6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | the Software, and to permit persons to whom the Software is furnished to do so. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 10 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 11 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 12 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 13 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 14 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 15 | 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Automating API Testing with AWS Code Pipeline, AWS Code Build, and Postman 2 | 3 | This post demonstrates the use of AWS CodeBuild, CodePipeline and Postman to deploy, 4 | functionally test an API, and view the generated reports using a new feature from 5 | CodeBuild called Reports. 6 | 7 | Please refer to the following blog post for instructions: https://aws.amazon.com/blogs/devops/automating-your-api-testing-with-aws-codebuild-aws-codepipeline-and-postman/ 8 | 9 | 10 | ## License 11 | 12 | This library is licensed under the MIT-0 License. See the LICENSE file. 13 | 14 | -------------------------------------------------------------------------------- /petstore-api-pipeline.yaml: -------------------------------------------------------------------------------- 1 | AWSTemplateFormatVersion: "2010-09-09" 2 | Description: Code pipeline for automated api functional testing 3 | 4 | Parameters: 5 | 6 | GitHubRepositoryName: 7 | Type: String 8 | Description: GitHub repository name. 9 | MinLength: 1 10 | MaxLength: 100 11 | GitHubUser: 12 | Type: String 13 | GitHubBranch: 14 | Type: String 15 | GitHubToken: 16 | Type: String 17 | Description: repository token. 18 | NoEcho: true 19 | BucketRoot: 20 | Type: String 21 | Description: bucket containing project artifacts e.g. postman config files, etc. 22 | MinLength: 1 23 | MaxLength: 100 24 | 25 | Resources: 26 | 27 | CodePipelineTrustRole: 28 | Type: "AWS::IAM::Role" 29 | Properties: 30 | RoleName: 31 | Fn::Sub: ${AWS::StackName}-CodePipelineRole 32 | AssumeRolePolicyDocument: 33 | Version: "2012-10-17" 34 | Statement: 35 | - 36 | Effect: "Allow" 37 | Principal: 38 | Service: 39 | - "codepipeline.amazonaws.com" 40 | Action: 41 | - "sts:AssumeRole" 42 | Path: / 43 | #TODO: scope resources 44 | Policies: 45 | - 46 | PolicyName: 47 | Fn::Sub: ${AWS::StackName}-CodePipelineRolePolicy 48 | PolicyDocument: 49 | Version: "2012-10-17" 50 | Statement: 51 | - 52 | Effect: "Allow" 53 | Action: 54 | - "s3:DeleteObject" 55 | - "s3:GetObject" 56 | - "s3:GetObjectVersion" 57 | - "s3:ListBucket" 58 | - "s3:PutObject" 59 | - "s3:GetBucketPolicy" 60 | Resource: '*' 61 | - 62 | Effect: "Allow" 63 | Action: 64 | - "codebuild:StartBuild" 65 | - "codebuild:BatchGetBuilds" 66 | Resource: '*' 67 | - 68 | Effect: "Allow" 69 | Action: 70 | - "cloudformation:DescribeStacks" 71 | - "cloudformation:DescribeChangeSet" 72 | - "cloudformation:CreateStack" 73 | - "cloudformation:DeleteStack" 74 | - "cloudformation:UpdateStack" 75 | - "cloudformation:CreateChangeSet" 76 | - "cloudformation:DeleteChangeSet" 77 | - "cloudformation:ExecuteChangeSet" 78 | - "cloudformation:SetStackPolicy" 79 | - "cloudformation:ValidateTemplate" 80 | - "iam:PassRole" 81 | Resource: '*' 82 | 83 | CodeBuildServiceRole: 84 | Type: AWS::IAM::Role 85 | Properties: 86 | RoleName: 87 | Fn::Sub: ${AWS::StackName}-CodeBuildServiceRole 88 | AssumeRolePolicyDocument: 89 | Version: "2012-10-17" 90 | Statement: 91 | - 92 | Effect: "Allow" 93 | Principal: 94 | Service: 95 | - "codebuild.amazonaws.com" 96 | Action: 97 | - "sts:AssumeRole" 98 | Path: /service-role/ 99 | Policies: 100 | - 101 | PolicyName: 102 | Fn::Sub: ${AWS::StackName}-CodeBuildServiceRole-Policy 103 | PolicyDocument: 104 | Version: "2012-10-17" 105 | Statement: 106 | - Effect: "Allow" 107 | Action: 108 | - "codebuild:CreateReportGroup" 109 | - "codebuild:CreateReport" 110 | - "codebuild:UpdateReport" 111 | - "codebuild:BatchPutTestCases" 112 | Resource: 113 | - "*" 114 | - Effect: "Allow" 115 | Action: 116 | - "cloudformation:DescribeStacks" 117 | Resource: 118 | - "*" 119 | - 120 | Effect: "Allow" 121 | Action: 122 | - "logs:CreateLogGroup" 123 | - "logs:CreateLogStream" 124 | - "logs:PutLogEvents" 125 | Resource: 126 | - Fn::Sub: arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/codebuild/* 127 | - 128 | Effect: "Allow" 129 | Action: 130 | - "s3:PutObject" 131 | - "s3:GetObject" 132 | - "s3:GetObjectVersion" 133 | - "s3:ListBucket" 134 | Resource: 135 | - Fn::Sub: arn:aws:s3:::${AWS::StackName}-codepipeline-artifact-bucket-${AWS::AccountId}/* 136 | - Fn::Sub: arn:aws:s3:::${AWS::StackName}-codepipeline-artifact-bucket-${AWS::AccountId} 137 | - Fn::Sub: arn:aws:s3:::${BucketRoot}/* 138 | - Fn::Sub: arn:aws:s3:::${BucketRoot} 139 | - 140 | Effect: "Allow" 141 | Action: 142 | - "iam:PassRole" 143 | Resource: 144 | - "*" 145 | 146 | CloudFormationTrustRole: 147 | Type: 'AWS::IAM::Role' 148 | Description: Creating service role in IAM for AWS CloudFormation 149 | Properties: 150 | Path: / 151 | ManagedPolicyArns: 152 | - 'arn:aws:iam::aws:policy/AdministratorAccess' 153 | RoleName: 154 | Fn::Sub: ${AWS::StackName}-CloudFormationTrustRole 155 | AssumeRolePolicyDocument: 156 | Statement: 157 | - Action: 'sts:AssumeRole' 158 | Effect: Allow 159 | Principal: 160 | Service: 161 | - cloudformation.amazonaws.com 162 | 163 | CodePipelineArtifactBucket: 164 | DeletionPolicy: Delete 165 | Type: 'AWS::S3::Bucket' 166 | Description: S3 bucket for pipeline artifacts 167 | Properties: 168 | AccessControl: Private 169 | BucketName: 170 | Fn::Sub: ${AWS::StackName}-codepipeline-artifact-bucket-${AWS::AccountId} 171 | VersioningConfiguration: 172 | Status: Enabled 173 | Tags: 174 | - Key: Name 175 | Value: 176 | Fn::Sub: ${AWS::StackName}-codepipeline-artifact-bucket 177 | 178 | #code build - api deployment using SAM (see buildspec.yml) 179 | PetStoreAPISAMCodeBuildProject: 180 | Type: AWS::CodeBuild::Project 181 | Properties: 182 | Name: petstore-api-stack-project 183 | Description: CodeBuild Project using AWS SAM to deploy PetStore API 184 | ServiceRole: 185 | Fn::GetAtt: [ CodeBuildServiceRole, Arn ] 186 | Artifacts: 187 | Type: S3 188 | Location: 189 | Ref: CodePipelineArtifactBucket 190 | Name: 191 | Fn::Sub: ${AWS::StackName}-petstore-api-stack-codebuild-project 192 | Environment: 193 | Type: LINUX_CONTAINER 194 | ComputeType: BUILD_GENERAL1_SMALL 195 | Image: aws/codebuild/nodejs:8.11.0 196 | Source: 197 | Location: !Join 198 | - '' 199 | - - 'https://github.com/' 200 | - !Ref GitHubUser 201 | - '/' 202 | - !Ref GitHubRepositoryName 203 | Type: GITHUB 204 | BuildSpec: 01api/petstore-api-buildspec.yml 205 | TimeoutInMinutes: 10 206 | Tags: 207 | - Key: Name 208 | Value: 209 | Fn::Sub: ${AWS::StackName}-petstore-api-stack-project 210 | 211 | #code build - api testing using Newman to execute Postman test def (see buildspec.yml) 212 | PostmanNewmanCodeBuildProject: 213 | Type: AWS::CodeBuild::Project 214 | Properties: 215 | Name: 216 | Fn::Sub: ${AWS::StackName}-newman-project 217 | Description: CodeBuild Project using newman cli to execute postman tests 218 | ServiceRole: 219 | Fn::GetAtt: [ CodeBuildServiceRole, Arn ] 220 | Artifacts: 221 | Type: S3 222 | Location: 223 | Ref: CodePipelineArtifactBucket 224 | Name: 225 | Fn::Sub: ${AWS::StackName}-newman-project 226 | Environment: 227 | Type: LINUX_CONTAINER 228 | ComputeType: BUILD_GENERAL1_SMALL 229 | Image: aws/codebuild/amazonlinux2-x86_64-standard:2.0 230 | EnvironmentVariables: 231 | - Name: S3_BUCKET 232 | Type: PLAINTEXT 233 | Value: 234 | Fn::Sub: ${BucketRoot} 235 | Source: 236 | Location: !Join 237 | - '' 238 | - - 'https://github.com/' 239 | - !Ref GitHubUser 240 | - '/' 241 | - !Ref GitHubRepositoryName 242 | Type: GITHUB 243 | BuildSpec: 03codebuild/postman-newman-buildspec.yml 244 | TimeoutInMinutes: 10 245 | Tags: 246 | - Key: Name 247 | Value: 248 | Fn::Sub: ${AWS::StackName}-newman-project 249 | 250 | 251 | PetStoreAPIPipeline: 252 | Type: "AWS::CodePipeline::Pipeline" 253 | DependsOn: 254 | - PetStoreAPISAMCodeBuildProject 255 | Properties: 256 | Name: petstore-api-pipeline 257 | RoleArn: 258 | Fn::GetAtt: [ CodePipelineTrustRole, Arn ] 259 | ArtifactStore: 260 | Type: S3 261 | Location: !Ref CodePipelineArtifactBucket 262 | Stages: 263 | - Name: Source 264 | Actions: 265 | - Name: CheckoutPetStoreAPISource 266 | ActionTypeId: 267 | Owner: ThirdParty 268 | Category: Source 269 | Version: 1 270 | Provider: GitHub 271 | Configuration: 272 | Owner: !Ref GitHubUser 273 | PollForSourceChanges: false 274 | Repo: !Ref GitHubRepositoryName 275 | Branch: !Ref GitHubBranch 276 | OAuthToken: !Ref GitHubToken 277 | InputArtifacts: [] 278 | OutputArtifacts: 279 | - Name: 'PetStoreAPI-SourceArtifact' 280 | RunOrder: 1 281 | 282 | - Name: Build 283 | Actions: 284 | - Name: BuildPetStoreAPI 285 | ActionTypeId: 286 | Owner: AWS 287 | Category: Build 288 | Version: 1 289 | Provider: CodeBuild 290 | Configuration: 291 | ProjectName: !Ref PetStoreAPISAMCodeBuildProject 292 | InputArtifacts: 293 | - Name: 'PetStoreAPI-SourceArtifact' 294 | OutputArtifacts: 295 | - Name: 'PetStoreAPI-BuildArtifact' 296 | RunOrder: 1 297 | 298 | - Name: Deploy 299 | Actions: 300 | - Name: GenerateChangeSet 301 | ActionTypeId: 302 | Owner: AWS 303 | Category: Deploy 304 | Version: 1 305 | Provider: CloudFormation 306 | Configuration: 307 | TemplatePath: !Join 308 | - '' 309 | - - 'PetStoreAPI-BuildArtifact' 310 | - '::01api/petstore-api-output.yaml' 311 | ActionMode: CHANGE_SET_REPLACE 312 | Capabilities: CAPABILITY_IAM 313 | ChangeSetName: pipeline-changeset 314 | RoleArn: !GetAtt 315 | - CloudFormationTrustRole 316 | - Arn 317 | StackName: petstore-api-stack 318 | InputArtifacts: 319 | - Name: 'PetStoreAPI-BuildArtifact' 320 | OutputArtifacts: [] 321 | RunOrder: 1 322 | 323 | - Name: ExecuteChangeSet 324 | ActionTypeId: 325 | Owner: AWS 326 | Category: Deploy 327 | Version: 1 328 | Provider: CloudFormation 329 | Configuration: 330 | ActionMode: CHANGE_SET_EXECUTE 331 | ChangeSetName: pipeline-changeset 332 | StackName: petstore-api-stack 333 | InputArtifacts: [] 334 | OutputArtifacts: [] 335 | RunOrder: 2 336 | 337 | # https://docs.aws.amazon.com/codepipeline/latest/userguide/actions-invoke-lambda-function.html 338 | - Name: Test 339 | Actions: 340 | - Name: TestPetStoreAPI 341 | ActionTypeId: 342 | Owner: AWS 343 | Category: Build 344 | Version: 1 345 | Provider: CodeBuild 346 | Configuration: 347 | ProjectName: !Ref PostmanNewmanCodeBuildProject 348 | InputArtifacts: 349 | - Name: 'PetStoreAPI-SourceArtifact' 350 | OutputArtifacts: 351 | - Name: 'PetStoreAPI-TestArtifact' 352 | RunOrder: 1 353 | 354 | PetStorePostmanPipelineRunnerRole: 355 | Type: AWS::IAM::Role 356 | Properties: 357 | AssumeRolePolicyDocument: 358 | Version: '2012-10-17' 359 | Statement: 360 | - Action: 361 | - 'sts:AssumeRole' 362 | Effect: Allow 363 | Principal: 364 | Service: 365 | - lambda.amazonaws.com 366 | Path: / 367 | Policies: 368 | - 369 | PolicyName: "root" 370 | PolicyDocument: 371 | Version: '2012-10-17' # Policy Document 372 | Statement: 373 | - 374 | Effect: Allow 375 | Action: 376 | - s3:PutObject 377 | - s3:PutObjectAcl 378 | - s3:GetObject 379 | - s3:GetObjectVersion 380 | Resource: 381 | - Fn::Sub: arn:aws:s3:::${BucketRoot}/* 382 | - 383 | Effect: Allow 384 | Action: 385 | - codepipeline:PutJobSuccessResult 386 | - codepipeline:PutJobSuccessFailure 387 | Resource: "*" 388 | - 389 | Effect: Allow 390 | Action: 391 | - cloudformation:ListExports 392 | Resource: "*" 393 | - 394 | Effect: Allow 395 | Action: 396 | - logs:CreateLogGroup 397 | - logs:CreateLogStream 398 | - logs:PutLogEvents 399 | Resource: 'arn:aws:logs:*:*:*' --------------------------------------------------------------------------------