generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(samples/cloudwatch-applicationsignals-mcp): add docker nodejs express app #1721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
86c47ac
add docker nodejs express app
yiyuan-he d740aaa
apply pretty json format
yiyuan-he d4c6c62
make generate-traffic executable for precommit check
yiyuan-he 31951ac
Merge branch 'main' into pr/nodejs-express
yiyuan-he db22a79
remove hardcoded region in s3 client
yiyuan-he fd1e369
Merge branch 'main' into pr/nodejs-express
yiyuan-he ef81402
Merge branch 'main' into pr/nodejs-express
yiyuan-he fe188cd
reapply precommit checks after merge conflict resolution
yiyuan-he File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...applicationsignals-mcp/get-enablement-guide-samples/docker-apps/nodejs/express/Dockerfile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # Use the official Node.js image as the base | ||
| FROM node:20-alpine | ||
|
|
||
| # Install curl | ||
| RUN apk add --no-cache curl | ||
|
|
||
| # Set working directory | ||
| WORKDIR /app | ||
|
|
||
| # Copy package files first for better layer caching | ||
| COPY package*.json ./ | ||
|
|
||
| # Install dependencies | ||
| RUN npm ci --only=production | ||
|
|
||
| # Copy app to image | ||
| COPY . ./ | ||
|
|
||
| # Set environment variables | ||
| ENV NODE_ENV=production | ||
| ENV HOST=0.0.0.0 | ||
| ENV PORT=8080 | ||
|
|
||
| # Change ownership to the built-in node user | ||
| RUN chown -R node:node /app | ||
|
|
||
| USER node | ||
|
|
||
| # Expose the port that the app will run on | ||
| EXPOSE 8080 | ||
|
|
||
| HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \ | ||
| CMD curl -f http://localhost:8080/health || exit 1 | ||
|
|
||
| # Command to start the application | ||
| CMD ["npm", "run", "start"] |
62 changes: 62 additions & 0 deletions
62
...tch-applicationsignals-mcp/get-enablement-guide-samples/docker-apps/nodejs/express/app.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| const express = require('express'); | ||
| const { S3Client, ListBucketsCommand } = require('@aws-sdk/client-s3'); | ||
| const logger = require('pino')(); | ||
|
|
||
| const HOST = process.env.HOST || '0.0.0.0'; | ||
| const PORT = parseInt(process.env.PORT || '8080', 10); | ||
|
|
||
| const app = express(); | ||
|
||
|
|
||
| const s3Client = new S3Client({ region: process.env.AWS_REGION || 'us-east-1' }); | ||
|
|
||
| app.get('/', (req, res) => { | ||
| healthCheck(res); | ||
| }); | ||
|
|
||
| app.get('/health', (req, res) => { | ||
| healthCheck(res); | ||
| }); | ||
|
|
||
| function healthCheck(res) { | ||
| logger.info('Health check endpoint called'); | ||
| res.type('application/json').send(JSON.stringify({status: 'healthy'}) + '\n'); | ||
| } | ||
|
|
||
| app.get('/api/buckets', async (req, res) => { | ||
| try { | ||
| const data = await s3Client.send(new ListBucketsCommand({})); | ||
| const buckets = data.Buckets.map(bucket => bucket.Name); | ||
| logger.info(`Successfully listed ${buckets.length} S3 buckets`); | ||
| res.type('application/json').send(JSON.stringify({ | ||
| bucket_count: buckets.length, | ||
| buckets: buckets | ||
| }) + '\n'); | ||
| } catch (e) { | ||
| if (e instanceof Error) { | ||
| logger.error(`Exception thrown when Listing Buckets: ${e.message}`); | ||
| } | ||
| res.status(500).type('application/json').send(JSON.stringify({ | ||
| error: 'Failed to retrieve S3 buckets' | ||
| }) + '\n'); | ||
| } | ||
| }); | ||
|
|
||
| app.listen(PORT, HOST, () => { | ||
| logger.info(`Listening for requests on ${HOST}:${PORT}`); | ||
| }); | ||
39 changes: 39 additions & 0 deletions
39
...onsignals-mcp/get-enablement-guide-samples/docker-apps/nodejs/express/generate-traffic.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| #!/bin/bash | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # Traffic generator script for Express application | ||
| PORT=${PORT:-8080} | ||
| BASE_URL="http://localhost:${PORT}" | ||
|
|
||
| echo "Starting continuous traffic generation to ${BASE_URL}" | ||
|
|
||
| while true; do | ||
| echo "[$(date '+%H:%M:%S')] Generating traffic..." | ||
|
|
||
| # Health check | ||
| curl -sf "${BASE_URL}/health" > /dev/null | ||
| if [ $? -ne 0 ]; then | ||
| echo "[$(date '+%H:%M:%S')] ERROR: Health check failed!" | ||
| fi | ||
|
|
||
| # API call (S3 buckets) | ||
| curl -sf "${BASE_URL}/api/buckets" > /dev/null | ||
| if [ $? -ne 0 ]; then | ||
| echo "[$(date '+%H:%M:%S')] ERROR: API call to /api/buckets failed!" | ||
| fi | ||
|
|
||
| # Sleep between requests | ||
| sleep 2 | ||
| done |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.