Skip to content

Commit

Permalink
feat: add vpc
Browse files Browse the repository at this point in the history
  • Loading branch information
briancaffey committed Apr 26, 2021
1 parent 4b7dacf commit f289c92
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
/lib
/test-reports/
build/Release
cdk.out
coverage
jspm_packages/
junit.xml
Expand Down
1 change: 1 addition & 0 deletions .projenrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ const project = new AwsCdkConstructLibrary({
// dependabot: true, /* Include dependabot configuration. */
// dependabotOptions: undefined, /* Options for dependabot. */
// gitignore: undefined, /* Additional entries to .gitignore. */
gitignore: ['cdk.out'],
// jest: true, /* Setup jest unit tests. */
// jestOptions: undefined, /* Jest options. */
// jsiiReleaseVersion: 'latest', /* Version requirement of `jsii-release` which is used to publish modules to npm. */
Expand Down
51 changes: 51 additions & 0 deletions cdk.context.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"vpc-provider:account=733623710918:filter.isDefault=true:region=us-east-1:returnAsymmetricSubnets=true": {
"vpcId": "vpc-5426b62c",
"vpcCidrBlock": "172.31.0.0/16",
"availabilityZones": [],
"subnetGroups": [
{
"name": "Public",
"type": "Public",
"subnets": [
{
"subnetId": "subnet-91c151da",
"cidr": "172.31.16.0/20",
"availabilityZone": "us-east-1a",
"routeTableId": "rtb-dc457ba6"
},
{
"subnetId": "subnet-21559c7c",
"cidr": "172.31.32.0/20",
"availabilityZone": "us-east-1b",
"routeTableId": "rtb-dc457ba6"
},
{
"subnetId": "subnet-1bc1a67f",
"cidr": "172.31.0.0/20",
"availabilityZone": "us-east-1c",
"routeTableId": "rtb-dc457ba6"
},
{
"subnetId": "subnet-5150847e",
"cidr": "172.31.80.0/20",
"availabilityZone": "us-east-1d",
"routeTableId": "rtb-dc457ba6"
},
{
"subnetId": "subnet-9f8e37a0",
"cidr": "172.31.64.0/20",
"availabilityZone": "us-east-1e",
"routeTableId": "rtb-dc457ba6"
},
{
"subnetId": "subnet-7fada173",
"cidr": "172.31.48.0/20",
"availabilityZone": "us-east-1f",
"routeTableId": "rtb-dc457ba6"
}
]
}
]
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,4 @@
}
},
"//": "~~ Generated by projen. To modify, edit .projenrc.js and run \"npx projen\"."
}
}
16 changes: 13 additions & 3 deletions src/django-cdk.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// import * as ec2 from '@aws-cdk/aws-ec2';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as s3 from '@aws-cdk/aws-s3';
import * as cdk from '@aws-cdk/core';
// import * as ecs from '@aws-cdk/aws-ecs';
Expand All @@ -7,21 +7,31 @@ export interface DjangoCdkProps {
/**
* Options to configure a Django CDK project
*/
readonly bucketName: string;
readonly bucketName?: string;
readonly vpc?: ec2.IVpc;
}

export class DjangoCdk extends cdk.Construct {

public staticFileBucket: s3.Bucket;
public vpc: ec2.IVpc;

constructor(scope: cdk.Construct, id: string, props? : DjangoCdkProps) {
super(scope, id);

/**
* VPC must have public, private and isolated subnets
*/
const vpc = props?.vpc ?? ec2.Vpc.fromLookup(this, 'DefaultVpc', {
isDefault: true,
});
this.vpc = vpc;

/**
* static files bucket name is derived from the Construct id if not provided
*/
const staticFilesBucket = new s3.Bucket(scope, 'StaticBucket', {
bucketName: props?.bucketName && `${id}-static-files-bucket`,
bucketName: props?.bucketName,
});

new cdk.CfnOutput(this, 'bucketName', { value: staticFilesBucket.bucketName! });
Expand Down
6 changes: 5 additions & 1 deletion src/integ.default.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import * as cdk from '@aws-cdk/core';
import { DjangoCdk } from './index';

const env = {
region: process.env.AWS_DEFAULT_REGION || 'us-east-1',
account: process.env.AWS_ACCOUNT_ID,
};
const app = new cdk.App();
const stack = new cdk.Stack(app, 'MyStack');
const stack = new cdk.Stack(app, 'MyStack', { env });

new DjangoCdk(stack, 'Cdk-Sample-Lib', {
bucketName: 'my-django-cdk-static-files-bucket',
Expand Down
26 changes: 23 additions & 3 deletions test/djangoCdk.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
import * as ec2 from '@aws-cdk/aws-ec2';
import * as cdk from '@aws-cdk/core';
import { DjangoCdk } from '../src/index';
import '@aws-cdk/assert/jest';

test('create app', () => {
const env = {
region: 'us-east-1',
account: '12345678',
};
const app = new cdk.App();
const stack = new cdk.Stack(app);
new DjangoCdk(stack, 'TestDjangoCdkStack');
const stack = new cdk.Stack(app, 'MyStack', { env });
const vpc = new ec2.Vpc(stack, 'myVpc');
new DjangoCdk(stack, 'TestDjangoCdkStack', { bucketName: 'my-bucket', vpc });
expect(stack).toHaveResource('AWS::S3::Bucket');
});
expect(stack).toHaveResource('AWS::EC2::VPC');
});

test('create app with default bucket name', () => {
const env = {
region: 'us-east-1',
account: '12345678',
};
const app = new cdk.App();
const stack = new cdk.Stack(app, 'MyStack', { env });
const vpc = new ec2.Vpc(stack, 'myVpc');
new DjangoCdk(stack, 'TestDjangoCdkStack', { vpc });
expect(stack).toHaveResource('AWS::S3::Bucket');
expect(stack).toHaveResource('AWS::EC2::VPC');
});

0 comments on commit f289c92

Please sign in to comment.