# @janiscommerce/accounts-ids-by-service

> Resolve the Janis serviceCode to AWS Account ID mapping from the shared Parameter Store

Latest version **1.0.0** (published 2026-07-03) · ISC license · 0 weekly downloads

## Install

```sh
npm install @janiscommerce/accounts-ids-by-service
pnpm add @janiscommerce/accounts-ids-by-service
yarn add @janiscommerce/accounts-ids-by-service
bun add @janiscommerce/accounts-ids-by-service
```

## Health

**Score 65/100 (B)** — status: active.

Positive: has types; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2026-07-03 |
| First published | 2026-07-03 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=18 |
| Dependencies | 2 |
| Unpacked size | 14.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Janis |
| Maintainers | janiscommerce |

## Links

- npm: https://www.npmjs.com/package/@janiscommerce/accounts-ids-by-service
- Repository: https://github.com/janis-commerce/accounts-ids-by-service
- Homepage: https://github.com/janis-commerce/accounts-ids-by-service.git#readme
- Issues: https://github.com/janis-commerce/accounts-ids-by-service/issues
- npm.io page: https://npm.io/package/@janiscommerce/accounts-ids-by-service

## Dependencies (2)

- [@aws-sdk/client-ram](https://npm.io/package/@aws-sdk/client-ram.md) ^3.859.0
- [@aws-sdk/client-ssm](https://npm.io/package/@aws-sdk/client-ssm.md) ^3.859.0

## Recent versions

- 1.0.0 (latest) — 2026-07-03

## README

# Accounts Ids By Service

![Build Status](https://github.com/janis-commerce/accounts-ids-by-service/workflows/Build%20Status/badge.svg?branch=master)
[![Coverage Status](https://coveralls.io/repos/github/janis-commerce/accounts-ids-by-service/badge.svg?branch=master)](https://coveralls.io/github/janis-commerce/accounts-ids-by-service?branch=master)
[![npm version](https://badge.fury.io/js/%40janiscommerce%2Faccounts-ids-by-service.svg)](https://www.npmjs.com/package/@janiscommerce/accounts-ids-by-service)

Resolve, at runtime, the mapping between a **Janis service code** and its **AWS Account ID**.

The mapping lives in a single SSM Parameter Store parameter named `accountsIdsByService`, shared across accounts via [AWS RAM](https://docs.aws.amazon.com/ram/latest/userguide/what-is.html). This package encapsulates the discovery (RAM) + read (SSM) + in-memory cache logic so consumers such as `@janiscommerce/lambda` (runtime) and `sls-helper-plugin-janis` (deploy-time) can resolve account IDs through a single dependency.

## Installation

```sh
npm install @janiscommerce/accounts-ids-by-service
```

## API

### `AccountsIdsByService.getAccountId(serviceCode)`

`async`. Returns the AWS Account ID (`string`) for the given Janis `serviceCode`.

- Throws an `AccountsIdsByServiceError` with code `NO_SERVICE_ACCOUNT_ID` when the service code is not present in the mapping.
- In a local/dev environment it resolves to `undefined` instead of throwing (see [Usage](#usage)).

```js
'use strict';

const { AccountsIdsByService } = require('@janiscommerce/accounts-ids-by-service');

const accountId = await AccountsIdsByService.getAccountId('catalog');
// '012345678901'
```

### `AccountsIdsByService.getMapping()`

`async`. Returns the full mapping object `{ [serviceCode]: awsAccountId }`. Used, for example, by the deploy-time plugin to build cross-account ARNs.

```js
'use strict';

const { AccountsIdsByService } = require('@janiscommerce/accounts-ids-by-service');

const mapping = await AccountsIdsByService.getMapping();
// { catalog: '012345678901', wms: '109876543210', ... }
```

The mapping is fetched once and cached in memory **permanently** for the lifetime of the Lambda container (no TTL). Both `getAccountId()` and `getMapping()` share the same cache.

### `AccountsIdsByServiceError`

Error class thrown by this package. Exposes `static get codes()`:

| Code | Value | Meaning |
|---|---|---|
| `NO_SERVICE_ACCOUNT_ID` | `1` | The requested service code is not present in the mapping |
| `INVALID_MAPPING` | `2` | The parameter value could not be parsed as JSON |

```js
'use strict';

const { AccountsIdsByService, AccountsIdsByServiceError } = require('@janiscommerce/accounts-ids-by-service');

try {
	await AccountsIdsByService.getAccountId('unknown-service');
} catch(error) {
	if(error.code === AccountsIdsByServiceError.codes.NO_SERVICE_ACCOUNT_ID)
		console.error('Service is not mapped to any AWS account');
}
```

### `accountsIdsPermissions`

An array of [sls-helper](https://www.npmjs.com/package/sls-helper) IAM statement hooks (`['iamStatement', {...}]`) granting the permissions this package needs at runtime: `ram:ListResources` and `ssm:GetParameter`. Spread it into your service `serverless.js`.

## Usage

Grant the runtime permissions by spreading `accountsIdsPermissions` into your service hooks:

```js
'use strict';

const { helper } = require('sls-helper');

const { accountsIdsPermissions } = require('@janiscommerce/accounts-ids-by-service');

module.exports = helper({
	hooks: [

		// ...other service hooks

		...accountsIdsPermissions
	]
});
```

### Local behaviour

When running locally the package never reaches AWS. It is considered a local/dev environment when **any** of these holds:

- `JANIS_ENV === 'local'`
- `JANIS_LOCAL === '1'`
- `NODE_ENV === 'dev'`

In that case:

- `getMapping()` resolves to an empty object `{}`.
- `getAccountId()` resolves to `undefined` and **does not throw** `NO_SERVICE_ACCOUNT_ID`, mirroring the behaviour of `@janiscommerce/lambda` `Invoker.getServiceAccountId()`.

### Resolution algorithm

On the first resolution (cache miss, non-local):

1. **RAM discovery** — `ram:ListResources` with `resourceOwner: 'OTHER-ACCOUNTS'` and `resourceType: 'ssm:Parameter'`, then find the resource whose ARN ends with `:parameter/accountsIdsByService`.
2. **SSM read** — if the shared ARN is found, `ssm:GetParameter` by that ARN and `JSON.parse` its value. This is the common case.
3. **Fallback** — if no share is found (i.e. the parameter lives in the same account, so it is not shared with itself), `ssm:GetParameter` by name (`accountsIdsByService`) in the local account.

The resolved mapping is cached in memory for the lifetime of the container.

---
_Source: https://npm.io/package/@janiscommerce/accounts-ids-by-service · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
