Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ docker buildx build --platform linux/amd64,linux/arm64 \
.
```

| Language-Framework | App Directory | ECR Repo |
|--------------------|--------------------------|--------------|
| python-flask | docker-apps/python/flask | python-flask |
| Language-Framework | App Directory | ECR Repo |
|--------------------|----------------------------|----------------|
| python-flask | docker-apps/python/flask | python-flask |
| nodejs-express | docker-apps/nodejs/express | nodejs-express |

##### Deploy & Cleanup Containerized Infrastructure

Expand All @@ -67,6 +68,7 @@ cdk deploy <stack-name>
cdk destroy <stack-name>
```

| Language-Framework | Stack Name |
|--------------------|---------------------|
| python-flask | PythonFlaskCdkStack |
| Language-Framework | Stack Name |
|--------------------|---------------------- |
| python-flask | PythonFlaskCdkStack |
| nodejs-express | NodejsExpressCdkStack |
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"]
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();

Check notice

Code scanning / Semgrep OSS

Semgrep Finding: javascript.express.security.audit.express-check-csurf-middleware-usage.express-check-csurf-middleware-usage Note

A CSRF middleware was not detected in your express application. Ensure you are either using one such as csurf or csrf (see rule references) and/or you are properly doing CSRF validation in your routes with a token or cookies.

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}`);
});
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
Loading
Loading