Skip to content

Commit 8415f6b

Browse files
committed
using singlenodeconstruct
1 parent ff3c3c3 commit 8415f6b

File tree

5 files changed

+109
-21
lines changed

5 files changed

+109
-21
lines changed

lib/allora/allora.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,25 @@
22
import 'dotenv/config';
33
import 'source-map-support/register';
44
import * as cdk from 'aws-cdk-lib';
5+
import * as ec2 from "aws-cdk-lib/aws-ec2";
6+
import * as constants from "../constructs/constants";
57
import { AlloraStack } from './lib/allora-stack';
68

9+
const parseDataVolumeType = (dataVolumeType: string) => {
10+
switch (dataVolumeType) {
11+
case "gp3":
12+
return ec2.EbsDeviceVolumeType.GP3;
13+
case "io2":
14+
return ec2.EbsDeviceVolumeType.IO2;
15+
case "io1":
16+
return ec2.EbsDeviceVolumeType.IO1;
17+
case "instance-store":
18+
return constants.InstanceStoreageDeviceVolumeType;
19+
default:
20+
return ec2.EbsDeviceVolumeType.GP3;
21+
}
22+
};
23+
724
const app = new cdk.App();
825
new AlloraStack(app, 'allora-single-node', {
926
stackName: 'allora-single-node',
@@ -16,5 +33,11 @@ new AlloraStack(app, 'allora-single-node', {
1633
vpcMaxAzs: Number(process.env.AWS_VPC_MAX_AZS || 1),
1734
vpcNatGateways: Number(process.env.AWS_VPC_NAT_GATEWAYS || 0),
1835
vpcSubnetCidrMask: Number(process.env.AWS_VPC_CIDR_MASK),
19-
resourceNamePrefix: process.env.AWS_RESOURCE_NAME_PREFIX || 'AlloraWorkerx'
36+
resourceNamePrefix: process.env.AWS_RESOURCE_NAME_PREFIX || 'AlloraWorkerx',
37+
dataVolume: {
38+
sizeGiB: process.env.EDGE_DATA_VOL_SIZE ? parseInt(process.env.EDGE_DATA_VOL_SIZE) : 256,
39+
type: parseDataVolumeType(process.env.EDGE_DATA_VOL_TYPE?.toLowerCase() ? process.env.EDGE_DATA_VOL_TYPE?.toLowerCase() : "gp3"),
40+
iops: process.env.EDGE_DATA_VOL_IOPS ? parseInt(process.env.EDGE_DATA_VOL_IOPS) : 10000,
41+
throughput: process.env.EDGE_DATA_VOL_THROUGHPUT ? parseInt(process.env.EDGE_DATA_VOL_THROUGHPUT) : 700
42+
}
2043
});

lib/allora/lib/allora-stack.ts

Lines changed: 72 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ import { Construct } from 'constructs';
33
import * as ec2 from 'aws-cdk-lib/aws-ec2';
44
import * as s3 from 'aws-cdk-lib/aws-s3';
55
import * as s3deploy from 'aws-cdk-lib/aws-s3-deployment';
6+
import { SingleNodeConstruct, SingleNodeConstructCustomProps } from "../../constructs/single-node"
67
import * as fs from 'fs';
78
import * as path from 'path';
8-
// import * as sqs from 'aws-cdk-lib/aws-sqs';
9+
import * as nag from "cdk-nag";
10+
import * as iam from "aws-cdk-lib/aws-iam";
11+
import * as configTypes from "../../constructs/config.interface";
912

1013

1114
export interface AlloraStackProps extends cdk.StackProps {
@@ -15,6 +18,7 @@ export interface AlloraStackProps extends cdk.StackProps {
1518
vpcNatGateways: number
1619
vpcSubnetCidrMask: number;
1720
resourceNamePrefix: string;
21+
dataVolume: configTypes.DataVolumeConfig;
1822
}
1923

2024

@@ -27,6 +31,7 @@ export class AlloraStack extends cdk.Stack {
2731
const amiId = props?.amiId || 'ami-04b70fa74e45c3917';
2832
const instanceType = props?.instanceType || 't2.medium';
2933
const resourceNamePrefix = props?.resourceNamePrefix || 'AlloraWorkerx';
34+
const dataVolume = props?.dataVolume;
3035

3136

3237

@@ -74,31 +79,80 @@ export class AlloraStack extends cdk.Stack {
7479
const ec2UserData = ec2.UserData.forLinux();
7580
ec2UserData.addCommands(modifiedUserData);
7681

77-
// EC2 Instance
78-
const instance = new ec2.Instance(this, `${resourceNamePrefix}Instance`, {
79-
vpc,
82+
// Getting the snapshot bucket name and IAM role ARN from the common stack
83+
const importedInstanceRoleArn = cdk.Fn.importValue("EdgeNodeInstanceRoleArn");
84+
85+
const instanceRole = iam.Role.fromRoleArn(this, "iam-role", importedInstanceRoleArn);
86+
87+
// Making sure our instance will be able to read the assets
88+
bucket.grantRead(instanceRole);
89+
90+
91+
// Define SingleNodeConstructCustomProps
92+
const singleNodeProps: SingleNodeConstructCustomProps = {
93+
instanceName: `${resourceNamePrefix}Instance`,
8094
instanceType: new ec2.InstanceType(instanceType),
81-
machineImage: ec2.MachineImage.genericLinux({
82-
[region]: amiId,
83-
}),
84-
vpcSubnets: {
85-
subnetType: ec2.SubnetType.PUBLIC,
86-
},
95+
dataVolumes: dataVolume ? [ dataVolume ] : [], // Define your data volumes here
96+
machineImage: ec2.MachineImage.genericLinux({ [region]: amiId }),
97+
role: instanceRole,
98+
vpc: vpc,
99+
rootDataVolumeDeviceName: '/dev/sda1',
87100
securityGroup: securityGroup,
88-
blockDevices: [{
89-
deviceName: '/dev/sda1',
90-
volume: ec2.BlockDeviceVolume.ebs(30, {
91-
volumeType: ec2.EbsDeviceVolumeType.GP3,
92-
}),
93-
}],
94-
userData: ec2UserData
95-
});
101+
availabilityZone: vpc.selectSubnets({ subnetType: ec2.SubnetType.PUBLIC }).availabilityZones[0],
102+
vpcSubnets: { subnetType: ec2.SubnetType.PUBLIC },
103+
};
104+
105+
// Instantiate SingleNodeConstruct
106+
const singleNode = new SingleNodeConstruct(this, `${resourceNamePrefix}SingleNode`, singleNodeProps);
107+
108+
const instance = singleNode.instance;
109+
110+
instance.addUserData(ec2UserData)
96111

97112
// Elastic IP
98113
const eip = new ec2.CfnEIP(this, `${resourceNamePrefix}EIP`);
99114
new ec2.CfnEIPAssociation(this, `${resourceNamePrefix}EIPAssociation`, {
100115
eip: eip.ref,
101116
instanceId: instance.instanceId,
102117
});
118+
119+
nag.NagSuppressions.addResourceSuppressions(
120+
this,
121+
[
122+
{
123+
id: "AwsSolutions-EC23",
124+
reason: "Inbound access from any IP is required for this application.",
125+
},
126+
{
127+
id: "AwsSolutions-IAM4",
128+
reason: "This IAM role requires broad permissions to function correctly.",
129+
},
130+
{
131+
id: "AwsSolutions-IAM5",
132+
reason: "Full access is needed for administrative tasks.",
133+
},
134+
{
135+
id: "AwsSolutions-S1",
136+
reason: "Server-side encryption is not required for this bucket.",
137+
},
138+
{
139+
id: "AwsSolutions-EC2",
140+
reason: "Unrestricted access is required for the instance to operate correctly.",
141+
},
142+
{
143+
id: "AwsSolutions-AS3",
144+
reason: "No notifications needed for this specific application.",
145+
},
146+
{
147+
id: "AwsSolutions-S2",
148+
reason: "Access logging is not necessary for this bucket.",
149+
},
150+
{
151+
id: "AwsSolutions-S10",
152+
reason: "HTTPS requirement is not needed for this bucket.",
153+
},
154+
],
155+
true
156+
);
103157
}
104158
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
AWS_RESOURCE_NAME_PREFIX="AlloraWorkerx"
21
AWS_ACCOUNT_ID="xxxxxxxxxxx"
2+
AWS_RESOURCE_NAME_PREFIX="AlloraWorkerx"
33
AWS_REGION="us-east-1"
44
AWS_AMI_ID="ami-04b70fa74e45c3917"
55
AWS_INSTANCE_TYPE="t2.medium"
66
AWS_VPC_MAX_AZS="1"
77
AWS_VPC_NAT_GATEWAYS="0"
88
AWS_VPC_CIDR_MASK="24"
99

10+
# Data volume configuration
11+
EDGE_DATA_VOL_TYPE="gp3" # Other options: "io1" | "io2" | "gp3" | "instance-store" . IMPORTANT: "instance-store" NOT recommended as it is ephermal and will be reset after stopping the instance. Use "instance-store" option only with instance types that support that feature, like popular for node g4dn, d3, i3en, and i4i instance families
12+
EDGE_DATA_VOL_SIZE="256" # Current required data size to keep both snapshot archive and unarchived version of it (not applicable for "instance-store")
13+
EDGE_DATA_VOL_IOPS="3000" # Max IOPS for EBS volumes (not applicable for "instance-store")
14+
EDGE_DATA_VOL_THROUGHPUT="125" # Max throughput for EBS gp3 volumes (not applicable for "io1" | "io2" | "instance-store")

lib/allora/test/.env-test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ AWS_VPC_MAX_AZS="1"
77
AWS_VPC_NAT_GATEWAYS="0"
88
AWS_VPC_CIDR_MASK="24"
99

10+
11+
# Data volume configuration
12+
EDGE_DATA_VOL_TYPE="gp3" # Other options: "io1" | "io2" | "gp3" | "instance-store" . IMPORTANT: "instance-store" NOT recommended as it is ephermal and will be reset after stopping the instance. Use "instance-store" option only with instance types that support that feature, like popular for node g4dn, d3, i3en, and i4i instance families
13+
EDGE_DATA_VOL_SIZE="256" # Current required data size to keep both snapshot archive and unarchived version of it (not applicable for "instance-store")
14+
EDGE_DATA_VOL_IOPS="3000" # Max IOPS for EBS volumes (not applicable for "instance-store")
15+
EDGE_DATA_VOL_THROUGHPUT="125" # Max throughput for EBS gp3 volumes (not applicable for "io1" | "io2" | "instance-store")

lib/allora/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"experimentalDecorators": true,
2323
"strictPropertyInitialization": false,
2424
"typeRoots": [
25-
"./node_modules/@types"
25+
"../../node_modules/@types"
2626
]
2727
},
2828
"exclude": [

0 commit comments

Comments
 (0)