Skip to content

Instantly share code, notes, and snippets.

@hoang-innomize
Created November 12, 2019 03:16
Show Gist options
  • Save hoang-innomize/d9f4f9e5e7e9d9ba431b83366d293f59 to your computer and use it in GitHub Desktop.
Save hoang-innomize/d9f4f9e5e7e9d9ba431b83366d293f59 to your computer and use it in GitHub Desktop.
An example of using AWS CDK to create a new VPC on AWS
import * as cdk from '@aws-cdk/core';
import * as ec2 from '@aws-cdk/aws-ec2';
export class NetworkStack extends cdk.Stack {
public readonly vpc: ec2.Vpc;
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
this.vpc = new ec2.Vpc(this, 'VPC', {
cidr: '10.0.0.0/16',
natGatewaySubnets: {
subnetName: 'Public'
},
subnetConfiguration: [
{
cidrMask: 26,
name: 'Public',
subnetType: ec2.SubnetType.PUBLIC
},
{
name: 'Application',
subnetType: ec2.SubnetType.PRIVATE
},
{
cidrMask: 27,
name: 'Database',
subnetType: ec2.SubnetType.ISOLATED
}
]
});
const vpcSecurityGroup = new ec2.SecurityGroup(this, 'SecurityGroup', {
vpc: this.vpc,
description: 'Allow ssh access to ec2 instances',
allowAllOutbound: true
});
vpcSecurityGroup.addIngressRule(
ec2.Peer.anyIpv4(),
ec2.Port.tcp(22),
'allow ssh access from the world'
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment