A simple web page monitoring app sending e-mail alerts about changes to a page.
- AWS : API Gateway, Lambda, DynamoDB, CloudWatch, SQS, SES
- SAM + Swagger (OpenAPI)
- NodeJS 8.10
- REST API, Security: API KEY
- AWS Account
- AWS CLI
- SAM CLI
- Node JS 8.10
- (Optional) jq
-
Create S3 bucket for deployment
BUCKET NAME
is where the deployment artefacts are placedaws s3api create-bucket --bucket [BUCKET NAME]
-
Upload Swagger (OpenAPI) file
aws s3 cp swagger.yaml s3://[BUCKET NAME]/
-
Build the project with SAM
sam build --build-dir ./dist --template sam.yaml
-
Package the project with SAM
sam package --s3-bucket [BUCKET NAME] --template-file dist/template.yaml --output-template-file dist/packaged.yaml
-
Deploy the project with CloudFormation
RECIPIENT EMAIL ADDRESS
is where the alert emails are sent about webpage content changesSENDER EMAIL ADDRESS
will show up as the sender for the alert emailsaws cloudformation deploy --template-file dist/packaged.yaml \ --stack-name WatcherAppStack \ --s3-bucket [BUCKET NAME] \ --capabilities CAPABILITY_NAMED_IAM \ --parameter-overrides \ "SwaggerLocation=s3://[BUCKET NAME]/swagger.yaml" \ "SESRecipient=[RECIPIENT EMAIL ADDRESS]" \ "SESSender=SENDER EMAIL ADDRESS"
There are optional parameters you can change
EnvironmentParameter=dev|prod
sets the environment for deploymentSESRegion=eu-west-1|us-east-1|us-west-2
sets the region for sending e-mails with SES
-
Get the output from the deployment
aws cloudformation describe-stacks --stack-name WatcherAppStack | jq .Stacks[0].Outputs
This command uses
jq
to extract the outputs from the JSON reponse. -
Verify sender and recipient e-mail addresses in the AWS SES console
-
Create an API key for authentication
aws apigateway create-api-key --enabled --name [API KEY NAME]
-
Add the key to the usage plan
PLAN ID
can be found in the deployment output, look for the object with"OutputKey": "UsagePlanId"
API KEY ID
look for the"id"
value in the reponse from creating the API keyaws apigateway create-usage-plan-key --key-type "API_KEY" --usage-plan-id [PLAN ID] --key-id [API KEY ID]
Remove the deployment (deleting stack) if the app is not needed anymore, or if something goes wrong during deployment that needs fixing before trying it again.
aws cloudformation delete-stack --stack-name WatcherAppStack
-
Create a new watcher item
API ROOT URL
can be found in the deployment output, look for the object with"OutputKey": "ApiRootUrl"
API KEY
is the value of the"value"
attribute from the response when the API key was createdcurl -X POST [API ROOT URL]/watcher \ -H "x-api-key:[API KEY]" \ -H "Content-Type: application/json" \ -d '{"location": "https://google.com/", "path": "//title/text()"}'
-
List the watcher items
curl -X GET [API ROOT URL]/watcher \ -H "x-api-key:[API KEY]"
-
Run a watcher manually to check for changes
WATCHER ID
is the value of the"id"
attribute of the watcher from the previous listingcurl -X GET [API ROOT URL]/watcher/[WATCHER ID]/run \ -H "x-api-key:[API KEY]"
-
Delete the watcher
curl -X DELETE [API ROOT URL]/watcher/[WATCHER ID] \ -H "x-api-key:[API KEY]"
NOTE: The scheduled timer will run the watcher automatically in every 6 hours if you do not delete it