@@ -7,7 +7,12 @@ import type { GuAsgCapacity } from '@guardian/cdk/lib/types';
7
7
import type { App , CfnElement } from 'aws-cdk-lib' ;
8
8
import { Duration } from 'aws-cdk-lib' ;
9
9
import type { InstanceSize } from 'aws-cdk-lib/aws-ec2' ;
10
- import { InstanceClass , InstanceType , Peer } from 'aws-cdk-lib/aws-ec2' ;
10
+ import {
11
+ InstanceClass ,
12
+ InstanceType ,
13
+ Peer ,
14
+ UserData ,
15
+ } from 'aws-cdk-lib/aws-ec2' ;
11
16
import {
12
17
HostedZone ,
13
18
RecordSet ,
@@ -46,6 +51,35 @@ export class MobileAppsRendering extends GuStack {
46
51
'/account/services/artifact.bucket' ,
47
52
) ;
48
53
54
+ const userData = UserData . forLinux ( ) ;
55
+ userData . addCommands (
56
+ `#!/bin/bash -ev` ,
57
+ `groupadd mapi` ,
58
+ `useradd -r -m -s /usr/bin/nologin -g mapi ${ appName } ` ,
59
+
60
+ `export App=${ appName } ` ,
61
+ `export Stack=${ this . stack } ` ,
62
+ `export Stage=${ this . stage } ` ,
63
+ `export NODE_ENV=production` ,
64
+
65
+ `aws s3 cp s3://${ artifactBucketName } /${ this . stack } /${ this . stage } /${ appName } /${ appName } .zip /tmp` ,
66
+ `mkdir -p /opt/${ appName } ` ,
67
+ `unzip /tmp/${ appName } .zip -d /opt/${ appName } ` ,
68
+ `chown -R ${ appName } :mapi /opt/${ appName } ` ,
69
+
70
+ `mkdir -p /usr/share/${ appName } /logs` ,
71
+ `chown -R ${ appName } :mapi /usr/share/${ appName } ` ,
72
+ `ln -s /usr/share/${ appName } /logs /var/log/${ appName } ` ,
73
+ `chown -R ${ appName } :mapi /var/log/${ appName } ` ,
74
+
75
+ `export PM2_HOME="/usr/share/${ appName } "` ,
76
+ `export ASSETS_MANIFEST="/opt/${ appName } /manifest.json"` ,
77
+
78
+ `/usr/local/node/pm2 start --name ${ appName } --uid ${ appName } --gid mapi /opt/${ appName } /server.js` ,
79
+ `/opt/aws-kinesis-agent/configure-aws-kinesis-agent ${ this . region } mobile-log-aggregation-${ this . stage } '/var/log/${ appName } /*'` ,
80
+ `/usr/local/node/pm2 logrotate -u ${ appName } ` ,
81
+ ) ;
82
+
49
83
const appsRenderingApp = new GuEc2App ( this , {
50
84
applicationPort : 3040 ,
51
85
app : 'mobile-apps-rendering' ,
@@ -72,31 +106,7 @@ export class MobileAppsRendering extends GuStack {
72
106
} ) ,
73
107
] ,
74
108
} ,
75
- userData : `#!/bin/bash -ev
76
- groupadd mapi
77
- useradd -r -m -s /usr/bin/nologin -g mapi ${ appName }
78
-
79
- export App=${ appName }
80
- export Stack=${ this . stack }
81
- export Stage=${ this . stage }
82
- export NODE_ENV=production
83
-
84
- aws s3 cp s3://${ artifactBucketName } /${ this . stack } /${ this . stage } /${ appName } /${ appName } .zip /tmp
85
- mkdir -p /opt/${ appName }
86
- unzip /tmp/${ appName } .zip -d /opt/${ appName }
87
- chown -R ${ appName } :mapi /opt/${ appName }
88
-
89
- mkdir -p /usr/share/${ appName } /logs
90
- chown -R ${ appName } :mapi /usr/share/${ appName }
91
- ln -s /usr/share/${ appName } /logs /var/log/${ appName }
92
- chown -R ${ appName } :mapi /var/log/${ appName }
93
-
94
- export PM2_HOME="/usr/share/${ appName } "
95
- export ASSETS_MANIFEST="/opt/${ appName } /manifest.json"
96
-
97
- /usr/local/node/pm2 start --name ${ appName } --uid ${ appName } --gid mapi /opt/${ appName } /server.js
98
- /opt/aws-kinesis-agent/configure-aws-kinesis-agent ${ this . region } mobile-log-aggregation-${ this . stage } '/var/log/${ appName } /*'
99
- /usr/local/node/pm2 logrotate -u ${ appName } ` ,
109
+ userData,
100
110
scaling : props . asgCapacity ,
101
111
} ) ;
102
112
0 commit comments