Skip to content

Commit 85042cb

Browse files
committed
adding types: [ node ] to tsconfig, and uploading the node.sh script to an s3 bucket and getting it executed as user data
1 parent abc4db3 commit 85042cb

File tree

4 files changed

+111
-1
lines changed

4 files changed

+111
-1
lines changed

lib/allora/lib/allora-stack.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import * as cdk from 'aws-cdk-lib';
22
import { Construct } from 'constructs';
33
import * as ec2 from 'aws-cdk-lib/aws-ec2';
4-
import * as iam from 'aws-cdk-lib/aws-iam';
4+
import * as s3 from 'aws-cdk-lib/aws-s3';
5+
import * as s3deploy from 'aws-cdk-lib/aws-s3-deployment';
6+
import * as fs from 'fs';
7+
import * as path from 'path';
58
// import * as sqs from 'aws-cdk-lib/aws-sqs';
69

710

@@ -25,6 +28,21 @@ export class AlloraStack extends cdk.Stack {
2528
const instanceType = props?.instanceType || 't2.medium';
2629
const resourceNamePrefix = props?.resourceNamePrefix || 'AlloraWorkerx';
2730

31+
32+
33+
// Create S3 Bucket
34+
const bucket = new s3.Bucket(this, `${resourceNamePrefix}Bucket`, {
35+
removalPolicy: cdk.RemovalPolicy.DESTROY,
36+
autoDeleteObjects: true,
37+
});
38+
39+
// Upload node.sh to S3
40+
new s3deploy.BucketDeployment(this, `${resourceNamePrefix}ScriptDeployment`, {
41+
sources: [s3deploy.Source.asset(path.join(__dirname, 'assets', 'user-data'))],
42+
destinationBucket: bucket,
43+
destinationKeyPrefix: 'user-data', // optional prefix in destination bucket
44+
});
45+
2846
// Create VPC
2947
const vpc = new ec2.Vpc(this, `${resourceNamePrefix}Vpc`, {
3048
maxAzs: props?.vpcMaxAzs || 1,
@@ -44,6 +62,18 @@ export class AlloraStack extends cdk.Stack {
4462
});
4563
securityGroup.addIngressRule(ec2.Peer.anyIpv4(), ec2.Port.tcp(9010), 'Allow inbound TCP 9010');
4664

65+
// Read user data script and inject variables
66+
const userData = fs.readFileSync(path.join(__dirname, 'assets', 'user-data', 'node.sh')).toString();
67+
const modifiedUserData = cdk.Fn.sub(userData, {
68+
_AWS_REGION_: region,
69+
_ASSETS_S3_PATH_: `s3://${bucket.bucketName}/user-data/node.sh`,
70+
// Add other variables as needed
71+
});
72+
73+
// Create UserData for EC2 instance
74+
const ec2UserData = ec2.UserData.forLinux();
75+
ec2UserData.addCommands(modifiedUserData);
76+
4777
// EC2 Instance
4878
const instance = new ec2.Instance(this, `${resourceNamePrefix}Instance`, {
4979
vpc,
@@ -61,6 +91,7 @@ export class AlloraStack extends cdk.Stack {
6191
volumeType: ec2.EbsDeviceVolumeType.GP3,
6292
}),
6393
}],
94+
userData: ec2UserData
6495
});
6596

6697
// Elastic IP

lib/allora/lib/assets/user-data/node.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/bin/bash
22

3+
echo "AWS_REGION=${_AWS_REGION_}" >> /etc/environment
4+
echo "ASSETS_S3_PATH=${_ASSETS_S3_PATH_}" >> /etc/environment
5+
36
#############################
47
# Prerequisites
58
# Pip

lib/allora/test/allora.test.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,79 @@ test('Stack has correct resources', () => {
5656
template.hasResourceProperties('AWS::EC2::EIPAssociation', {
5757
InstanceId: Match.anyValue(),
5858
});
59+
60+
// Check for S3 Bucket
61+
template.resourceCountIs('AWS::S3::Bucket', 1);
62+
63+
// Check for Bucket Deployment
64+
template.hasResourceProperties('Custom::CDKBucketDeployment', {
65+
DestinationBucketName: {
66+
'Ref': Match.anyValue(),
67+
},
68+
DestinationBucketKeyPrefix: 'user-data',
69+
Prune: true,
70+
ServiceToken: {
71+
'Fn::GetAtt': [
72+
Match.anyValue(),
73+
'Arn'
74+
]
75+
},
76+
SourceBucketNames: [
77+
Match.anyValue(),
78+
],
79+
SourceObjectKeys: [
80+
Match.stringLikeRegexp('.*\\.zip')
81+
]
82+
});
83+
84+
// Check for S3 bucket policy to allow read access to the EC2 instance
85+
template.hasResourceProperties('AWS::S3::BucketPolicy', {
86+
Bucket: {
87+
'Ref': Match.anyValue(),
88+
},
89+
PolicyDocument: {
90+
Statement: Match.arrayWith([
91+
Match.objectLike({
92+
Action: [
93+
"s3:PutBucketPolicy",
94+
"s3:GetBucket*",
95+
"s3:List*",
96+
"s3:DeleteObject*"
97+
],
98+
Effect: 'Allow',
99+
Resource: [
100+
{
101+
'Fn::GetAtt': [
102+
Match.anyValue(),
103+
'Arn',
104+
],
105+
},
106+
{
107+
'Fn::Join': [
108+
'',
109+
[
110+
{
111+
'Fn::GetAtt': [
112+
Match.anyValue(),
113+
'Arn'
114+
]
115+
},
116+
'/*'
117+
]
118+
],
119+
}
120+
],
121+
Principal: {
122+
AWS: {
123+
'Fn::GetAtt': [
124+
Match.anyValue(),
125+
'Arn',
126+
],
127+
},
128+
},
129+
}),
130+
]),
131+
Version: '2012-10-17'
132+
},
133+
});
59134
});

lib/allora/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"compilerOptions": {
3+
"types": ["node"],
34
"target": "ES2020",
45
"module": "commonjs",
56
"lib": [

0 commit comments

Comments
 (0)