Integration & Automation
Emulate your test environment using a service virtualization on AWS
When DevOps teams must test using external components that aren’t readily available, for example because those components are restricted, too costly, or owned by third parties, service virtualization can be an efficient alternative. With service virtualization, you can simulate the behavior of those components using application programming interfaces (APIs) without actually having to access the live components in production.
In this post, I explain how to create a service virtualization for a continuous integration and continuous delivery (CI/CD) pipeline test environment that depends on an external API that’s not always available. My solution uses Amazon API Gateway to create a mock API that simulates the external API and an AWS CloudFormation template that automates running two customizable REST operations (GET and POST). Using the provided CloudFormation template, you set up the expected requests, configure the logic in your endpoints so that the appropriate response is selected, and use the response in your test environment. The result is a collection of mock endpoints that you can customize in your local test environment.
The advantages of using the provided CloudFormation template include the following:
- The template already contains the base code, so edit only the YAML code using your preferred IDE or text editor.
- CloudFormation automatically checks for syntax problems in the template.
- CloudFormation creates and deletes the entire stack.
Note: I don’t discuss permissions and advanced options, such as Lambda integration, in this post. I also don’t discuss security aspects of the configuration described here because each organization handles security differently, based on their own needs and requirements.
About this blog post | |
Time to read | ~10 min. |
Time to complete | ~15 min. |
Cost to complete | If using the free tier, $0 as you don’t use more than 1 million requests per month. If not using the free tier, then the cost is a few cents a month. |
Learning level | Intermediate (200) |
AWS services | Amazon API Gateway AWS CloudFormation |
Overview
Figure 1 depicts the workflow of the service virtualization.
Figure 1: Service virtualization workflow
- You (the developer) either run the application in test mode or do a Postman call.
- The template creates a stack with mock endpoints that simulate the service virtualization.
- The API Gateway endpoints respond to the requests based on the information given to each mock endpoint in the virtualized service.
Process overview
In the exercises that follow, you perform the following three steps:
- Deploy the stack with the provided AWS CloudFormation template.
- Learn about two mock REST API operations created in API Gateway.
- Test the service virtualization using Postman.
Prerequisites
This post assumes that you have the following:
- An AWS account. If you don’t have an account, sign up for one at http://aws.amazon.com
- Access to the AWS Management Console to deploy the CloudFormation stack
- An Amazon API Gateway license
- Basic knowledge of YAML
- Basic knowledge of Postman (or a similar tool, such as Insomnia) to call API endpoints
Walkthrough
Complete the following steps using the AWS Management Console.
Step 1: Deploy the AWS CloudFormation stack
- Download the CloudFormation template to your local machine to provision the resources.
- Navigate to the AWS CloudFormation console.
- Choose Create stack.
- Choose Template is ready.
- Choose Upload a template file.
- Choose Choose file, and specify the YAML template file that you downloaded to your local machine in the first step.
- Choose Next.
- Specify the stack name (for example, ServiceVirtualization).
- Choose Next.
- Add at least one tag (key-value pair) (for example, System: ServiceVirtualization or Testing: ServiceVirtualization).
- Choose Next.
- Review your settings, and choose Create stack.
- Wait about a minute for the stack creation process to complete. You may need to refresh the page to see the CREATE_COMPLETE status.
Keep this screen open so you can explore the stack’s API resources in the next step.
Step 2: Explore the REST operations
The GET operation contains a request with a query-string parameter that defines which response the mock API returns. Possible responses include status codes 200, 201, 500, and 503, each with a different message.
- With your new stack open in the AWS Management Console, choose the Resources tab.
- To open the API Gateway console, choose the RestApi link.
- In the tree view, choose the GET operation.
The following four Method Execution blocks display.
Method Request
Expand the URL Query String Parameters section, and locate the preconfigured query-string parameter called method. Note that the parameter is required.
Method Response
The Method Response option has four possible HTTP status responses: 200, 201, 500, and 503. You can use JSON to configure each response.
Integration Request
Using the Integration Request option, specify information about the target backend that should be called by the operation. In this example, the integration type is Mock.
In the Mapping Templates section, choose application/json to open the mapping template that was created by the CloudFormation template.
Here, you can set up your expected requests and responses, based on your mock endpoint. The goal is to provide the Mock operation a logical way to choose a status code, based on the parameter.
As an example, I created the following if/then statements using Velocity Template Language (VTL):
- If the scope value is ok, then the statusCode value is 200.
- If the scope value is created, then the statusCode value is 201.
- If the scope value is internalerror, then the statusCode value is 500.
- If the scope value is anything other than ok, created, or internalerror, then the statusCode value is 503.
Note that you can use other statements, such as set (to set a parameter with a value) or foreach (to make a loop statement), among others.
Integration Response
The Integration Response page defines the mappings of the four responses that the Mock operation uses for status codes (200, 201, 500, or 503).
Each mapping has a corresponding mapping template with a JSON response that you can modify.
Step 3: Test the service virtualization
Use Postman to test the service virtualization endpoints using an example collection I wrote. Note that you can use another similar tool if desired.
- Download the Postman collection at https://aws-quickstart.s3.us-east-1.amazonaws.com/quickstart-examples/blog-assets/svcvirt-apigateway-cfn/AWS%20Blog%20Post.SVwithAPIGateway.postman_collection.json to your local machine.
- Open the Postman application, and choose Import to import the collection that you downloaded step 1.
- After the collection is imported into Postman, locate the GET and POST operations to use with the service virtualization.
- Open both operations, and replace https://put-you-api-gateway-stage-address-here/v0 with your own API Gateway address.
GET operation
The query parameter key called method supports the following four options:
- ok: returns 200
- created: returns 201
- internalerror: returns 500
- Anything else (even empty): returns 503
Test each option to see how they return the status code with the corresponding message. For example, here is the method=created option:
POST operation
The POST operation returns a value that’s based on the status code in the body. If the status code is anything with 2xx, the operation returns status code 200.
If the status code in the body is anything with 5xx, it returns an error message.
Cleanup
When you finish testing and exploring the service virtualization, remove the resources by deleting the AWS CloudFormation stack.
- Navigate to the CloudFormation console.
- In the main navigation pane, choose Stacks.
- Choose the mock API stack, and choose Delete.
- Wait a few minutes for the stack to be deleted. To update the screen, choose Refresh, located next to Delete.
Conclusion
In this post, you learned about service virtualizations and how they are ideal when you need to test an application that uses an external API that’s not always available. I showed you how to use Amazon API Gateway and AWS CloudFormation to create your own service virtualization for your CI/CD environment. Now you can simulate calling the external API without actually calling the real API in production.
To learn more about API Gateway and mock integrations, refer to the following:
- Create a REST API by importing an example
- Set up mock integrations in API Gateway
- Enable mock integration using the API Gateway console
To use AWS Lambda to add more options to your service virtualization, follow the Build an API Gateway REST API with Lambda integration tutorial.