In this module, you'll enhance the AWS CodePipeline that you built in Module 2 to add integration tests, and a Beta environment in which to test them.
To add integration tests to the pipeline, a second test application has been introduced to the Unicorn API project. Like the Unicorn API, the Test application consists of Lamdba Functions and a SAM CloudFormation template (unicorn-api/test-sam.yaml). In addition to the Lambda Functions and SAM template updates to the Unicorn API, the AWS CodeBuild unicorn-api/buildspec.yml has been modified to include a second set of commands that mirror the original application deployment. These commands install test dependencies, package the test SAM template, and include the test output SAM template as an additional artifact.
The integration test use a library for API testing, hippie, which includes a DSL for the easy description of HTTP requests. The unicorn-api/test/test.js defines a series of requests that excercise the Unicorn API REST resources, which are chained together using Javascript Promises. Below is a code snippet showing the chained Promises to execute the test cases. If all tests pass successfully, the function uses the injected CodePipeline Job Id to send a success callback, signaling the CodePipeline to transition. If any of the tests fail, a failure callback is sent, signaling the CodePipeline to halt.
exports.lambda_handler = (event, context, callback) => {
var api = event.api_url + '/unicorns/';
var unicorn = build_unicorn();
Promise.resolve()
.then(result => {
return list_unicorns(api, unicorn);
})
.then(result => {
return update_unicorn(api, unicorn);
})
.then(result => {
return view_unicorn_found(api, unicorn);
})
.then(result => {
return view_unicorn_not_found(api, unicorn);
})
.then(result => {
return remove_unicorn(api, unicorn);
})
.then(result => {
console.log('SUCCESS');
complete_job(event.job_id, result, callback);
})
.catch(reason => {
console.log('ERROR: ' + reason.test_name + ' | ' + reason.message);
fail_job(event.job_id, reason, context.invokeid, callback);
});
};
The test.js script is focused on defining and executing the API integration tests. The unicorn-api/test/setup.js script is responsible for querying the CloudFormation Stack to auto-discover the API URL to send to the test.js for test execution. After the API URL is discovered, the setup.js script asynchronously invokes the test.js Lambda Function, injecting the CodePipeline Job Id and API URL, as seen in the code snippet below.
exports.lambda_handler = (event, context, callback) => {
var job_id = event["CodePipeline.job"].id;
var stack_name = event["CodePipeline.job"].data.actionConfiguration.configuration.UserParameters;
get_api_url(stack_name).then(function(api_url) {
return invoke_test(job_id, api_url);
}).catch(function(err) {
fail_job(job_id, err, context.invokeid, callback);
});
};
In this module, you will update the CodePipeline you built in Module 2 with two new stages that are ordered between the Build and Prod stages. Like the Prod stage, the new Test stage will include two actions that use the test output SAM template artifact from CodeBuild to Create and Execute a CloudFormation Change Set to deploy the Lambda functions as a new CloudFormation Stack.
Following the Test stage, you will add a Beta stage that includes two actions that creates a new CloudFormation Stack for the Unicorn API in a new environment for testing. After the CloudFormation has been deployed, a third action will invoke a Lambda function from the Test application to drive integration tests against the Beta Unicorn API. If the tests pass, the pipeline will transition to the Prod stage to complete the Unicorn API changes in the Prod environment.
Below is an image depicting the CodePipeline upon completion:
Each of the following sections provide an implementation overview and detailed, step-by-step instructions. The overview should provide enough context for you to complete the implementation if you're already familiar with the AWS Management Console or you want to explore the services yourself without following a walkthrough.
If you're using the latest version of the Chrome, Firefox, or Safari web browsers the step-by-step instructions won't be visible until you expand the section.
You will reuse the S3 Bucket that you created in Module 1: Serverless Application Model from the DevOps Workshop. If you have not completed this Module, please refer to the module instructions to create the S3 Bucket
If you are unsure of your S3 Bucket's name, please follow the instructions below.
Step-by-step instructions (expand for details)
-
In the AWS Management Console choose Services then select S3 under Storage.
-
Browse the list of Buckets or use the search box to identify the S3 Bucket.
wildrydes-devops-yournamewas recommended as the Bucket name, however you may have chosen a different globaly unique name.
-
From the AWS Management Console, click on Services and then select CodePipeline in the Developer Tools section.
-
Choose
wildrydes-unicorn-apifrom the list of pipelines. -
Choose
Edit.
-
Choose
+ActionbelowTest. -
In the Add action dialog, select
Deployfor the Action category. -
Enter
CreateChangeSetfor the Action name. -
Select
AWS CloudFormationfor the Deployment provider. -
Select
Create or replace a change setfor Action mode -
Enter
wildrydes-unicorn-api-testfor Stack name -
Enter
wildrydes-unicorn-api-test-changesetfor Change set name -
Enter
MyAppBuild::test-sam-output.yamlfor Template -
Select
CAPABILITY_IAMfor Capabilities -
Enter
WildRydesUnicornApiCloudFormationfor Role name -
Enter
MyAppBuildfor Input artifacts #1 -
Choose Add Action
-
Choose
+ActionbelowCreateChangeSet. -
In the Add action dialog, select
Deployfor the Action category. -
Enter
ExecuteChangeSetfor the Action name. -
Select
AWS CloudFormationfor the Deployment provider. -
Select
Execute a change setfor Action mode -
Enter
wildrydes-unicorn-api-testfor Stack name -
Enter
wildrydes-unicorn-api-test-changesetfor Change set name -
Choose Add Action
The pipeline should look like the following screenshot after adding the new Test stage.
-
Scroll to the top of the pipeline and choose
Save pipeline changes -
Choose
Save and Continuewhen prompted by the Save Pipeline Changes dialog.
The addition of the Test stage is complete. You will now validate the Test stage is working by creating and uploading a deployment package, and then monitoring the completion of the pipeline.
The CodePipeline that you will create in the next step will be triggered by updates to a deployment package in S3.
-
Change directory to
aws-serverless-workshops-master/DevOps/4_MultipleEnvironments/unicorn-api. -
To create a deployment for this project, zip the contents of the
unicorn-apidirectory into a file namedunicorn-api.zip, which is your deployment package. If you are unsure how to zip the files in theunicorn-apidirectory, follow the instructions for Microsoft workstations here, and macOS workstations here.
Important
Zip the directory content, not the directory. The contents of the Zip file are available as the current working directory of the Lambda function. For example: /app-sam.yaml
-
In the AWS Management Console, choose Services then select S3 under Storage.
-
Browse the list of Buckets or use the search box to find the S3 bucket that you identified previously.
-
Choose Upload
-
Choose Add files, select the local copy of
unicorn-api.zipand then choose Upload in the bottom left corner of the dialog.
-
From the AWS Management Console, click on Services and then select CodePipeline in the Developer Tools section.
-
Choose
wildrydes-unicorn-apifrom the list of pipelines. -
Observe that each stage's color will turn blue during execution and green on completion. Following the successful execution of all stages, the pipeline should look like the following screenshot.
-
In the AWS Management Console, choose Services then select CloudFomation under Management Tools.
-
Select the checkbox to the left of
wildrydes-unicorn-api-testfrom the list of CloudFormation Stacks. -
In the Stack Details, select the Outputs tab.
-
Copy the value of the CodePipelineInvokeFunction Output variable
-
From the AWS Management Console, click on Services and then select CodePipeline in the Developer Tools section.
-
Choose
wildrydes-unicorn-apifrom the list of pipelines. -
Choose
Edit.
-
Choose
+ActionbelowBeta. -
In the Add action dialog, select
Deployfor the Action category. -
Enter
CreateChangeSetfor the Action name. -
Select
AWS CloudFormationfor the Deployment provider. -
Select
Create or replace a change setfor Action mode -
Enter
wildrydes-unicorn-api-betafor Stack name -
Enter
wildrydes-unicorn-api-beta-changesetfor Change set name -
Enter
MyAppBuild::app-sam-output.yamlfor Template -
Select
CAPABILITY_IAMfor Capabilities -
Enter
WildRydesUnicornApiCloudFormationfor Role name -
Enter
MyAppBuildfor Input artifacts #1 -
Choose Add Action
-
Choose
+ActionbelowCreateChangeSet. -
In the Add action dialog, select
Deployfor the Action category. -
Enter
ExecuteChangeSetfor the Action name. -
Select
AWS CloudFormationfor the Deployment provider. -
Select
Execute a change setfor Action mode -
Enter
wildrydes-unicorn-api-betafor Stack name -
Enter
wildrydes-unicorn-api-beta-changesetfor Change set name -
Choose Add Action
-
Choose
+ActionbelowExecuteChangeSet. -
In the Add action dialog, select
Invokefor the Action category. -
Enter
InvokeLambdaTestFunctionfor the Action name. -
Select
AWS Lambdafor the Deployment provider. -
Enter the Lambda Test Function Name identified in Step 1 for Function name
-
Enter
wildrydes-unicorn-api-betafor User parameters -
Choose Add Action
The pipeline should look like the following screenshot after adding the new Test stage.
-
Scroll to the top of the pipeline and choose
Save pipeline changes -
Choose
Save and Continuewhen prompted by the Save Pipeline Changes dialog.
The addition of the Beta stage is complete. You will now validate the Beta stage is working by triggering a release of the current change.
-
Choose the Release change button to start the pipeline.
-
Choose Release when prompted by the dialog box.
-
From the AWS Management Console, click on Services and then select CodePipeline in the Developer Tools section.
-
Choose
wildrydes-unicorn-apifrom the list of pipelines. -
Observe that each stage's color will turn blue during execution and green on completion. You should see that the
InvokeLambdaTestFunctionaction in theBetastage fails, causing the stage to turn red, like the following image. -
Choose the Details link in the failed action to see the details of the failed job.
The test_list_unicorns integration test has failed! Next, let's locate and fix the bug.
- On your workstation, open the
unicorn-api/app/list.jsfile and naviagte to line 17, which should look like the following code snippet:
docClient.scan(params, function(error, data) {
// Comment or Delete the following line of code to remove simulated error
error = Error("something is wrong");
-
Comment or delete Line 17 to fix the code bug
-
Save the
unicorn-api/app/list.jsfile.
The CodePipeline that you will create in the next step will be triggered by updates to a deployment package in S3.
-
Change directory to
aws-serverless-workshops-master/DevOps/4_MultipleEnvironments/unicorn-api. -
To create a deployment for this project, zip the contents of the
unicorn-apidirectory into a file namedunicorn-api.zip, which is your deployment package. If you are unsure how to zip the files in theunicorn-apidirectory, follow the instructions for Microsoft workstations here, and macOS workstations here.
Important
Zip the directory content, not the directory. The contents of the Zip file are available as the current working directory of the Lambda function. For example: /app-sam.yaml
-
In the AWS Management Console, choose Services then select S3 under Storage.
-
Browse the list of Buckets or use the search box to find the S3 bucket that you identified previously.
-
Choose Upload
-
Choose Add files, select the local copy of
unicorn-api.zipand then choose Upload in the bottom left corner of the dialog.
-
From the AWS Management Console, click on Services and then select CodePipeline in the Developer Tools section.
-
Choose
wildrydes-unicorn-apifrom the list of pipelines. -
Observe that each stage's color will turn blue during execution and green on completion. You should see that the
InvokeLambdaTestFunctionin theBetastage passes, causing the stage to turn green, like the following image.
Following the successful execution of all stages, the pipeline should look like the following screenshot.
Congratulations, you've successfully completed the Multiple Environment CI/CD Pipeline Module!


















