Create autotask from code

:computer: Environment

  • Windows
  • Node v16.14.2
  • defender-autotask-client - 1.34.0
  • defender-relay-client - 1.34.0

:memo:Details

I want to execute an index.js. This index.js will check if exist a relayer and an autotask and create them in case they don't exist. The code of the autotask is exactly the same index.js file.

When I try the next code

    const ROLE = "RoleInContract";
    const NETWORK = "goerli";
    const AUTOTASK_CODE_PATH = "./path/to/folder/containing/indexjs/";
    const AUTOTASK_FREQUENCY_MINUTES = 60;

    let zippedCode = await autotaskClient.getEncodedZippedCodeFromFolder(
      AUTOTASK_CODE_PATH
    );

    const newAutotask = {
      name: ROLE,
      encodedZippedCode: zippedCode,
      relayerId: relayerId,
      trigger: {
        type: AUTOTASK_TYPE,
        frequencyMinutes: AUTOTASK_FREQUENCY_MINUTES,
      },
      paused: false,
    };

    autotaskResponse = await autotaskClient.create(newAutotask);

I obtain the next error

  request: { path: '/autotask/autotasks', method: 'POST' },       
  response: {
    status: 500,
    statusText: 'Internal Server Error',
    data: {
      message: 'User: arn:aws:sts::665768922667:assumed-role/defender-api-api-autotask-management-role/defender-api-api-autotask-create is not authorized to perform: lambda:GetLayerVersion on resource: arn:aws:lambda:us-west-2:665768922667:layer:defender-api-prod-api-autotask-wrapper-layer:2 because no identity-based policy allows the lambda:GetLayerVersion action'
    }
  }

:1234: Code to reproduce

Full code:

// 🟢 Running in local:
// $ nvm use 18.2.0
// $ node ./path/to/folder/containing/indexjs/index.js

const { RelayClient } = require("defender-relay-client");
const { AutotaskClient } = require("defender-autotask-client");
require("dotenv").config();

const ROLE = "RoleInContract";
const NETWORK = "goerli";
const AUTOTASK_CODE_PATH = "./path/to/folder/containing/indexjs/";
const AUTOTASK_FREQUENCY_MINUTES = 60;

// Entrypoint for the Autotask
exports.handler = async function (credentials) {
  // Create reinvestor autotask
  let autotaskId = "";
  const autotaskClient = new AutotaskClient(credentials);
  const autotasks = await autotaskClient.list();
  let autotaskResponse = autotasks.items.find(
    (autotask) => autotask.name === ROLE
  );

  if (!autotaskResponse) {
    let zippedCode = await autotaskClient.getEncodedZippedCodeFromFolder(
      AUTOTASK_CODE_PATH
    );

    const newAutotask = {
      name: ROLE,
      encodedZippedCode: zippedCode,
      relayerId: relayerId,
      trigger: {
        type: AUTOTASK_TYPE,
        frequencyMinutes: AUTOTASK_FREQUENCY_MINUTES,
      },
      paused: false,
    };

    autotaskResponse = await autotaskClient.create(newAutotask);
  }

  return await Promise.all(executions);
};

// To run locally (this code will not be executed in Autotasks)
if (require.main === module) {
  const RELAYER_API_KEY = process.env.RELAYER_API_KEY || "";
  const RELAYER_SECRET_KEY = process.env.RELAYER_SECRET_KEY || "";

  const credentials = {
    apiKey: RELAYER_API_KEY,
    apiSecret: RELAYER_SECRET_KEY,
  };

  exports
    .handler(credentials)
    .then(() => process.exit(0))
    .catch((error) => {
      console.error(error);
      process.exit(1);
    });
}

Thanks in advanced for your help

Hi @CryptoMurphy thanks for raising the issue!

It seems there has been a misconfiguration, it should be fixed now.

Good continuation!

Fixed, thanks @bjmrq!

I was starting to get crazy :sweat_smile:

1 Like