Source code for this sample can be found on GitHub.
event-sdk-ecs¶
Sample FunctionGraph timer event function to start or stop an ECS instance via the ECS API and PHP curl.
Two variants are provided:
File |
Auth method |
|---|---|
|
AK/SK signing via |
|
Token via |
How it works¶
The function is triggered by a FunctionGraph Timer event. The user_event field of the timer event controls the action:
start— sends a batch start request to the ECS API (os-start)stop— sends a soft batch stop request to the ECS API (os-stop)
The ECS instance to act on is configured via the ECS_INSTANCE_ID user data variable.
Prerequisites¶
An OTC account with an ECS instance
An IAM agency granting FunctionGraph permission to start/stop ECS instances (created by Terraform)
Configuration¶
The following variables must be set in terraform/variables.tfvars:
Variable |
Description |
|---|---|
|
ID of the ECS instance to start/stop |
|
The ECS endpoint, defaults to |
Deploy¶
# Deploy with Terraform
make tf_apply
Test events¶
Two pre-configured test events are created by Terraform:
start — triggers
os-starton the configured instancestop — triggers a soft
os-stopon the configured instance
Example start event (resources/event_start.json):
{
"version": "v2.0",
"time": "2023-06-01T08:30:00+08:00",
"trigger_type": "TIMER",
"trigger_name": "Timer_start",
"user_event": "start"
}