Skip to content

Composite Providers ​

Composite providers allow you to combine multiple runner providers with different strategies. There are two types:

Fallback Strategy: Try providers in order until one succeeds. Useful for trying spot instances first, then falling back to on-demand if spot capacity is unavailable.

ts
import { CompositeProvider, EcsRunnerProvider, GitHubRunners } from '@cloudsnorkel/cdk-github-runners';

// Try spot instances first, fall back to on-demand if spot is unavailable
const ecsFallback = CompositeProvider.fallback(this, 'ECS Fallback', [
  new EcsRunnerProvider(this, 'ECS Spot', {
    labels: ['ecs', 'linux', 'x64'],
    spot: true,
    // ... other config
  }),
  new EcsRunnerProvider(this, 'ECS On-Demand', {
    labels: ['ecs', 'linux', 'x64'],
    spot: false,
    // ... other config
  }),
]);

new GitHubRunners(this, 'runners', {
  providers: [ecsFallback],
});
python
from cloudsnorkel.cdk_github_runners import CompositeProvider, EcsRunnerProvider, GitHubRunners


# Try spot instances first, fall back to on-demand if spot is unavailable
ecs_fallback = CompositeProvider.fallback(self, "ECS Fallback", [
    EcsRunnerProvider(self, "ECS Spot",
        labels=["ecs", "linux", "x64"],
        spot=True
    ),
    EcsRunnerProvider(self, "ECS On-Demand",
        labels=["ecs", "linux", "x64"],
        spot=False
    )
])

GitHubRunners(self, "runners",
    providers=[ecs_fallback]
)
java
import com.cloudsnorkel.cdk.github.runners.CompositeProvider;
import com.cloudsnorkel.cdk.github.runners.EcsRunnerProvider;
import com.cloudsnorkel.cdk.github.runners.GitHubRunners;


// Try spot instances first, fall back to on-demand if spot is unavailable
ICompositeProvider ecsFallback = CompositeProvider.fallback(this, "ECS Fallback", List.of(
    EcsRunnerProvider.Builder.create(this, "ECS Spot")
            .labels(List.of("ecs", "linux", "x64"))
            .spot(true)
            .build(),
    EcsRunnerProvider.Builder.create(this, "ECS On-Demand")
            .labels(List.of("ecs", "linux", "x64"))
            .spot(false)
            .build()));

GitHubRunners.Builder.create(this, "runners")
        .providers(List.of(ecsFallback))
        .build();
csharp
using CloudSnorkel;


// Try spot instances first, fall back to on-demand if spot is unavailable
var ecsFallback = CompositeProvider.Fallback(this, "ECS Fallback", new [] {
    new EcsRunnerProvider(this, "ECS Spot", new EcsRunnerProviderProps {
        Labels = new [] { "ecs", "linux", "x64" },
        Spot = true
    }),
    new EcsRunnerProvider(this, "ECS On-Demand", new EcsRunnerProviderProps {
        Labels = new [] { "ecs", "linux", "x64" },
        Spot = false
    }) });

new GitHubRunners(this, "runners", new GitHubRunnersProps {
    Providers = new [] { ecsFallback }
});
go
import "github.com/CloudSnorkel/cdk-github-runners-go/cloudsnorkelcdkgithubrunners"


// Try spot instances first, fall back to on-demand if spot is unavailable
ecsFallback := cloudsnorkelcdkgithubrunners.CompositeProvider_Fallback(this, jsii.String("ECS Fallback"), []IRunnerProvider{
	cloudsnorkelcdkgithubrunners.NewEcsRunnerProvider(this, jsii.String("ECS Spot"), &EcsRunnerProviderProps{
		Labels: []*string{
			jsii.String("ecs"),
			jsii.String("linux"),
			jsii.String("x64"),
		},
		Spot: jsii.Boolean(true),
	}),
	cloudsnorkelcdkgithubrunners.NewEcsRunnerProvider(this, jsii.String("ECS On-Demand"), &EcsRunnerProviderProps{
		Labels: []*string{
			jsii.String("ecs"),
			jsii.String("linux"),
			jsii.String("x64"),
		},
		Spot: jsii.Boolean(false),
	}),
})

cloudsnorkelcdkgithubrunners.NewGitHubRunners(this, jsii.String("runners"), &GitHubRunnersProps{
	Providers: []interface{}{
		ecsFallback,
	},
})

Weighted Distribution Strategy: Randomly select a provider based on weights. Useful for distributing load across multiple availability zones or instance types.

ts
import { aws_ec2 as ec2 } from 'aws-cdk-lib';
import { CompositeProvider, FargateRunnerProvider, GitHubRunners } from '@cloudsnorkel/cdk-github-runners';

declare const vpc: ec2.Vpc;

// Distribute 60% of traffic to AZ-1, 40% to AZ-2
const distributedProvider = CompositeProvider.distribute(this, 'Fargate Distribution', [
  {
    weight: 3, // 3/(3+2) = 60%
    provider: new FargateRunnerProvider(this, 'Fargate AZ-1', {
      labels: ['fargate', 'linux', 'x64'],
      subnetSelection: vpc.selectSubnets({
        availabilityZones: [vpc.availabilityZones[0]],
      }),
      // ... other config
    }),
  },
  {
    weight: 2, // 2/(3+2) = 40%
    provider: new FargateRunnerProvider(this, 'Fargate AZ-2', {
      labels: ['fargate', 'linux', 'x64'],
      subnetSelection: vpc.selectSubnets({
        availabilityZones: [vpc.availabilityZones[1]],
      }),
      // ... other config
    }),
  },
]);

new GitHubRunners(this, 'runners', {
  providers: [distributedProvider],
});
python
from aws_cdk import aws_ec2 as ec2
from cloudsnorkel.cdk_github_runners import CompositeProvider, FargateRunnerProvider, GitHubRunners

# vpc: ec2.Vpc


# Distribute 60% of traffic to AZ-1, 40% to AZ-2
distributed_provider = CompositeProvider.distribute(self, "Fargate Distribution", [
    weight=3,  # 3/(3+2) = 60%
    provider=FargateRunnerProvider(self, "Fargate AZ-1",
        labels=["fargate", "linux", "x64"],
        subnet_selection=vpc.select_subnets(
            availability_zones=[vpc.availability_zones[0]]
        )
    )
,
    weight=2,  # 2/(3+2) = 40%
    provider=FargateRunnerProvider(self, "Fargate AZ-2",
        labels=["fargate", "linux", "x64"],
        subnet_selection=vpc.select_subnets(
            availability_zones=[vpc.availability_zones[1]]
        )
    )

])

GitHubRunners(self, "runners",
    providers=[distributed_provider]
)
java
import software.amazon.awscdk.services.ec2.*;
import com.cloudsnorkel.cdk.github.runners.CompositeProvider;
import com.cloudsnorkel.cdk.github.runners.FargateRunnerProvider;
import com.cloudsnorkel.cdk.github.runners.GitHubRunners;

Vpc vpc;


// Distribute 60% of traffic to AZ-1, 40% to AZ-2
ICompositeProvider distributedProvider = CompositeProvider.distribute(this, "Fargate Distribution", List.of(WeightedRunnerProvider.builder()
        .weight(3) // 3/(3+2) = 60%
        .provider(FargateRunnerProvider.Builder.create(this, "Fargate AZ-1")
                .labels(List.of("fargate", "linux", "x64"))
                .subnetSelection(vpc.selectSubnets(SubnetSelection.builder()
                        .availabilityZones(List.of(vpc.getAvailabilityZones()[0]))
                        .build()))
                .build())
        .build(), WeightedRunnerProvider.builder()
        .weight(2) // 2/(3+2) = 40%
        .provider(FargateRunnerProvider.Builder.create(this, "Fargate AZ-2")
                .labels(List.of("fargate", "linux", "x64"))
                .subnetSelection(vpc.selectSubnets(SubnetSelection.builder()
                        .availabilityZones(List.of(vpc.getAvailabilityZones()[1]))
                        .build()))
                .build())
        .build()));

GitHubRunners.Builder.create(this, "runners")
        .providers(List.of(distributedProvider))
        .build();
csharp
using Amazon.CDK.AWS.EC2;
using CloudSnorkel;

Vpc vpc;


// Distribute 60% of traffic to AZ-1, 40% to AZ-2
var distributedProvider = CompositeProvider.Distribute(this, "Fargate Distribution", new [] { new WeightedRunnerProvider {
    Weight = 3,  // 3/(3+2) = 60%
    Provider = new FargateRunnerProvider(this, "Fargate AZ-1", new FargateRunnerProviderProps {
        Labels = new [] { "fargate", "linux", "x64" },
        SubnetSelection = vpc.SelectSubnets(new SubnetSelection {
            AvailabilityZones = new [] { vpc.AvailabilityZones[0] }
        })
    })
}, new WeightedRunnerProvider {
    Weight = 2,  // 2/(3+2) = 40%
    Provider = new FargateRunnerProvider(this, "Fargate AZ-2", new FargateRunnerProviderProps {
        Labels = new [] { "fargate", "linux", "x64" },
        SubnetSelection = vpc.SelectSubnets(new SubnetSelection {
            AvailabilityZones = new [] { vpc.AvailabilityZones[1] }
        })
    })
} });

new GitHubRunners(this, "runners", new GitHubRunnersProps {
    Providers = new [] { distributedProvider }
});
go
import "github.com/aws/aws-cdk-go/awscdk"
import "github.com/CloudSnorkel/cdk-github-runners-go/cloudsnorkelcdkgithubrunners"

var vpc Vpc


// Distribute 60% of traffic to AZ-1, 40% to AZ-2
distributedProvider := cloudsnorkelcdkgithubrunners.CompositeProvider_Distribute(this, jsii.String("Fargate Distribution"), []WeightedRunnerProvider{
	&WeightedRunnerProvider{
		Weight: jsii.Number(3),
		 // 3/(3+2) = 60%
		Provider: *cloudsnorkelcdkgithubrunners.NewFargateRunnerProvider(this, jsii.String("Fargate AZ-1"), &FargateRunnerProviderProps{
			Labels: []*string{
				jsii.String("fargate"),
				jsii.String("linux"),
				jsii.String("x64"),
			},
			SubnetSelection: vpc.selectSubnets(&SubnetSelection{
				AvailabilityZones: []*string{
					vpc.AvailabilityZones[jsii.Number(0)],
				},
			}),
		}),
	},
	&WeightedRunnerProvider{
		Weight: jsii.Number(2),
		 // 2/(3+2) = 40%
		Provider: *cloudsnorkelcdkgithubrunners.NewFargateRunnerProvider(this, jsii.String("Fargate AZ-2"), &FargateRunnerProviderProps{
			Labels: []*string{
				jsii.String("fargate"),
				jsii.String("linux"),
				jsii.String("x64"),
			},
			SubnetSelection: vpc.selectSubnets(&SubnetSelection{
				AvailabilityZones: []*string{
					vpc.*AvailabilityZones[jsii.Number(1)],
				},
			}),
		}),
	},
})

cloudsnorkelcdkgithubrunners.NewGitHubRunners(this, jsii.String("runners"), &GitHubRunnersProps{
	Providers: []interface{}{
		distributedProvider,
	},
})

Important: All providers in a composite must have the exact same labels. This ensures any provisioned runner can match the labels requested by the GitHub workflow job.

Released under the Apache-2.0 License.