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 buildPrepare a S3 bucket to upload the binary Lambda function:
aws s3 mb s3://citium-builds --profile adminuserPackage Lambda function to S3:
sam package \
--template-file template.yaml \
--s3-bucket citium-builds \
--output-template-file template.output.yaml \
--profile adminuserThe 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 adminuserAfter the deployment is complete, run the following command to retrieve stack info:
aws cloudformation describe-stacks --stack-name citium-serverlessThe 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_scheduleDefault 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 --debugNOTE: 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-toolsIf 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=trueTo safely halt request execution, for the case of execution failure that needs manual intervention:
./citium-cli \
-action=unlock \
-table=citium_schedule \
-id=test-delete-resourceTo release the execution lock:
./citium-cli \
-action=lock \
-table=citium_schedule \
-id=test-delete-resourceOptional 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