0

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

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
    
  3. Create a virtual environment for Python

    python3 -m venv .venv
    
  4. Activate the virtual environment

    source .venv/bin/activate
    

    For a Windows platform, activate the virtualenv like this:

    .venv\Scripts\activate.bat
    
  5. Install the Python required dependencies:

    pip install -r requirements.txt
    
  6. 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
    
  7. Note the outputs from the CDK deployment process. These contain the API Gateway ID which is used for testing.

  8. 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:

    1. On the left panel, click functions.
    2. Click on the MyServerlessApplicationStac-MyFunction* function.
    3. Under the code tab, edit the Hello World code under the EDIT ME comment.
    4. Under the file menu in the code editor, click save.
    5. In the code editor, click the deploy button. This deploys the code to the $LATEST version.
  9. 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

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


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí