SwitchboardStack Reference
The SwitchboardStack is the main CDK stack class for deploying Amazon Connect resources. It handles orchestration, dependency management, and resource reference resolution.
Class Overview
public class SwitchboardStack : Stack
{
public SwitchboardStack(
Construct scope,
string id,
string instanceAlias,
IStackProps? props = null
)
}Namespace: Switchboard.Core
Constructor
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
scope | Construct | Yes | The CDK app or parent construct |
id | string | Yes | Unique stack identifier |
instanceAlias | string | Yes | Connect instance friendly name |
props | IStackProps? | No | CDK stack properties (environment, tags, etc.) |
Example
var app = new App();
var stack = new SwitchboardStack(app, "MyContactCenter", "my-center");With Environment
var stack = new SwitchboardStack(app, "ProdCenter", "prod-center", new StackProps
{
Env = new Amazon.CDK.Environment
{
Account = "123456789012",
Region = "us-east-1"
}
});Properties
Instance
Type: ConnectInstanceConstruct
The Connect instance construct created by the stack.
var instanceConstruct = stack.Instance;InstanceArn
Type: string
The ARN of the Connect instance.
var arn = stack.InstanceArn;
// "arn:aws:connect:us-east-1:123456789012:instance/abc123"InstanceId
Type: string
The unique ID of the Connect instance.
var id = stack.InstanceId;
// "abc123"InstanceAlias
Type: string
The friendly name of the Connect instance.
var alias = stack.InstanceAlias;
// "my-center"Methods
AddHoursOfOperation()
Adds hours of operation to the stack.
public HoursOfOperationConstruct AddHoursOfOperation(HoursOfOperation hoursOfOperation)Parameters:
hoursOfOperation- The hours configuration
Returns: HoursOfOperationConstruct
Throws:
ArgumentNullExceptionif hoursOfOperation is nullInvalidOperationExceptionif validation failsInvalidOperationExceptionif hours with same name already exists
Example:
var hours = new HoursOfOperation
{
Name = "BusinessHours",
TimeZone = "America/New_York"
};
hours.AddDayConfig(new HoursOfOperationConfig
{
Day = DayOfWeek.Monday,
StartTime = new TimeRange { Hours = 9, Minutes = 0 },
EndTime = new TimeRange { Hours = 17, Minutes = 0 }
});
var hoursConstruct = stack.AddHoursOfOperation(hours);AddQueue()
Adds a queue to the stack, optionally associating it with hours of operation.
public QueueConstruct AddQueue(Queue queue, string? hoursOfOperationName = null)Parameters:
queue- The queue configurationhoursOfOperationName(optional) - Name of existing hours to associate
Returns: QueueConstruct
Throws:
ArgumentNullExceptionif queue is nullInvalidOperationExceptionif validation failsInvalidOperationExceptionif queue with same name already existsInvalidOperationExceptionif hoursOfOperationName specified but not found
Example:
// Without hours
var queue = new QueueBuilder()
.SetName("Sales")
.SetMaxContacts(100)
.Build();
var queueConstruct = stack.AddQueue(queue);
// With hours
var queueWithHours = new QueueBuilder()
.SetName("Support")
.Build();
stack.AddQueue(queueWithHours, "BusinessHours");AddRoutingProfile()
Adds a routing profile to the stack.
public RoutingProfileConstruct AddRoutingProfile(RoutingProfile routingProfile)Parameters:
routingProfile- The routing profile configuration
Returns: RoutingProfileConstruct
Throws:
ArgumentNullExceptionif routingProfile is nullInvalidOperationExceptionif validation failsInvalidOperationExceptionif routing profile with same name already exists
Example:
var profile = new RoutingProfileBuilder()
.SetName("GeneralAgent")
.AddMediaConcurrency(ChannelType.Voice, 1)
.AddQueue("Sales", ChannelType.Voice, 1)
.Build();
var profileConstruct = stack.AddRoutingProfile(profile);AddFlow()
Adds a contact flow to the stack with automatic reference resolution and dependency management.
public FlowConstruct AddFlow(IFlow flow)Parameters:
flow- The flow configuration
Returns: FlowConstruct
Throws:
ArgumentNullExceptionif flow is nullInvalidOperationExceptionif validation failsInvalidOperationExceptionif flow with same name already exists
Example:
var flow = new FlowBuilder()
.SetName("MainFlow")
.PlayPrompt("Welcome")
.TransferToQueue("Sales") // Uses placeholder { {Queue:Sales} }
.Build();
var flowConstruct = stack.AddFlow(flow); // Placeholder resolved to actual ARNAutomatic Features:
- Resolves
{ {Queue:QueueName} }placeholders to actual queue ARNs - Adds CloudFormation dependencies on referenced resources
- Validates flow content before deployment
GetQueue()
Retrieves a queue construct by name.
public QueueConstruct? GetQueue(string name)Parameters:
name- The queue name
Returns: QueueConstruct? - The queue construct if found; otherwise, null
Example:
var salesQueue = stack.GetQueue("Sales");
if (salesQueue != null)
{
var arn = salesQueue.QueueArn;
}GetHoursOfOperation()
Retrieves hours of operation construct by name.
public HoursOfOperationConstruct? GetHoursOfOperation(string name)Parameters:
name- The hours name
Returns: HoursOfOperationConstruct? - The hours construct if found; otherwise, null
Example:
var hours = stack.GetHoursOfOperation("BusinessHours");
if (hours != null)
{
var arn = hours.HoursOfOperationArn;
}Resource Reference Resolution
The stack automatically resolves resource references in flow content.
Queue References
// In FlowBuilder
var flow = new FlowBuilder()
.SetName("Transfer")
.TransferToQueue("Sales") // Creates placeholder: { {Queue:Sales} }
.Build();
// In stack
stack.AddQueue(salesQueue); // Registers queue with name "Sales"
stack.AddFlow(flow); // Replaces { {Queue:Sales} } with actual ARNPlaceholder Format: { {Queue:QueueName} }
Resolved To: arn:aws:connect:us-east-1:123456789012:instance/abc/queue/xyz
Future Reference Types
The framework is designed to support additional reference types:
{ {Lambda:FunctionName} }- Lambda function ARNs{ {HoursOfOperation:Name} }- Hours of operation ARNs{ {Flow:FlowName} }- Other flow ARNs
Dependency Management
The stack automatically manages CloudFormation dependencies:
// Add resources in any order
stack.AddFlow(flow); // References "Sales" queue
stack.AddQueue(salesQueue); // Queue named "Sales"
// CloudFormation will deploy in correct order:
// 1. Instance
// 2. Queue
// 3. Flow (depends on queue)Automatic Dependency Detection:
- Scans flow content for resource references
- Adds CloudFormation dependencies using
Node.AddDependency() - Ensures resources are created in correct order
Validation
The stack validates resources before adding them:
var queue = new Queue { Name = "" }; // Invalid - empty name
stack.AddQueue(queue); // Throws InvalidOperationExceptionValidation Checks:
- Resource names are not empty
- Required properties are set
- Configurations are valid
- No duplicate resource names
Complete Example
using Amazon.CDK;
using Switchboard.Core;
using Switchboard.Builders;
var app = new App();
var stack = new SwitchboardStack(app, "ProductionCenter", "acme-prod-center", new StackProps
{
Env = new Amazon.CDK.Environment
{
Account = "123456789012",
Region = "us-east-1"
}
});
// Add hours of operation
var hours = new HoursOfOperation
{
Name = "BusinessHours",
TimeZone = "America/New_York"
};
hours.AddDayConfig(new HoursOfOperationConfig
{
Day = DayOfWeek.Monday,
StartTime = new TimeRange { Hours = 9, Minutes = 0 },
EndTime = new TimeRange { Hours = 17, Minutes = 0 }
});
stack.AddHoursOfOperation(hours);
// Add queues
var salesQueue = new QueueBuilder()
.SetName("Sales")
.SetMaxContacts(100)
.Build();
stack.AddQueue(salesQueue, "BusinessHours");
var supportQueue = new QueueBuilder()
.SetName("Support")
.SetMaxContacts(150)
.Build();
stack.AddQueue(supportQueue, "BusinessHours");
// Add routing profile
var profile = new RoutingProfileBuilder()
.SetName("GeneralAgent")
.AddMediaConcurrency(ChannelType.Voice, 1)
.AddQueue("Sales", ChannelType.Voice, 1)
.AddQueue("Support", ChannelType.Voice, 2)
.Build();
stack.AddRoutingProfile(profile);
// Add flow
var flow = new FlowBuilder()
.SetName("MainFlow")
.PlayPrompt("Welcome to our contact center")
.GetCustomerInput("Press 1 for sales, 2 for support")
.Branch(branch =>
{
branch.Case("1", "sales-transfer");
branch.Case("2", "support-transfer");
branch.Otherwise("default");
})
.TransferToQueue("Sales")
.Build();
stack.AddFlow(flow);
// Synthesize
app.Synth();Best Practices
1. Use Environment Configuration
Always specify account and region for production:
// Good
var stack = app.CreateStack(
"ProdStack",
"prod",
account: "123456789012",
region: "us-east-1"
);
// Avoid (environment undefined)
var stack = app.CreateStack("ProdStack", "prod");2. Configure Resources Before Synth
Add all resources before calling Synth():
var app = new App();
var stack = new SwitchboardStack(app, "MyStack", "center");
// Add all resources
stack.AddHoursOfOperation(hours);
stack.AddQueue(queue1);
stack.AddQueue(queue2);
stack.AddFlow(flow);
// Then synth
app.Synth();3. Use Meaningful Names
Use descriptive names for resources:
// Good
var stack = new SwitchboardStack(app, "ProductionContactCenter", "acme-prod");
// Avoid
var stack = new SwitchboardStack(app, "Stack1", "s1");4. Leverage Automatic Dependency Management
Let the stack handle dependencies:
// Good - Stack handles dependencies
stack.AddQueue(queue);
stack.AddFlow(flow); // Flow references queue, dependency added automatically
// Avoid - Manual dependency management
var queueConstruct = new QueueConstruct(this, "Queue", props);
var flowConstruct = new FlowConstruct(this, "Flow", flowProps);
flowConstruct.Node.AddDependency(queueConstruct); // Manual, error-proneRelated
- Building Queues - Creating queues
- Building Flows - Creating contact flows
- Building Routing Profiles - Creating routing profiles
- Building Hours - Creating hours of operation
- Complete Example - Full contact center example