amazon-web-servicesaws-cdk

Is there a way to specify timezone in an auto-scaling schedule?


I'm using CDK to autoscale Lambda provisioned concurrency on a schedule. For example:

// Define the provisioned concurrency
const target = new asg.ScalableTarget(this, 'ScalableTarget', {
  serviceNamespace: asg.ServiceNamespace.LAMBDA,
  maxCapacity: 1,
  minCapacity: 0,
  resourceId: `function:${alias.lambda.functionName}:${alias.aliasName}`,
  scalableDimension: 'lambda:function:ProvisionedConcurrency'
});

target.node.addDependency(alias);

// Start the provisioned concurrency at 8am
target.scaleOnSchedule('ScaleUpInTheMorning', {
  schedule: asg.Schedule.cron({ hour: '08', minute: '00' }),
  minCapacity: 1,
  maxCapacity: 1
});

// Stop the provisioned concurrency at night
target.scaleOnSchedule('ScaleDownAtNight', {
  schedule: asg.Schedule.cron({ hour: '17', minute: '10' }),
  minCapacity: 0
  maxCapacity: 0
});

From what I have read and my own testing, the time in the cron definition is in UTC.

Is there a way to specify the timezone?


Solution

  • You can achieve this with L1 constructs, since CloudFormation supports it: https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-applicationautoscaling.CfnScalableTarget.html

    I don't have any experience with TypeScript, so hopefully the following syntax is correct. In any case, you get the idea.

    const target = new asg.CfnScalableTarget(this, 'ScalableTarget', {
      serviceNamespace: asg.ServiceNamespace.LAMBDA,
      maxCapacity: 1,
      minCapacity: 0,
      resourceId: `function:${alias.lambda.functionName}:${alias.aliasName}`,
      scalableDimension: 'lambda:function:ProvisionedConcurrency',
      scheduledActions: {
        schedule: asg.Schedule.cron(
          { hour: '08', minute: '00' }).expressionString,
        scheduledActionName: 'morning',
        timezone: 'Pacific/Tahiti'
      },
      scalableTargetAction: {
        minCapacity: 1,
        maxCapacity: 1
      }
    });