The @nx/s3-cache plugin enables you to self-host your remote cache on an Amazon S3 bucket.

Free managed remote cache with Nx Cloud
Self-hosted caching is now free

Self-hosted caching is now free for everyone to use.

Set Up @nx/s3-cache

1. Install the Package

Run the following command:

nx add @nx/s3-cache

This will add the @nx/s3-cache NPM package and automatically configure it for your workspace. As part of this process you'll be guided to generate a new activation key. This is a fully automated process to register your plugin.

The key will be saved in your repository (.nx/key/key.ini) and should be committed so that every developer has access to it. If your repository is public (or in CI) you can also use an environment variable:

.env
1NX_KEY=YOUR_ACTIVATION_KEY 2

If you didn't get an activation key or skipped that step, you can easily generate one at any time by running nx register in your terminal.

Why require an activation key? It simply helps us know and support our users. If you prefer not to provide this information, you can also build your own cache server. Learn more.

2. Authenticate with AWS

There are four different ways to authenticate with AWS. They will be attempted in this order:

  1. Environment variables
  2. INI config files
  3. Single sign-on
  4. nx.json settings

Environment Variables

AWS provides environment variables that can be used to authenticate:

Environment VariableDescription
AWS_ACCESS_KEY_IDThe access key for your AWS account.
AWS_SECRET_ACCESS_KEYThe secret key for your AWS account.
AWS_SESSION_TOKENThe session key for your AWS account. This is only needed when you are using temporary credentials.
AWS_CREDENTIAL_EXPIRATIONThe expiration time of the credentials contained in the environment variables described above. This value must be in a format compatible with the ISO-8601 standard and is only needed when you are using temporary credentials.

Both the AWS_ACCESS_KEY_ID and the AWS_SECRET_ACCESS_KEY environment variables are required to use the environment variable authentication method.

Here's an example of using OIDC in GitHub Actions to set the environment variables in CI:

.github/workflows/ci.yml
1name: CI 2... 3permissions: 4 id-token: write 5 ... 6 7jobs: 8 main: 9 env: 10 NX_KEY: ${{ secrets.NX_KEY }} 11 runs-on: ubuntu-latest 12 steps: 13 ... 14 15 - name: 'Configure AWS Credentials' 16 uses: aws-actions/configure-aws-credentials@v4.0.2 17 with: 18 role-to-assume: arn:aws:iam::123456789123:role/GhAIBucketUserRole 19 aws-region: us-east-1 20 21 ... 22 23 - run: pnpm exec nx affected -t lint test build 24

INI Config Files

AWS can read your authentication credentials from shared INI config files. The files are located at ~/.aws/credentials and ~/.aws/config. Both files are expected to be INI formatted with section names corresponding to profiles. Sections in the credentials file are treated as profile names, whereas profile sections in the config file must have the format of [profile profile-name], except for the default profile. Profiles that appear in both files will not be merged, and the version that appears in the credentials file will be given precedence over the profile found in the config file.

Single Sign-On

Nx can read the active access token created after running aws sso login then request temporary AWS credentials. You can create the AwsCredentialIdentityProvider functions using the inline SSO parameters (ssoStartUrl, ssoAccountId, ssoRegion, ssoRoleName) or load them from AWS SDKs and Tools shared configuration and credentials files. Profiles in the credentials file are given precedence over profiles in the config file.

Credentials in nx.json File

Storing your credentials in the nx.json file is the least secure of the 4 authentication options, since anyone with read access to your codebase will have access to your AWS credentials.

nx.json
1{ 2 "s3": { 3 "ssoProfile": "default", 4 "accessKeyId": "MYACCESSKEYID", 5 "secretAccessKey": "MYSECRETACCESSKEY" 6 } 7} 8
PropertyDescription
ssoProfileThe name of the profile to use from your AWS CLI SSO Configuration (optional)
endpointThe AWS endpoint URL (optional)
accessKeyIdAWS Access Key ID (optional)
secretAccessKeyAWS secret access key (optional)

3. Configure S3 Cache

Regardless of how you manage your AWS authentication, you need to configure your Nx cache in the nx.json file. The bucket that you specify needs to already exist - Nx doesn't create it for you.

nx.json
1{ 2 "s3": { 3 "region": "us-east-1", 4 "bucket": "my-bucket", 5 "encryptionKey": "create-your-own-key" 6 } 7} 8
PropertyDescription
regionThe ID of the AWS region to use
bucketThe name of the S3 bucket to use
encryptionKeyNx encryption key used to encrypt and decrypt artifacts from the cache (optional)

S3 Compatible Providers

To use @nx/s3-cache with S3 compatible providers (MinIO, LocalStack, DigitalOcean Spaces, Cloudflare, etc.), endpoint will need to be provided. Some providers also need to have forcePathStyle set to true in the configuration.

Below is an example on how to connect to MinIO:

nx.json
1{ 2 "s3": { 3 "region": "us-east-1", 4 "bucket": "my-bucket", 5 "endpoint": "https://play.min.io", 6 "forcePathStyle": true, 7 "accessKeyId": "abc1234", 8 "secretAccessKey": "4321cba" 9 } 10} 11
PropertyDescription
regionThe ID of the S3 compatible storage region to use
bucketThe name of the S3 compatible storage bucket to use
forcePathStyleChanges the way artifacts are uploaded. Usually used for S3 compatible providers (MinIO, LocalStack, etc)
endpointThe custom endpoint to upload artifacts to. If endpoint is not defined, the default AWS endpoint is used
accessKeyIdAWS Access Key ID (optional if AWS_ACCESS_KEY_ID is set in the environment)
secretAccessKeyAWS secret access key (optional if AWS_SECRET_ACCESS_KEY is set in the environment)

Cache Modes

By default, Nx will try to write and read from the remote cache while running locally. This means that permissions must be set for users who are expected to access the remote cache.

Nx will only show warnings when the remote cache is not writable. You can disable these warnings by setting localMode to read-only or no-cache in the nx.json file.

nx.json
1{ 2 "s3": { 3 "region": "us-east-1", 4 "bucket": "my-bucket", 5 "localMode": "read-only" 6 } 7} 8

The cache mode in CI can also be configured by setting ciMode to read-only or no-cache in the nx.json file. Or by setting NX_POWERPACK_CACHE_MODE to read-only or no-cache in the CI environment.

nx.json
1{ 2 "s3": { 3 "region": "us-east-1", 4 "bucket": "my-bucket", 5 "ciMode": "read-only" 6 } 7} 8

Migrating from Custom Tasks Runners

Many people who are interested in Nx Powerpack have previously used custom task runners. Nx offers a new and simpler extension API designed to meet the same use cases as the now-deprecated custom task runners.

To learn more about migrating from custom task runners, please refer to this detailed guide.