Webhook Listener
A Python Lambda Webhook Listener - Generates a permanent URL on created assets. See doc on Generating a Permanent URL: https://www.contentstack.com/docs/developers/apis/content-management-api/#generate-permanent-asset-url
This repository is not officially supported by Contentstack
Use the issue tracker in this repository or contact [email protected]
Note: This code can only be executed once on every asset. The permanent URL cannot be changed after generating it.
Developed using Python 3.9.
Step by step:
1. Create a Lambda function and an API Gateway in AWS Lambda. Based on these documentation articles:
- TUTORIAL: Build an API Gateway API with Lambda Non-Proxy Integration.
- AWS Lambda Deployment Package in Python
- (Optional) To run locally - run the
testLambda.pyscript - Note: In the test script, you will need to change the asset uid to make it work on a asset on your stack. You will also need to change the secret key to match your secret environmental variable (See step 2).
2. Environmental variables needed:
CS_APIKEY-> API Key of the Stack.CS_MANAGEMENTTOKEN-> Management Token on the stack with write accessCS_REGION-> Either NA (North America Region) or EU (Europe Region)CS_SECRET-> Defined secret - Used as a custom header in the webhook settings in Contentstack.- Note: The header value in Contentstack (
secret) needs to match the value of this environmental variable.
- Note: The header value in Contentstack (
You can define those environmental variables in your OS and run testLambda.py. You will need to define them in the Lambda configuration for it to work in a real world scenario.
3. It's good to update both code and configuration in Lambda with awscli.
- To update code via
awscli: * install pip module into a subdirectory like this:pip install --target ./package requests. * Add all files and folders from thepackagefolder (not thepackagefolder itself) into a zip file with thelambda_function.py, along with theconfigandcmafolders. Finally upload the zip file to Lambda like this:aws lambda update-function-code --function-name <Name of your Lambda function> --zip-file fileb://function.zip
4. Define a webhook in Contentstack.
- Give it a descriptive name
- The URL should be the URL generated by the API Gateway in step 1.
- Leave authentication information empty
- Define a custom header
secret- Give it the same value as theCS_SECRETvariable in step 2. - Define the condition for the webhook: When
Any->AssetisCreated. - Make sure the webhook is not set to concise and is enabled (The two checkmarks at the bottom).