🔗 Official AWS Repository: This example has been contributed to and is available in the official AWS CDK Examples repository:
https://github.com/aws-samples/aws-cdk-examples/tree/ea5de820d2b080b13c372ff6c699abbda1976225/typescript/batch-ecr-openmpPull Request: aws-samples/aws-cdk-examples#1188
This repository serves as a personal record of my contributions for portfolio display.
This is a stable example. It should successfully build out of the box
This examples is built on Construct Libraries marked "Stable" and does not have any infrastructure prerequisites to build.
This example demonstrates how to use AWS Batch with a containerized C++ OpenMP application stored in Amazon ECR, with optional Lambda function for job submission. The OpenMP application performs parallel computing benchmarks (arithmetic operations, mathematical functions, matrix multiplication) to demonstrate CPU-intensive workloads and compare sequential vs parallel execution performance.
Before deploying, ensure you have:
- AWS CLI configured with sufficient permissions for infrastructure deployment
- AdministratorAccess recommended, or custom policy with EC2, VPC, Batch, ECR, Lambda, IAM, CloudFormation, and CloudWatch Logs permissions
- Docker installed and running
- Node.js and npm installed
jq
command-line JSON processor (for job submission script)
For a complete end-to-end deployment and testing:
# 1. Test locally first (optional but recommended)
./scripts/test-local.sh
# 2. Deploy everything to AWS
AWS_PROFILE=your-profile ./scripts/build-and-deploy.sh
# 3. Submit a benchmark job
./scripts/submit-job.sh --benchmark-type simple
To build this app manually, you need to be in this example's root folder. Then run the following:
npm install -g aws-cdk
npm install
npm run build
This will install the necessary CDK, then this example's dependencies, and then build your TypeScript files and your CloudFormation template.
Use the provided script for a complete deployment:
AWS_PROFILE=your-profile ./scripts/build-and-deploy.sh
This script will:
- Build the CDK stack
- Deploy the infrastructure to AWS
- Build and push the Docker image to ECR
- Save deployment information for job submission
For manual control over each step:
# Deploy CDK stack
npx cdk deploy --profile your-profile
# Get ECR login and build/push Docker image
aws ecr get-login-password --region your-region | docker login --username AWS --password-stdin your-account.dkr.ecr.your-region.amazonaws.com
docker build -f docker/Dockerfile -t openmp-benchmark:latest .
docker tag openmp-benchmark:latest your-account.dkr.ecr.your-region.amazonaws.com/openmp-benchmark:latest
docker push your-account.dkr.ecr.your-region.amazonaws.com/openmp-benchmark:latest
After deployment, you will see the Stack outputs, which include the ECR repository URI and Batch job queue name.
After successful deployment, you can submit OpenMP benchmark jobs using the provided script:
# Submit a simple benchmark (default)
./scripts/submit-job.sh
# Submit specific benchmark types
./scripts/submit-job.sh --benchmark-type math
./scripts/submit-job.sh --benchmark-type matrix
./scripts/submit-job.sh --benchmark-type heavy
./scripts/submit-job.sh --benchmark-type all
# Custom parameters
./scripts/submit-job.sh --benchmark-type matrix --instance c6i.2xlarge --threads 8
# Submit via Lambda function
./scripts/submit-job.sh --method lambda --benchmark-type simple
# Preview commands without executing
./scripts/submit-job.sh --dry-run --benchmark-type all
- simple - Basic arithmetic operations (~2 seconds)
- math - Mathematical functions: sin, cos, sqrt, pow (~5 seconds)
- matrix - Matrix multiplication (memory intensive, ~3 seconds)
- heavy - Complex arithmetic expressions (~3 seconds)
- all - Run all benchmarks sequentially (~15 seconds total)
After submitting a job, you can monitor it through:
-
AWS Console:
- Batch: https://console.aws.amazon.com/batch/
- CloudWatch Logs: https://console.aws.amazon.com/cloudwatch/
-
AWS CLI:
# Check job status aws batch describe-jobs --profile your-profile --jobs <JOB_ID> # View logs aws logs describe-log-streams --profile your-profile --log-group-name /aws/batch/openmp-benchmark
Before deploying to AWS, you can test the OpenMP application locally:
./scripts/test-local.sh
This script will:
- Build the OpenMP application locally
- Run C++ unit tests (8 test cases)
- Execute integration tests with various parameters
- Build and test the Docker container
- Run CDK unit tests
- ✓ OpenMP application builds successfully
- ✓ C++ unit tests pass (sequential vs parallel validation)
- ✓ Integration tests with different parameters
- ✓ Docker container builds and runs correctly
- ✓ CDK stack synthesizes without errors
The whole component contains:
- An ECR repository for storing the Docker image
- AWS Batch compute environment with managed EC2 instances
- AWS Batch job definition and job queue
- Lambda function for job submission (optional)
- VPC with public and private subnets
- CloudWatch log group for job logs
The cdk.json
file in the root of this repository includes
instructions for the CDK toolkit on how to execute this program.
After building your TypeScript code, you will be able to run the CDK toolkit commands as usual:
$ cdk ls
<list all stacks in this program>
$ cdk synth
<generates and outputs cloudformation template>
$ cdk deploy
<deploys stack to your account>
$ cdk diff
<shows diff against deployed stack>
To completely remove all AWS resources and avoid ongoing charges:
Before destroying the stack, ensure no jobs are running:
- Navigate to AWS Batch Console
- Click on "Jobs" in the left sidebar
- Select your job queue (e.g., "AwsBatchOpenmpBenchmarkStack-OpenMPJobQueue...")
- Filter by status: RUNNING, RUNNABLE, or PENDING
- Select any active jobs and click "Terminate job"
- Provide a reason (e.g., "Stack cleanup")
- Verify all jobs show as SUCCEEDED, FAILED, or CANCELLED
# List running jobs
aws batch list-jobs --profile your-profile \
--job-queue <queue-name> \
--job-status RUNNING
# Terminate each job
aws batch terminate-job --profile your-profile \
--job-id <job-id> \
--reason "Stack cleanup"
npx cdk destroy --profile your-profile
Type 'y' when prompted to confirm deletion. This will remove:
- ✓ All infrastructure resources (VPC, subnets, NAT gateway)
- ✓ ECR repository and all Docker images inside it
- ✓ Batch compute environment, job queue, and job definitions
- ✓ Lambda function and IAM roles
- ✓ CloudWatch log groups and all logs
- ✓ Security groups and all networking components
# Remove deployment info file
rm -f deployment-info.json
# Remove all local Docker images (including tagged ECR images)
docker rmi $(docker images | grep openmp-benchmark | awk '{print $3}')
# Clean up Docker build cache (frees significant disk space)
docker builder prune
# Clean up any dangling Docker images
docker image prune
After cleanup, verify in the AWS Console that:
- The CloudFormation stack is deleted
- No ECR repositories remain for this project
- No VPC resources are left behind
- No unexpected charges appear in your AWS billing
Note: The stack is specifically configured for complete resource deletion. All resources have appropriate removal policies to ensure clean deletion without leaving orphaned resources.