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


Figure 1 depicts the workflow of the service virtualization.

Service virtualization workflow

Figure 1: Service virtualization workflow

  1. You (the developer) either run the application in test mode or do a Postman call.
  2. The template creates a stack with mock endpoints that simulate the service virtualization.
  3. 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:

  1. Deploy the stack with the provided AWS CloudFormation template.
  2. Learn about two mock REST API operations created in API Gateway.
  3. Test the service virtualization using Postman.


This post assumes that you have the following:

  • An AWS account. If you don’t have an account, sign up for one at
  • 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


Complete the following steps using the AWS Management Console.

Step 1: Deploy the AWS CloudFormation stack

  1. Download the CloudFormation template to your local machine to provision the resources.
  2. Navigate to the AWS CloudFormation console.
  3. Choose Create stack.
  4. Choose Template is ready.
  5. Choose Upload a template file.
  6. Choose Choose file, and specify the YAML template file that you downloaded to your local machine in the first step.
  7. Choose Next.
  8. Specify the stack name (for example, ServiceVirtualization).
  9. Choose Next.
  10. Add at least one tag (key-value pair) (for example, System: ServiceVirtualization or Testing: ServiceVirtualization).
  11. Choose Next.
  12. Review your settings, and choose Create stack.
  13. Wait about a minute for the stack creation process to complete. You may need to refresh the page to see the CREATE_COMPLETE status.

Event status page

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.

  1. With your new stack open in the AWS Management Console, choose the Resources tab.
  2. To open the API Gateway console, choose the RestApi link.
  3. In the tree view, choose the GET operation.

The following four Method Execution blocks display.

API Gateway Resources page

Method Request

Expand the URL Query String Parameters section, and locate the preconfigured query-string parameter called method. Note that the parameter is required.

GET Method Request

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.

Method 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.

Integration Request

In the Mapping Templates section, choose application/json to open the mapping template that was created by the CloudFormation template.

Mapping 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).

Integration response

Each mapping has a corresponding mapping template with a JSON response that you can modify.

Mapping template

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.

  1. Download the Postman collection at to your local machine.
  2. Open the Postman application, and choose Import to import the collection that you downloaded step 1.
  3. After the collection is imported into Postman, locate the GET and POST operations to use with the service virtualization.
  4. Open both operations, and replace https://put-you-api-gateway-stage-address-here/v0 with your own API Gateway address.

GET operation

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:

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.

POST operation

If the status code in the body is anything with 5xx, it returns an error message.

GET operation


When you finish testing and exploring the service virtualization, remove the resources by deleting the AWS CloudFormation stack.

  1. Navigate to the CloudFormation console.
  2. In the main navigation pane, choose Stacks.
  3. Choose the mock API stack, and choose Delete.
  4. Wait a few minutes for the stack to be deleted. To update the screen, choose Refresh, located next to Delete.


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:

To use AWS Lambda to add more options to your service virtualization, follow the Build an API Gateway REST API with Lambda integration tutorial.

About the author

Vinicius Pereira Headshot

Vinicius Pereira

Vinicius is a technical program manager at eero Engineering (Amazon Devices). He holds a PhD in Computer Science, is a published author, and has led the delivery of the main work stream in one of the largest migration projects ever delivered by AWS. Vinicius helps internal teams improve their daily work, always with the customers’ needs in mind, and he enjoys doing it because it gives him an opportunity to be creative and learn new technologies to help design better solutions. See Vinicius’ LinkedIn profile to connect.