@nx/powerpack-s3-cache

The @nx/powerpack-s3-cache plugin enables you to use an Amazon S3 bucket instead of Nx Cloud to host your remote cache.

This plugin will enable the remote cache for your Nx workspace, but does not provide any of the other features of Nx Cloud. If you want to leverage distributed task execution, re-running flaky tasks or automatically splitting tasks, you'll need to connect to Nx Cloud and use Nx Replay instead.

Potential Cache Poisoning

Using your own Amazon S3 bucket to host the remote cache opens you up to the possibility of cache poisoning. To avoid this, use Nx Replay.

This plugin requires an active Nx Powerpack license

In order to use @nx/powerpack-s3-cache, you need to have an active Powerpack license. If you don't have a license or it has expired, your cache will no longer be shared and each machine will use its local cache.

Set Up @nx/powerpack-s3-cache

1. Install the Package

  1. Activate Powerpack if you haven't already
  2. Install the package

โฏ

nx add @nx/powerpack-s3-cache

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 OICD in GitHub Actions to set the environment variables in CI:

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

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 code base 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 AWS bucket to use
encryptionKeyNx encryption key used to encrypt and decrypt artifacts from the cache (optional)

Package reference

Here is a list of all the executors and generators available from this package.

Guides

Executors

Generators

  • init

    Initialize the S3 Cache