New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

aws-metadata-utils

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

aws-metadata-utils

Small utility library that helps you extract helpful metadata from your AWS event and context objects

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
137
increased by21.24%
Maintainers
1
Weekly downloads
 
Created
Source

aws-metadata-utils

aws-metadata-utils is a small utility library that helps you extract helpful metadata from your AWS event and context objects.

Build Status

Quality Gate Status

codecov

Maintainability


This library is what drives the majority of contextual metadata generated in aws-metadata-utils, MikroLog, MikroTrace, and MikroMetric outputs.

Usage

Here's an example of running aws-metadata-utils in AWS Lambda and just returning back the metadata.

import { getMetadata } from 'aws-metadata-utils';

export async function handler(event: any, context: any) {
  // Pass in your AWS event and context objects, such as from API Gateway
  const metadata = getMetadata(event, context);

  return {
    statusCode: 200,
    body: JSON.stringify(metadata)
  };
}

aws-metadata-utils will attempt to pick out various interesting details for you from the event and context objects, such as the function name, region, account ID, correlation ID (AWS request ID) and the stage that is used.

The result of the above example could result in an object with the following shape:

{
  "accountId": "123412341234",
  "correlationId": "6c933bd2-9535-45a8-b09c-84d00b4f50cc",
  "functionMemorySize": "1024",
  "functionName": "somestack-FunctionName",
  "functionVersion": "$LATEST",
  "region": "eu-north-1",
  "resource": "/functionName",
  "runtime": "AWS_Lambda_node16.x",
  "stage": "shared",
  "timestampRequest": "1657389598171",
  "user": "some user",
  "viewerCountry": "SE"
}

Usage: Getting the correlation ID

You can also use aws-metadata-utils to only get a correlation ID.

import { getCorrelationId } from 'aws-metadata-utils';

export async function handler(event: any, context: any) {
  // Pass in your AWS event and context objects, such as from API Gateway
  const correlationId = getCorrelationId(event, context);

  return {
    statusCode: 200,
    body: JSON.stringify(correlationId)
  };
}

To get the correlation ID it first checks if an appropriate ID is passed:

  1. Via an event — EventBridge: event.detail.metadata.correlationId
  2. Via a header — API Gateway: event.headers.x-correlation-id
  3. Set new one from AWS request ID (context.awsRequestId)

If none of the above match, set it as empty.

Dynamic metadata

The dynamic metadata fields are picked up automatically if you pass them in during instantiation. Most of those metadata fields will relate to unique value types available in AWS, primarily Lambda.

If these values are not available, they will be dropped at the time of log output. In effect, this means you won't have to deal with them (being empty or otherwise) if you use aws-metadata-utils in another type of context.

FieldTypeDescription
accountIdstringThe AWS account ID that the system is running in.
correlationIdstringCorrelation ID (AWS request ID) for this function call.
functionMemorySizestringMemory size of the current function.
functionNamestringThe name of the function.
functionVersionstringThe version of the function.
regionstringThe region of the responding function/system.
resourcestringThe resource (channel, URL path...) that is responding.
runtimestringWhat runtime is used?
stagestringWhat AWS stage are we in?
timestampRequeststringRequest time in Unix epoch of the incoming request.
userstringThe user in this context.
viewerCountrystringWhich country did AWS CloudFront infer the user to be in?

Keywords

FAQs

Package last updated on 01 Apr 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc