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.py
script - 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.
awscli
.
3. It's good to update both code and configuration in Lambda with - To update code via
awscli
: * install pip module into a subdirectory like this:pip install --target ./package requests
. * Add all files and folders from thepackage
folder (not thepackage
folder itself) into a zip file with thelambda_function.py
, along with theconfig
andcma
folders. 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_SECRET
variable in step 2. - Define the condition for the webhook: When
Any
->Asset
isCreated
. - Make sure the webhook is not set to concise and is enabled (The two checkmarks at the bottom).