Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-2.1· AnthropicHugging Face Arena Human Preference 55K

Can you write me a piece of CDKvs2 code with a unit test for creating a VPC?

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-instant-1’s answer.
  1. User

    Can you write me a piece of CDKvs2 code with a unit test for creating a VPC?

  2. Claude Assistant

    Here is an example CDK v2 code for creating a VPC with a unit test:

    import { Construct } from 'constructs';
    import { App, TerraformStack, Testing } from 'cdktf';
    import { AwsProvider, ec2 } from './.gen/providers/aws';
    
    class MyStack extends TerraformStack {
      constructor(scope: Construct, id: string) {
        super(scope, id);
        
        new AwsProvider(this, 'aws', {
          region: 'us-east-1'
        });
    
        new ec2.Vpc(this, 'myVpc', {
          cidrBlock: "10.0.0.0/16"
        });
      }
    }
    
    const app = new App();
    new MyStack(app, 'test');
    
    const stack = Testing.synth(app);
    
    test('VPC Created', () => {
      expect(stack).toHaveResource('aws_vpc', {
        cidr_block: "10.0.0.0/16"
      });
    });
    

    The key points:

    • Create a TerraformStack extending the CDK Construct
    • Add an AWS provider
    • Create the VPC resource
    • Synth the app to a template to test
    • Validate in a test that the VPC resource exists

    This tests that the VPC is synthesized as expected. Additional tests could validate other attributes of the VPC.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.