DEV Community

R
R

Posted on

Using AWS SDK from n8n Code Node

HTTP Request vs. JS Client Code

Many of the service-integration nodes in n8n are just customized wrappers for a RESTful, http based API. If the entire API is not supported in a particular service-integration node, it is often possible to use an HTTP Request node to implement other RESTful service API calls. It is even possible to "borrow" the credentials item from the service-integration node and use it in the HTTP Request node.

Some services, like (Amazon) AWS services are (apparently) are too complex to support with a simple http based client, so they generally require the use of an SDK client library, and cannot be implemented within n8n by simply using an HTTP Request node. n8n also has a Code node that can use npm based libraries/modules with a bit of special configuration in the n8n runtime environment. This article shows how to use AWS SDK libraries in an n8n Code node.

Prerequisites

  • n8n self-hosted, preferably running in Docker / Docker Compose
  • AWS Account + AWS IAM User + AWS Access Key for the IAM User
  • Basic knowledge of Javascript and JS Object Notation
  • Basic knowledge of Docker, docker build, docker compose

Overview / Steps

  1. Get an Access Key & Access Secret for an AWS IAM User.
  2. Assign Permissions (Policies), to the IAM User, for the SDK to be used.
  3. Create an n8n container with the SDK npm libraries installed.
  4. Configure the Docker runtime (compose) with required environment vars.
  5. Use an n8n Code node to run SDK client code in a workflow.

Example SDK: Polly Text-To-Speech

To demonstrate the use of an AWS SDK library, and handling a binary data response, the example use-case here will call the Polly Text-to-Speech (TTS) service to generate an audio file from text input. The general approach to installing, enabling and using AWS SDK libraries should be applicable to other SDKs, but it's sometimes difficult to understand instructions given in generalized/abstract terms, so hopefully it will be easier to understand this concrete example.

SDK Access Policy

Before using any of the various AWS services via SDK, the authenticated user needs to have access to the service. AWS manages this primarily through policies. Access to the Polly API service can be granted by adding the AWSPollyFullAccess policy to the IAM user to which the Access Key/Secret belongs. (Links below to the AWS user/policy management page.)

Setting up the n8n Runtime Environment

This assumes n8n is running self-hosted, in a Docker container. There are 3 elements of the n8n runtime environment that need to be customized in order to get an AWS SDK client to work.

  1. Install the SDK module/library, and its dependencies
  2. Allow n8n to use the SDK module/library (and dependencies)
  3. Provide configuration/environment-variable-values required by the SDK.

Installing (Polly) SDK and AWS CredentialsProviders npm modules/libraries

Use docker build with a Dockerfile like the following to create a customized n8n (Docker) container image with the AWS SDK npm libraries added.

  • Dockerfile
  ARG tag=latest
  FROM n8nio/n8n:$tag
  USER root
  RUN npm install -g @aws-sdk/credential-providers
  RUN npm install -g @aws-sdk/client-polly
  USER node
Enter fullscreen mode Exit fullscreen mode

Allow n8n to use the AWS SDK modules/libraries

Use the image created in the first step instead of the base, published n8n image, specify (or add) the npm library/module names in the NODE_FUNCTION_ALLOW_EXTERNAL environment variable's csv list

  • docker-compose.yml

    services:
      n8n:
        image: n8n-with-aws-sdk-addons
      ...
        environment:
          - N8N_HOST=n8n
          - N8N_PORT=5678
          ...
          - NODE_FUNCTION_ALLOW_EXTERNAL=@aws-sdk/credential-providers,@aws-sdk/client-polly
    

Set AWS SDK credentials environment variables

In this case, the Code node will use the fromEnv() credentials provider, so it will need environment variables with the access key and secret.

  • docker-compose.yml

    services:
      n8n:
      ...
        environment:
          - N8N_HOST=n8n
          - N8N_PORT=5678
          ...
          - AWS_ACCESS_KEY_ID={your-aws-access-key}
          - AWS_SECRET_ACCESS_KEY={your-aws-secret}
    

Create a Code node to use the SDK Library/Module

The following example code takes a number of input items and, for each one, calls the Polly AWS service to convert the input text to a generated audio file.

  • The input item must include the text-to-speech input text in an attribute named ttsTextInput like this:

    {
      "otherAttribute": "any other json content",
      "ttsTextInput": "What would a wood chuck chuck if a wood chuck could chuck wood."
    }
    
  • Code node Javascript

const { PollyClient, SynthesizeSpeechCommand } = require("@aws-sdk/client-polly");
const { fromEnv } = require("@aws-sdk/credential-providers");

/*
 * This gets the credentials from environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.
 */
const config = {
  region: "us-east-1",
  credentials: fromEnv()
}

const client = new PollyClient(config);

async function sendPollyRequest(ttsTextInput) {
  const input = {
    OutputFormat: "mp3",
    Text: ttsTextInput,
    VoiceId: "Joanna"
  };
  const command = new SynthesizeSpeechCommand(input);
  const response = await client.send(command);
  const pollyResponseAudioBytes = await response.AudioStream.transformToByteArray();
  // console.log(`Received polly response with ${pollyResponseBytes.length} bytes`);
  return pollyResponseAudioBytes;
}

for (const item of $input.all()) {
  const pollyResponseAudioBytes = await sendPollyRequest(item.json.ttsTextInput);
  const pollyResponseAudioBytesBuffer = Buffer.from(pollyResponseAudioBytes);
  const n8nBinaryDataPollyAudio = {
       data: await this.helpers.prepareBinaryData(pollyResponseAudioBytesBuffer,
            'polly-audio-response.mp3', 'audio/mp3')
  }

  // console.log(`Binary data item: ${JSON.stringify(n8nBinaryDataPollyAudio)}`);
  item.binary = n8nBinaryDataPollyAudio;
}


return $input.all();
Enter fullscreen mode Exit fullscreen mode

Links and References

Notes

  • This article does not cover connectivity issues. If n8n is unable to reach AWS, the answers will not be here.
  • This article does not offer any advise on how to manage the cost of AWS services. Consuming API services without monitoring usage can lead to unexpected charges.
  • There are alternatives in n8n that might be easier to implement, like installing the AWS SDK CLI (command line interface) and using an Execute Command node in the workflow, or running a "sidecar" service that wraps AWS SDK functions with a RESTful/http-based API service and using an HTTP Request node to call that service instead of directly accessing AWS.
  • Docker Compose could be used to run the Docker build and create a container image from the Dockerfile automatically, instead of using docker build separately, but that is another topic that is covered in the Docker documentation.

Top comments (0)