Serverless function to trigger a scheduled HTTP request
- AWS CLI already configured with AdministratorAccess permission IAM Admin
- AWS SAM CLI installed SAM CLI
Compile binary function:
make build
Prepare a S3 bucket
to upload the binary Lambda function:
aws s3 mb s3://citium-builds --profile adminuser
Package Lambda function to S3:
sam package \
--template-file template.yaml \
--s3-bucket citium-builds \
--output-template-file template.output.yaml \
--profile adminuser
The returned file template.output.yaml
now should contain the CodeUri
that points to the artifact to be deployed.
Create a Cloudformation Stack and deploy SAM resources:
sam deploy \
--template-file template.output.yaml \
--stack-name citium-serverless \
--capabilities CAPABILITY_IAM \
--profile adminuser
After the deployment is complete, run the following command to retrieve stack info:
aws cloudformation describe-stacks --stack-name citium-serverless
The dynamodb table name for storing requests is citium_schedule
which could be overridden at packing step.
...
Parameters:
ScheduleTableName:
Type: String
Description: Name of the dynamodb table to be created & used by function
Default: citium_schedule
Default checking interval is 5 minutes.
...
Events:
PeriodicCheck:
Type: Schedule
Properties:
Schedule: rate(5 minutes)
Invoking function locally
sam local invoke TriggerAPIFunction --no-event --env-vars env.json --debug
NOTE: Template file should be modified before hand with added property CodeUri
pointing to current dir
...
TriggerAPIFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./
Handler: citium
Download cli tool from release or compile from source:
make build-tools
If persistent=false
then the scheduled request will be removed after successfully executed:
./citium-cli \
-action=create \
-table=citium_schedule \
-id=test-post-request \
-freeze=30m \
-method=POST \
-url=http://example.com \
-headers=Content-Type:application/json \
-persistent=true
To safely halt request execution, for the case of execution failure that needs manual intervention:
./citium-cli \
-action=unlock \
-table=citium_schedule \
-id=test-delete-resource
To release the execution lock:
./citium-cli \
-action=lock \
-table=citium_schedule \
-id=test-delete-resource
Optional extra values for API request authorization, base url, etc are configured via environment variables:
...
Environment:
Variables:
TABLE_NAME: !Ref ScheduleTableName
BASE_URL: ""
API_TOKEN: ""
USER_AGENT: citium/0.0.1