npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

detect-ec2

v1.1.0

Published

Detect if running on AWS EC2 via IMDS (supports IMDSv1 and IMDSv2)

Readme

detect-ec2

Detect if your code is running on AWS EC2 via Instance Metadata Service (IMDS).

Supports both IMDSv1 and IMDSv2.

Usage

CLI (via npx)

npx detect-ec2

Options:

| Flag | Description | |------|-------------| | -j, --json | Output result as JSON | | -v, --verbose | Include instance metadata | | -t, --timeout <ms> | Set timeout (default: 1000ms) | | -e, --set-env | Output shell export commands | | -p, --prefix <str> | Set env var prefix (default: EC2_) | | -h, --help | Show help |

Exit codes:

  • 0 - Running on EC2
  • 1 - Not running on EC2

Examples:

# Simple check
npx detect-ec2

# JSON output for scripting
npx detect-ec2 --json

# With instance details
npx detect-ec2 --verbose

# Custom timeout
npx detect-ec2 --timeout 2000

# Set environment variables in shell
eval $(npx detect-ec2 --set-env)
echo $EC2_IS_EC2

# Custom prefix
eval $(npx detect-ec2 --set-env --prefix AWS_)
echo $AWS_IS_EC2

Programmatic Usage

npm install detect-ec2
const { detectEC2 } = require('detect-ec2');

async function main() {
  const result = await detectEC2();

  if (result.isEC2) {
    console.log(`Running on EC2 (${result.imdsVersion})`);
  } else {
    console.log('Not running on EC2');
  }
}

// With options
const result = await detectEC2({
  timeout: 2000,  // Custom timeout in ms
  verbose: true   // Include metadata
});

// result.metadata contains:
// - instance-id
// - instance-type
// - ami-id
// - local-ipv4
// - public-ipv4

Auto-Set Environment Variables

Use setEnv() to automatically populate process.env with EC2 detection results:

const { setEnv } = require('detect-ec2');

// Basic usage - sets EC2_IS_EC2, EC2_INSTANCE_ID, etc.
await setEnv();
console.log(process.env.EC2_IS_EC2);        // "true" or "false"
console.log(process.env.EC2_INSTANCE_ID);   // "i-1234567890abcdef0"

// Custom prefix
await setEnv({ prefix: 'AWS_' });
console.log(process.env.AWS_IS_EC2);

// Custom env var names
await setEnv({
  envNames: {
    isEC2: 'MY_APP_ON_EC2',
    instanceId: 'MY_APP_INSTANCE',
  }
});

// Without metadata (faster)
await setEnv({ includeMetadata: false });

Environment variables set:

| Variable | Description | |----------|-------------| | EC2_IS_EC2 | "true" or "false" | | EC2_IMDS_VERSION | "v1" or "v2" (only if EC2) | | EC2_INSTANCE_ID | Instance ID (only if EC2) | | EC2_INSTANCE_TYPE | Instance type (only if EC2) | | EC2_AMI_ID | AMI ID (only if EC2) | | EC2_LOCAL_IPV4 | Private IP (only if EC2) | | EC2_PUBLIC_IPV4 | Public IP (only if EC2) |

Use Cases

CI/CD Pipelines

GitHub Actions:

- name: Check if running on EC2
  run: |
    if npx detect-ec2; then
      echo "Running on EC2 self-hosted runner"
    else
      echo "Running on GitHub-hosted runner"
    fi

GitLab CI:

detect_environment:
  script:
    - eval $(npx detect-ec2 --set-env)
    - echo "EC2: $EC2_IS_EC2"

Application Startup

const { setEnv } = require('detect-ec2');

async function bootstrap() {
  await setEnv();

  if (process.env.EC2_IS_EC2 === 'true') {
    // Use EC2-specific configuration
    console.log(`Running on ${process.env.EC2_INSTANCE_TYPE}`);
  } else {
    // Use local/fallback configuration
  }
}

Conditional Configuration

const { detectEC2 } = require('detect-ec2');

async function getConfig() {
  const { isEC2, metadata } = await detectEC2({ verbose: true });

  return {
    logDriver: isEC2 ? 'cloudwatch' : 'console',
    metricsEndpoint: isEC2 ? 'cloudwatch' : 'local',
    instanceId: metadata?.['instance-id'] || 'local',
  };
}

Shell Scripts

#!/bin/bash
eval $(npx detect-ec2 --set-env)

if [ "$EC2_IS_EC2" = "true" ]; then
  echo "Deploying to EC2 instance: $EC2_INSTANCE_ID"
  # EC2-specific deployment
else
  echo "Local environment detected"
  # Local deployment
fi

Docker/ECS Detection

const { detectEC2 } = require('detect-ec2');
const fs = require('fs');

async function getRuntime() {
  const { isEC2 } = await detectEC2();
  const isDocker = fs.existsSync('/.dockerenv');

  if (isEC2 && isDocker) return 'ecs';  // or EKS
  if (isEC2) return 'ec2';
  if (isDocker) return 'docker-local';
  return 'local';
}

How It Works

The package queries the EC2 Instance Metadata Service at http://169.254.169.254. This IP address is only accessible from within EC2 instances.

  1. First tries IMDSv2 (token-based, more secure)
  2. Falls back to IMDSv1 if v2 is unavailable
  3. Uses a 1-second timeout to quickly fail on non-EC2 environments

API

detectEC2(options?)

Options:

  • timeout (number): Request timeout in milliseconds. Default: 1000
  • verbose (boolean): Fetch additional metadata. Default: false

Returns: Promise<DetectionResult>

interface DetectionResult {
  isEC2: boolean;
  imdsVersion?: 'v1' | 'v2';
  metadata?: {
    'instance-id'?: string;
    'instance-type'?: string;
    'ami-id'?: string;
    'local-ipv4'?: string;
    'public-ipv4'?: string;
  };
}

setEnv(options?)

Detects EC2 and sets environment variables in process.env.

Options:

  • timeout (number): Request timeout in milliseconds. Default: 1000
  • prefix (string): Prefix for env var names. Default: 'EC2_'
  • envNames (object): Override specific env var names
  • includeMetadata (boolean): Include metadata fields. Default: true

Returns: Promise<DetectionResult> (same as detectEC2)

License

MIT