Scalable targets define the resource to be scaled and its capacity constraints. They serve as the foundation for all scaling policies and scheduled scaling actions.
The primary class for creating scalable targets that can be used with scaling policies.
/**
* Define a scalable target for Application Auto Scaling
*/
class ScalableTarget extends Resource implements IScalableTarget {
/**
* Create a new ScalableTarget
* @param scope - CDK construct scope
* @param id - Construct identifier
* @param props - ScalableTarget properties
*/
constructor(scope: Construct, id: string, props: ScalableTargetProps);
/**
* Import an existing scalable target by its ID
* @param scope - CDK construct scope
* @param id - Construct identifier
* @param scalableTargetId - The scalable target ID to import
* @returns IScalableTarget interface
*/
static fromScalableTargetId(scope: Construct, id: string, scalableTargetId: string): IScalableTarget;
/**
* ID of the Scalable Target
* Example: service/ecsStack-MyECSCluster-AB12CDE3F4GH/ecsStack-MyECSService-AB12CDE3F4GH|ecs:service:DesiredCount|ecs
*/
readonly scalableTargetId: string;
/**
* The role used to give AutoScaling permissions to your resource
*/
readonly role: iam.IRole;
/**
* Add a policy statement to the role's policy
* @param statement - IAM policy statement to add
*/
addToRolePolicy(statement: iam.PolicyStatement): void;
/**
* Scale out or in based on time
* @param id - Identifier for the scheduled action
* @param action - Scaling schedule configuration
*/
scaleOnSchedule(id: string, action: ScalingSchedule): void;
/**
* Scale out or in, in response to a metric
* @param id - Identifier for the step scaling policy
* @param props - Step scaling policy configuration
* @returns Created StepScalingPolicy
*/
scaleOnMetric(id: string, props: BasicStepScalingPolicyProps): StepScalingPolicy;
/**
* Scale out or in in order to keep a metric around a target value
* @param id - Identifier for the target tracking policy
* @param props - Target tracking policy configuration
* @returns Created TargetTrackingScalingPolicy
*/
scaleToTrackMetric(id: string, props: BasicTargetTrackingScalingPolicyProps): TargetTrackingScalingPolicy;
}
/**
* Properties for a scalable target
*/
interface ScalableTargetProps {
/**
* The minimum value that Application Auto Scaling can use to scale a target during a scaling activity.
*/
readonly minCapacity: number;
/**
* The maximum value that Application Auto Scaling can use to scale a target during a scaling activity.
*/
readonly maxCapacity: number;
/**
* Role that allows Application Auto Scaling to modify your scalable target.
* @default A role is automatically created
*/
readonly role?: iam.IRole;
/**
* The resource identifier to associate with this scalable target.
* This string consists of the resource type and unique identifier.
* Example: service/ecsStack-MyECSCluster-AB12CDE3F4GH/ecsStack-MyECSService-AB12CDE3F4GH
*/
readonly resourceId: string;
/**
* The scalable dimension that's associated with the scalable target.
* Specify the service namespace, resource type, and scaling property.
* Example: ecs:service:DesiredCount
*/
readonly scalableDimension: string;
/**
* The namespace of the AWS service that provides the resource
*/
readonly serviceNamespace: ServiceNamespace;
}
/**
* Interface for scalable targets
*/
interface IScalableTarget extends IResource {
/**
* The scalable target ID
*/
readonly scalableTargetId: string;
}Usage Examples:
import * as appscaling from '@aws-cdk/aws-applicationautoscaling';
import * as iam from '@aws-cdk/aws-iam';
// Lambda provisioned concurrency scaling
const lambdaTarget = new appscaling.ScalableTarget(this, 'LambdaTarget', {
serviceNamespace: appscaling.ServiceNamespace.LAMBDA,
maxCapacity: 100,
minCapacity: 10,
resourceId: `function:${handler.functionName}:${version.version}`,
scalableDimension: 'lambda:function:ProvisionedConcurrency',
});
// DynamoDB table scaling
const dynamoTarget = new appscaling.ScalableTarget(this, 'DynamoTarget', {
serviceNamespace: appscaling.ServiceNamespace.DYNAMODB,
maxCapacity: 100,
minCapacity: 5,
resourceId: `table/${table.tableName}`,
scalableDimension: 'dynamodb:table:ReadCapacityUnits',
});
// Add custom IAM permissions
lambdaTarget.addToRolePolicy(new iam.PolicyStatement({
actions: ['lambda:InvokeFunction'],
resources: [handler.functionArn],
}));Abstract base class for creating service-specific scalable attributes that wrap ScalableTarget functionality.
/**
* Represent an attribute for which autoscaling can be configured
* This class provides protected methods for derived service-specific classes
*/
abstract class BaseScalableAttribute extends Construct {
/**
* Create a new BaseScalableAttribute
* @param scope - CDK construct scope
* @param id - Construct identifier
* @param props - BaseScalableAttribute properties
*/
constructor(scope: Construct, id: string, props: BaseScalableAttributeProps);
/**
* Scale out or in based on time
* @param id - Identifier for the scheduled action
* @param props - Scaling schedule configuration
*/
protected doScaleOnSchedule(id: string, props: ScalingSchedule): void;
/**
* Scale out or in based on a metric value
* @param id - Identifier for the step scaling policy
* @param props - Step scaling policy configuration
*/
protected doScaleOnMetric(id: string, props: BasicStepScalingPolicyProps): void;
/**
* Scale out or in in order to keep a metric around a target value
* @param id - Identifier for the target tracking policy
* @param props - Target tracking policy configuration
*/
protected doScaleToTrackMetric(id: string, props: BasicTargetTrackingScalingPolicyProps): void;
}
/**
* Properties for a BaseScalableAttribute
*/
interface BaseScalableAttributeProps extends EnableScalingProps {
/**
* Service namespace of the scalable attribute
*/
readonly serviceNamespace: ServiceNamespace;
/**
* Resource ID of the attribute
*/
readonly resourceId: string;
/**
* Scalable dimension of the attribute
*/
readonly dimension: string;
/**
* Role to use for scaling
*/
readonly role: iam.IRole;
}
/**
* Properties for enabling Application Auto Scaling
*/
interface EnableScalingProps {
/**
* Minimum capacity to scale to
* @default 1
*/
readonly minCapacity?: number;
/**
* Maximum capacity to scale to
*/
readonly maxCapacity: number;
}All supported AWS service namespaces for Application Auto Scaling.
/**
* The service that supports Application AutoScaling
*/
enum ServiceNamespace {
/**
* Elastic Container Service
*/
ECS = 'ecs',
/**
* Elastic Map Reduce
*/
ELASTIC_MAP_REDUCE = 'elasticmapreduce',
/**
* Elastic Compute Cloud
*/
EC2 = 'ec2',
/**
* App Stream
*/
APPSTREAM = 'appstream',
/**
* Dynamo DB
*/
DYNAMODB = 'dynamodb',
/**
* Relational Database Service
*/
RDS = 'rds',
/**
* SageMaker
*/
SAGEMAKER = 'sagemaker',
/**
* Custom Resource
*/
CUSTOM_RESOURCE = 'custom-resource',
/**
* Lambda
*/
LAMBDA = 'lambda',
/**
* Comprehend
*/
COMPREHEND = 'comprehend',
/**
* Kafka
*/
KAFKA = 'kafka',
/**
* ElastiCache
*/
ELASTICACHE = 'elasticache',
}