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 Python.
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
- 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
- Python 3.9+ installed
Deployment Instructions
-
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
-
Create a virtual environment for Python
python3 -m venv .venv
-
Activate the virtual environment
source .venv/bin/activate
For a Windows platform, activate the virtualenv like this:
.venvScriptsactivate.bat
-
Install the Python required dependencies:
pip install -r requirements.txt
-
From the command line, use AWS CDK to deploy the AWS resources for the serverless application as specified in the app.py file:
cdk deploy MyServerlessApplicationStack
-
Note the outputs from the CDK deployment process. These contain the API Gateway ID which is used for testing.
-
Once the serverless application stack is successfully deployed, you will need to deploy new Lambda code. This can be accomplished by performing the following on the Lambda page in the AWS console:
- On the left panel, click functions.
- Click on the
MyServerlessApplicationStac-MyFunction*
function. - Under the code tab, edit the Hello World code under the EDIT ME comment.
- Under the file menu in the code editor, click save.
- In the code editor, click the deploy button. This deploys the code to the $LATEST version.
-
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
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" "https://{MyServerlessApplicationStack.ApigwId}.execute-api.{Region}.amazonaws.com/prod"
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
- Delete the Canary Deployment stack
cdk destroy CanaryDeploymentStack
- Delete the Serverless application stack
cdk destroy MyServerlessApplicationStack
Nguồn: viblo.asia