constructor()

in lib/module_1/ab_testing_module_1.ts [28:86]


  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const hostingBucket = new s3.Bucket(this, 'hosting-bucket');
    const myOrigin = new origins.S3Origin(hostingBucket);

    const myConfigBucket = new s3.Bucket(this, "my-config-ab-testing-bucket");

    const viewerRequestFunction = new cloudfront.Function(this, 'StatelessViewerRequest', {
      code: cloudfront.FunctionCode.fromInline(`
      var X_Experiment_A = 'index.html';
      var X_Experiment_B = 'index_b.html';

      function handler(event) {
        var request = event.request;
        if (Math.random() < 0.8) {
           request.uri = '/' + X_Experiment_A;
        } else {
           request.uri = '/' + X_Experiment_B;
        }
        console.log('X_Experiment_V ' + (request.uri == '/index.html' ? 'A_VERSION' : 'B_VERSION'));

        return request;
      }

      `),
    });

    new s3deployment.BucketDeployment(this, "myDeployment", {
      sources: [s3deployment.Source.asset("./resources/website")],
      destinationBucket: hostingBucket,
    });

    const myDistribution = new cloudfront.Distribution(this, "myDistribution", {
      defaultBehavior: {
        origin: myOrigin,
        viewerProtocolPolicy: cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
      },
      additionalBehaviors: {
        '/': {
          origin: myOrigin,
          viewerProtocolPolicy: cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
          functionAssociations: [{
            function: viewerRequestFunction,
            eventType: cloudfront.FunctionEventType.VIEWER_REQUEST,
          }],
        }
      },
      comment: "AB Testing Workshop - Module 1",
    });

    const dashboard = new ABDashboard(this, "MonitoringDashboard");
    dashboard.createModule1Dashboard(viewerRequestFunction.functionName, "ABTestingWorkshopModule1");

    new cdk.CfnOutput(this, "CloudFrontURL", {
      description: "The CloudFront distribution URL",
      value: "https://" + myDistribution.domainName,
    });
  }