The scheduler template (Documentation Index
Fetch the complete documentation index at: https://mintlify.com/ti-infinite/GSMInfrastructure/llms.txt
Use this file to discover all available pages before exploring further.
devops/scheduler/template.yml) automates cost savings by shutting down and restarting the GSM platform’s EC2 ECS host on a weekday schedule aligned to Colombia Time (COT, UTC−5). Two Python Lambda functions handle the stop and start sequences: the stop function scales all four ECS services to zero, disassociates the Elastic IP (to avoid idle EIP charges), and stops the EC2 instance; the start function reverses the sequence — starts the instance, waits for it to reach the running state, reassociates the EIP, then scales ECS services back to one. An EventBridge Scheduler group drives both functions on configurable cron expressions.
Parameters
The deployment environment. Used to prefix all resource names.Allowed values:
dev, qa, prodThe application name. Combined with
Environment in all Lambda, role, schedule, and log group names.The ID of the ECS host EC2 instance to stop and start (e.g.
i-0abc123def456). Passed to both Lambda functions as the INSTANCE_ID environment variable.The allocation ID of the Elastic IP (e.g.
eipalloc-0abc123def456). The EIP is disassociated before the instance shuts down and reassociated after it starts up, preventing the idle-EIP hourly charge. Passed to both Lambda functions as the EIP_ALLOCATION_ID environment variable.Name of the ECS cluster that contains the services to scale. Passed to both Lambda functions as the
CLUSTER_NAME environment variable. Use the ECSClusterName output from the infrastructure stack.Name of the ECS Gateway service to scale down/up.
Name of the ECS Auth service to scale down/up.
Name of the ECS Application service to scale down/up.
Name of the ECS Operations service to scale down/up.
EventBridge Scheduler cron expression (UTC) for the start schedule. The default fires at 09:00 UTC on Monday–Saturday, which corresponds to 04:00 AM COT.
EventBridge Scheduler cron expression (UTC) for the stop schedule. The default fires at 01:00 UTC on Monday–Saturday, which corresponds to 08:00 PM COT the previous evening.
Resources
SchedulerLambdaRole
| Field | Value |
|---|---|
| Type | AWS::IAM::Role |
| Name pattern | {Environment}-{AppName}-scheduler-lambda-role |
| Trusted by | lambda.amazonaws.com |
| Managed policy | AWSLambdaBasicExecutionRole (CloudWatch Logs write access) |
EC2EIPECSAccess grants the following permissions (all resources — *):
| Service | Actions |
|---|---|
| EC2 | StopInstances, StartInstances, DescribeInstances, DescribeInstanceStatus |
| EC2 (EIP) | AssociateAddress, DisassociateAddress, DescribeAddresses |
| ECS | UpdateService, DescribeServices |
StopFunction
| Field | Value |
|---|---|
| Type | AWS::Lambda::Function |
| Name pattern | {Environment}-{AppName}-ec2-stop |
| Runtime | python3.12 |
| Architecture | arm64 |
| Handler | index.handler |
| Timeout | 120 seconds |
| Role | SchedulerLambdaRole |
desiredCount=0, waits 10 seconds for the ECS agent to process the scale-down, disassociates the Elastic IP, then stops the EC2 instance.
StartFunction
| Field | Value |
|---|---|
| Type | AWS::Lambda::Function |
| Name pattern | {Environment}-{AppName}-ec2-start |
| Runtime | python3.12 |
| Architecture | arm64 |
| Handler | index.handler |
| Timeout | 300 seconds |
| Role | SchedulerLambdaRole |
instance_running waiter (polling every 10 seconds, up to 30 attempts), reassociates the Elastic IP, then waits 30 seconds for the ECS agent to register before scaling all services to desiredCount=1.
StopFunctionLogGroup / StartFunctionLogGroup
| Field | Value |
|---|---|
| Type | AWS::Logs::LogGroup |
| Retention | 14 days |
| Deletion policy | Delete |
| Resource | Log group name pattern |
|---|---|
StopFunctionLogGroup | /aws/lambda/{Environment}-{AppName}-ec2-stop |
StartFunctionLogGroup | /aws/lambda/{Environment}-{AppName}-ec2-start |
SchedulerExecutionRole
| Field | Value |
|---|---|
| Type | AWS::IAM::Role |
| Name pattern | {Environment}-{AppName}-eventbridge-scheduler-role |
| Trusted by | scheduler.amazonaws.com |
InvokeLambdas grants lambda:InvokeFunction on both StopFunction.Arn and StartFunction.Arn.
SchedulerGroup
| Field | Value |
|---|---|
| Type | AWS::Scheduler::ScheduleGroup |
| Name pattern | {Environment}-{AppName}-ec2-schedules |
StopScheduleWeekdays
| Field | Value |
|---|---|
| Type | AWS::Scheduler::Schedule |
| Name pattern | {Environment}-{AppName}-stop-lun-sab |
| Expression | SchedulerStopExpression (default: cron(0 1 ? * MON-SAT *)) |
| Timezone | UTC |
| Target | StopFunction |
| Retry policy | 2 maximum retry attempts; 3600 seconds maximum event age |
| Input | {"action": "stop", "trigger": "weekday-night"} |
StartScheduleWeekdays
| Field | Value |
|---|---|
| Type | AWS::Scheduler::Schedule |
| Name pattern | {Environment}-{AppName}-start-lun-sab |
| Expression | SchedulerStartExpression (default: cron(0 9 ? * MON-SAT *)) |
| Timezone | UTC |
| Target | StartFunction |
| Retry policy | 2 maximum retry attempts; 3600 seconds maximum event age |
| Input | {"action": "start", "trigger": "weekday-morning"} |
StopFunctionPermission / StartFunctionPermission
| Field | Value |
|---|---|
| Type | AWS::Lambda::Permission |
| Action | lambda:InvokeFunction |
| Principal | scheduler.amazonaws.com |
| Source ARN | arn:aws:scheduler:{region}:{account}:schedule/{SchedulerGroup}/* |
SchedulerGroup to prevent cross-group invocation.
Lambda environment variables
BothStopFunction and StartFunction receive an identical set of environment variables:
| Variable | Source | Description |
|---|---|---|
INSTANCE_ID | EC2InstanceId parameter | The EC2 instance ID (e.g. i-0abc123def456) to stop or start. Used in all ec2 API calls that target a specific instance. |
EIP_ALLOCATION_ID | EIPAllocationId parameter | The Elastic IP allocation ID (e.g. eipalloc-0abc123). Used to disassociate the EIP before shutdown and reassociate it after the instance reaches the running state. |
CLUSTER_NAME | ECSClusterName parameter | The ECS cluster name that contains the four microservice services (e.g. dev-gsmapplication-cluster). |
GATEWAY_SERVICE | GatewayServiceName parameter | Name of the ECS Gateway service passed to ecs:UpdateService for scale-down/up. |
AUTH_SERVICE | AuthServiceName parameter | Name of the ECS Auth service passed to ecs:UpdateService for scale-down/up. |
APPLICATION_SERVICE | ApplicationServiceName parameter | Name of the ECS Application service passed to ecs:UpdateService for scale-down/up. |
OPERATIONS_SERVICE | OperationsServiceName parameter | Name of the ECS Operations service passed to ecs:UpdateService for scale-down/up. |
Outputs
| Output | Description |
|---|---|
StopFunctionArn | ARN of the StopFunction Lambda. Use to verify the function was created or to invoke it manually for testing. |
StartFunctionArn | ARN of the StartFunction Lambda. Use to verify the function was created or to invoke it manually for testing. |
SchedulerGroupName | Name of the EventBridge Scheduler group (e.g. dev-GSMApplication-ec2-schedules). |
HorarioCOT | Human-readable summary of the configured schedule: Stop Lun-Sab 9pm COT | Start Lun-Sab 7am COT. |