Skip to content

Latest commit

 

History

History

apigw-canary-deployment-cdk-java

Amazon API Gateway Canary Deployment

This pattern creates an Amazon API Gateway RESTful API, an AWS Lambda function, and then creates an APIGW Canary Deployment using the AWS Cloud Development Kit (AWS CDK) in Java.

Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/apigw-canary-deployment-cdk.

Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the AWS Pricing page for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.

Requirements

Deployment Instructions

  1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:

    git clone https://github.com/aws-samples/serverless-patterns
    
  2. Change directory to the pattern directory:

    cd serverless-patterns/apigw-canary-deployment-cdk-java
    
  3. From the command line, use AWS CDK to deploy the AWS resources for the serverless application as specified in the MyServerlessApplicationStack.java file:

    cdk deploy MyServerlessApplicationStack
    
  4. From the command line, use AWS CDK to deploy a different version of the Lambda function:

    cdk deploy MyServerlessApplicationStack -c env=dev
    
  5. Note the outputs from the CDK deployment process. These contain the API Gateway ID which is used for testing. If you navigate to the ApiGw URL you should be getting Production Version all the time.

  6. From the command line, use AWS CDK to deploy the AWS resources for the canary deployment as specified in the app.py file:

    cdk deploy CanaryDeploymentStack
    

How it works

The API Gateway Canary Deployment will route 50% of the traffic to the new function version created using the updated code.

Testing

Either from the command line, run the following command to send an HTTP GET request to APIs endpoint. Note that you must edit the {MyServerlessApplicationStack.ApigwId} and {Region} placeholder with the ID of the deployed API and Region that it is deployed in. This is provided in the MyServerlessApplicationStack deployment outputs.

curl -H "Origin: https://www.example.com" "http://{MyServerlessApplicationStack.ApigwId}.execute-api.{Region}.amazonaws.com/prod"

Or simply navigate to the url in our browser.

Since the canary deployment is set at 50% traffic, when you run the above command more than once you should see the old version's and new version's output at a rate of about 50/50.

The -H "Origin: https://www.example.com" is the third party domain making the request, which you can substitute for other domains.

Cleanup

  1. Delete the Canary Deployment stack
    cdk destroy CanaryDeploymentStack
    
  2. Delete the Serverless application stack
    cdk destroy MyServerlessApplicationStack
    

Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: MIT-0