Skip to content

Instantly share code, notes, and snippets.

//Variables with example values behind comment
const volumeEncryptionKey = ssm.StringParameter.valueForStringParameter(this, '/acme/cdk/ec2/VolumeKmsKeyId'); //arn:aws:kms:eu-west-1:2222222222222:key/1234567-98765432-asdf-987654321234
var servername = ssm.StringParameter.valueForStringParameter(this, '/acme/cdk/ec2/app-mysql/servername'); //app-mysql-001
const environment = ssm.StringParameter.valueForStringParameter(this, '/acme/cdk/ec2/app-mysql/environment'); //test
const serverkey = ssm.StringParameter.valueForStringParameter(this, '/acme/cdk/ec2/app-mysql/serverkey'); //acmekey, key must exist in account
const serverdescription = ssm.StringParameter.valueForStringParameter(this, '/acme/cdk/ec2/app-mysql/serverdescription');//mysql server
var instancetypeparam = ssm.StringParameter.valueForStringParameter(this, '/acme/cdk/ec2/app-mysql/instancetype'); //t3.medium
const domainJoinDocument = ssm.StringParameter.valueForStringParameter(this, '/acme/cdk/ec2/app-mysql/domainJoinD
//Set default value to be t3.medium
var instancetype = ec2.InstanceType.of(
ec2.InstanceClass.T3,
ec2.InstanceSize.MEDIUM
);
//Check the instancetypeparam from ssm param store and change the instance type accordingly
if (instancetypeparam == "m5.large"){
instancetype = ec2.InstanceType.of(
ec2.InstanceClass.M5,
ec2.InstanceSize.LARGE
//Create vpc object from the vpc in the account that has the name acme-standard-vpc
const vpc = ec2.Vpc.fromLookup(this, 'VPC', {
vpcName: 'acme-standard-vpc'
});
//Get subnetid from another stack
var subnet1Id = cdk.Fn.importValue('infra-acme-network-stack-private-sn-1');
//Create the subnet selection which is the type the L2 construct Instance expect
const subnet = vpc.selectSubnets({
subnets:[
ec2.Subnet.fromSubnetAttributes(this, 'subnet1', {
subnetId: subnet1Id,
availabilityZone: 'eu-west-1a'
})
//Get subnetid from static value, you could also use CfnImport to import from another stack
var publicSubnet1Id = "subnet-98765432123456";
//Create the subnet selection which is the type the nlb requires
const publicSubnet = vpc.selectSubnets({
subnets:[
ec2.Subnet.fromSubnetAttributes(this, 'publicSubnet1', {
subnetId: publicSubnet1Id,
availabilityZone: 'eu-west-1a'
})
//Network load balancer
const nlb = new elbv2.NetworkLoadBalancer(this, 'NLB', {
vpc,
internetFacing: true,
vpcSubnets: publicSubnet,
loadBalancerName: "app-mysql-nlb-public",
crossZoneEnabled: false
});
cdk.Tags.of(nlb).add("Name", servername+"-network-load-balancer");
//Create the IAM Role
const role = new iam.Role(this, 'app-mysql-server-role', {
assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com')
})
//Add some AWS managed policies to that role
role.addManagedPolicy(iam.ManagedPolicy.fromManagedPolicyArn(this, "AmazonSSMAutomationRole", "arn:aws:iam::aws:policy/service-role/AmazonSSMAutomationRole"));
role.addManagedPolicy(iam.ManagedPolicy.fromManagedPolicyArn(this, "AmazonSSMManagedInstanceCore", "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"));
//Create the underlying json for the custom policy
const securityGroup = new ec2.SecurityGroup(this,'app-mysql-server-sg',
{
vpc: vpc,
allowAllOutbound: true, // will let your instance send outboud traffic
securityGroupName: 'app-mysql-server-sg',
})
cdk.Tags.of(securityGroup).add("Name", servername+"-security-group");
const publicIp = require('public-ip');
async function waitForIP() {
var ip = await publicIp.v4();
// console.log(ip);
return ip;
}
function setLaptopIngressRule() {
waitForIP().then(result => securityGroup.addIngressRule(ec2.Peer.ipv4(result+"/32"), ec2.Port.tcp(3389), 'Allows rdp access from laptop public'));
}
const dVolume = new ec2.Volume(this, 'Data', {
availabilityZone: 'eu-west-1a',
size: cdk.Size.gibibytes(volumeDsize),
encrypted: true,
volumeType: ec2.EbsDeviceVolumeType.GP3,
encryptionKey: kms.Key.fromKeyArn(this, 'VolumeEncryptionKey', volumeEncryptionKey),
});
cdk.Tags.of(dVolume).add("Name", servername+"-d-volume");