@@ -4,47 +4,52 @@ import * as ec2 from 'aws-cdk-lib/aws-ec2';
4
4
import * as iam from 'aws-cdk-lib/aws-iam' ;
5
5
// import * as sqs from 'aws-cdk-lib/aws-sqs';
6
6
7
+
8
+ export interface AlloraStackProps extends cdk . StackProps {
9
+ amiId : string ;
10
+ instanceType : string ;
11
+ vpcMaxAzs : number ;
12
+ vpcNatGateways : number
13
+ vpcSubnetCidrMask : number ;
14
+ resourceNamePrefix : string ;
15
+ }
16
+
17
+
7
18
export class AlloraStack extends cdk . Stack {
8
- constructor ( scope : Construct , id : string , props ?: cdk . StackProps ) {
19
+ constructor ( scope : Construct , id : string , props ?: AlloraStackProps ) {
9
20
super ( scope , id , props ) ;
10
21
11
22
// Parameters
12
- // const resourceNamePrefixParam = new cdk.CfnParameter(this, 'ResourceNamePrefix', {
13
- // type: 'String',
14
- // description: 'Prefix for resource names to override AWS auto-generated naming convention',
15
- // });
16
-
17
- const instanceSizeParam = new cdk . CfnParameter ( this , 'InstanceSize' , {
18
- type : 'String' ,
19
- default : 't2.medium' ,
20
- description : 'EC2 Instance Size' ,
21
- } ) ;
23
+ const region = props ?. env ?. region || 'us-east-1' ;
24
+ const amiId = props ?. amiId || 'ami-04b70fa74e45c3917' ;
25
+ const instanceType = props ?. instanceType || 't2.medium' ;
26
+ const resourceNamePrefix = props ?. resourceNamePrefix || 'AlloraWorkerx' ;
22
27
23
28
// Create VPC
24
- const vpc = new ec2 . Vpc ( this , 'AlloraWorkerxVpc' , {
25
- maxAzs : 1 ,
26
- natGateways : 0 ,
29
+ const vpc = new ec2 . Vpc ( this , ` ${ resourceNamePrefix } Vpc` , {
30
+ maxAzs : props ?. vpcMaxAzs || 1 ,
31
+ natGateways : typeof props ?. vpcNatGateways !== 'undefined' ? props ?. vpcNatGateways : 0 ,
27
32
subnetConfiguration : [ {
28
- cidrMask : 24 ,
29
- name : `AlloraWorkerxPublicSubnet `,
33
+ cidrMask : props ?. vpcSubnetCidrMask || 24 ,
34
+ name :` ${ resourceNamePrefix } PublicSubnet `,
30
35
subnetType : ec2 . SubnetType . PUBLIC ,
31
36
} ]
32
37
} ) ;
33
38
34
39
// Security Group with inbound TCP port 9010 open
35
- const securityGroup = new ec2 . SecurityGroup ( this , 'AlloraWorkerxSecurityGroup' , {
40
+ const securityGroup = new ec2 . SecurityGroup ( this , ` ${ resourceNamePrefix } SecurityGroup` , {
36
41
vpc,
37
42
allowAllOutbound : true ,
38
43
description : 'Allow inbound TCP 9010' ,
39
44
} ) ;
40
45
securityGroup . addIngressRule ( ec2 . Peer . anyIpv4 ( ) , ec2 . Port . tcp ( 9010 ) , 'Allow inbound TCP 9010' ) ;
41
46
42
47
// EC2 Instance
43
- const instance = new ec2 . Instance ( this , 'AlloraWorkerxInstance' , {
48
+ const instance = new ec2 . Instance ( this , ` ${ resourceNamePrefix } Instance` , {
44
49
vpc,
45
- instanceType : new ec2 . InstanceType ( instanceSizeParam . valueAsString ) ,
50
+ instanceType : new ec2 . InstanceType ( instanceType ) ,
46
51
machineImage : ec2 . MachineImage . genericLinux ( {
47
- 'us-east-1' : 'ami-04b70fa74e45c3917' ,
52
+ [ region ] : amiId ,
48
53
} ) ,
49
54
vpcSubnets : {
50
55
subnetType : ec2 . SubnetType . PUBLIC ,
@@ -59,8 +64,8 @@ export class AlloraStack extends cdk.Stack {
59
64
} ) ;
60
65
61
66
// Elastic IP
62
- const eip = new ec2 . CfnEIP ( this , 'AlloraWorkerxEIP' ) ;
63
- new ec2 . CfnEIPAssociation ( this , 'AlloraWorkerxEIPAssociation' , {
67
+ const eip = new ec2 . CfnEIP ( this , ` ${ resourceNamePrefix } EIP` ) ;
68
+ new ec2 . CfnEIPAssociation ( this , ` ${ resourceNamePrefix } EIPAssociation` , {
64
69
eip : eip . ref ,
65
70
instanceId : instance . instanceId ,
66
71
} ) ;
0 commit comments