Skip to content

Commit a668933

Browse files
committed
add docker nodejs express app
1 parent 3126d9e commit a668933

File tree

7 files changed

+4466
-6
lines changed

7 files changed

+4466
-6
lines changed

samples/cloudwatch-applicationsignals-mcp/get-enablement-guide-samples/README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ docker buildx build --platform linux/amd64,linux/arm64 \
4848
.
4949
```
5050

51-
| Language-Framework | App Directory | ECR Repo |
52-
|--------------------|--------------------------|--------------|
53-
| python-flask | docker-apps/python/flask | python-flask |
51+
| Language-Framework | App Directory | ECR Repo |
52+
|--------------------|----------------------------|----------------|
53+
| python-flask | docker-apps/python/flask | python-flask |
54+
| nodejs-express | docker-apps/nodejs/express | nodejs-express |
5455

5556
##### Deploy & Cleanup Containerized Infrastructure
5657

@@ -67,6 +68,7 @@ cdk deploy <stack-name>
6768
cdk destroy <stack-name>
6869
```
6970

70-
| Language-Framework | Stack Name |
71-
|--------------------|---------------------|
72-
| python-flask | PythonFlaskCdkStack |
71+
| Language-Framework | Stack Name |
72+
|--------------------|---------------------- |
73+
| python-flask | PythonFlaskCdkStack |
74+
| nodejs-express | NodejsExpressCdkStack |
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Use the official Node.js image as the base
16+
FROM node:20-alpine
17+
18+
# Install curl
19+
RUN apk add --no-cache curl
20+
21+
# Set working directory
22+
WORKDIR /app
23+
24+
# Copy package files first for better layer caching
25+
COPY package*.json ./
26+
27+
# Install dependencies
28+
RUN npm ci --only=production
29+
30+
# Copy app to image
31+
COPY . ./
32+
33+
# Set environment variables
34+
ENV NODE_ENV=production
35+
ENV HOST=0.0.0.0
36+
ENV PORT=8080
37+
38+
# Change ownership to the built-in node user
39+
RUN chown -R node:node /app
40+
41+
USER node
42+
43+
# Expose the port that the app will run on
44+
EXPOSE 8080
45+
46+
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
47+
CMD curl -f http://localhost:8080/health || exit 1
48+
49+
# Command to start the application
50+
CMD ["npm", "run", "start"]
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const express = require('express');;
18+
const { S3Client, ListBucketsCommand } = require('@aws-sdk/client-s3');
19+
const logger = require('pino')()
20+
21+
const HOST = process.env.HOST || '0.0.0.0';
22+
const PORT = parseInt(process.env.PORT || '8080', 10);
23+
24+
const app = express();
25+
26+
const s3Client = new S3Client({ region: 'us-east-1' });
27+
28+
app.get('/', (req, res) => {
29+
healthCheck(res);
30+
});
31+
32+
app.get('/health', (req, res) => {
33+
healthCheck(res);
34+
});
35+
36+
function healthCheck(res) {
37+
logger.info('Health check endpoint called');
38+
res.type('application/json').send(JSON.stringify({status: 'healthy'}) + '\n');
39+
}
40+
41+
app.get('/api/buckets', async (req, res) => {
42+
try {
43+
const data = await s3Client.send(new ListBucketsCommand({}));
44+
const buckets = data.Buckets.map(bucket => bucket.Name);
45+
logger.info(`Successfully listed ${buckets.length} S3 buckets`);
46+
res.type('application/json').send(JSON.stringify({
47+
bucket_count: buckets.length,
48+
buckets: buckets
49+
}) + '\n');
50+
} catch (e) {
51+
if (e instanceof Error) {
52+
logger.error(`Exception thrown when Listing Buckets: ${e.message}`);
53+
}
54+
res.status(500).type('application/json').send(JSON.stringify({
55+
error: 'Failed to retrieve S3 buckets'
56+
}) + '\n');
57+
}
58+
});
59+
60+
app.listen(PORT, HOST, () => {
61+
logger.info(`Listening for requests on ${HOST}:${PORT}`);
62+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/bash
2+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
# Traffic generator script for Express application
17+
PORT=${PORT:-8080}
18+
BASE_URL="http://localhost:${PORT}"
19+
20+
echo "Starting continuous traffic generation to ${BASE_URL}"
21+
22+
while true; do
23+
echo "[$(date '+%H:%M:%S')] Generating traffic..."
24+
25+
# Health check
26+
curl -sf "${BASE_URL}/health" > /dev/null
27+
if [ $? -ne 0 ]; then
28+
echo "[$(date '+%H:%M:%S')] ERROR: Health check failed!"
29+
fi
30+
31+
# API call (S3 buckets)
32+
curl -sf "${BASE_URL}/api/buckets" > /dev/null
33+
if [ $? -ne 0 ]; then
34+
echo "[$(date '+%H:%M:%S')] ERROR: API call to /api/buckets failed!"
35+
fi
36+
37+
# Sleep between requests
38+
sleep 2
39+
done

0 commit comments

Comments
 (0)