constructor()

in src/index.ts [73:158]


  constructor(scope: cdk.Construct, id: string, props: ISeleniumGridProps = {}) {
    super(scope, id);

    // Create new VPC if it doesnt exist
    this.vpc = props.vpc ?? new ec2.Vpc(this, 'Vpc', { natGateways: 1 });
    this.seleniumVersion = props.seleniumVersion ?? '3.141.59';
    this.memory = props.memory ?? 512;
    this.cpu = props.cpu ?? 256;
    this.seleniumNodeMaxInstances = props.seleniumNodeMaxInstances ?? 5;
    this.seleniumNodeMaxSessions = props.seleniumNodeMaxSessions ?? 5;
    this.minInstances = props.minInstances ?? 1;
    this.maxInstances = props.maxInstances ?? 10;

    // Cluster
    const cluster = new ecs.Cluster(this, 'cluster', {
      vpc: this.vpc,
      containerInsights: true
    });

    // Setup capacity providers and default strategy for cluster
    const cfnEcsCluster = cluster.node.defaultChild as ecs.CfnCluster;
    cfnEcsCluster.capacityProviders = ['FARGATE', 'FARGATE_SPOT'];
    cfnEcsCluster.defaultCapacityProviderStrategy = [{
      capacityProvider: 'FARGATE',
      weight: 1,
      base: 4,
    }, {
      capacityProvider: 'FARGATE_SPOT',
      weight: 4,
    }];

    // Create security group and add inbound and outbound traffic ports
    var securityGroup = new ec2.SecurityGroup(this, 'security-group-selenium', {
      vpc: cluster.vpc,
      allowAllOutbound: true,
    });

    // Open up port 4444 and 5555 for execution
    securityGroup.addIngressRule(ec2.Peer.anyIpv4(), ec2.Port.tcp(4444), 'Port 4444 for inbound traffic');
    securityGroup.addIngressRule(ec2.Peer.anyIpv4(), ec2.Port.tcp(5555), 'Port 5555 for inbound traffic');

    // Setup Load balancer & register targets
    var loadBalancer = new elbv2.ApplicationLoadBalancer(this, 'app-lb', {
      vpc: this.vpc,
      internetFacing: true,
    });
    loadBalancer.addSecurityGroup(securityGroup);

    // Register SeleniumHub resources
    this.createHubResources({
      cluster: cluster,
      identifier: 'hub',
      loadBalancer: loadBalancer,
      securityGroup: securityGroup,
      stack: this,
      maxInstances: this.maxInstances,
      minInstances: this.minInstances     
    });

    // Register Chrome node resources
    this.createBrowserResource({
      cluster: cluster,
      identifier: 'chrome',
      loadBalancer: loadBalancer,
      securityGroup: securityGroup,
      stack: this,
      maxInstances: this.maxInstances,
      minInstances: this.minInstances
    }, 'selenium/node-chrome');

    // Register Firefox node resources
    this.createBrowserResource({
      cluster: cluster,
      identifier: 'firefox',
      loadBalancer: loadBalancer,
      securityGroup: securityGroup,
      stack: this,
      maxInstances: this.maxInstances,
      minInstances: this.minInstances
    }, 'selenium/node-firefox');

    new cdk.CfnOutput(this, 'LoadBalancerDNS', {
      exportName: 'Selenium-Hub-DNS',
      value: loadBalancer.loadBalancerDnsName,
    });
  }