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.
- Create an AWS account if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
- AWS CLI installed and configured
- Git Installed
- AWS CDK Toolkit installed and configured
- Java 11+ installed
- Docker Installed
-
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
-
Change directory to the pattern directory:
cd serverless-patterns/apigw-canary-deployment-cdk-java
-
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
-
From the command line, use AWS CDK to deploy a different version of the Lambda function:
cdk deploy MyServerlessApplicationStack -c env=dev
-
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.
-
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
The API Gateway Canary Deployment will route 50% of the traffic to the new function version created using the updated code.
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.
- Delete the Canary Deployment stack
cdk destroy CanaryDeploymentStack
- Delete the Serverless application stack
cdk destroy MyServerlessApplicationStack
Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0