# Create autotask from code

**URL:** <https://forum.openzeppelin.com/t/create-autotask-from-code/32895>\
**Category:** Defender\
**Tags:** defender\
**Created:** [November 3, 2022, 12:46am UTC](https://forum.openzeppelin.com/t/create-autotask-from-code/32895 "2022-11-03T00:46:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![CryptoMurphy](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/cryptomurphy/32/11898_2.png) [@CryptoMurphy](https://forum.openzeppelin.com/u/CryptoMurphy)\
**Post date:** [November 3, 2022, 12:46am UTC](https://forum.openzeppelin.com/t/create-autotask-from-code/32895/1 "2022-11-03T00:46:15Z")

</div>

**💻 Environment**

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

**📝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

```javascript
    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

```bash
  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'
    }
  }

```

**🔢 Code to reproduce**

Full code:

```javascript
// 🟢 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

---

<div class="post-metadata">

**Author:** ![bjmrq](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/bjmrq/32/15749_2.png) [@bjmrq](https://forum.openzeppelin.com/u/bjmrq)\
**Post date:** [November 3, 2022, 1:01am UTC](https://forum.openzeppelin.com/t/create-autotask-from-code/32895/2 "2022-11-03T01:01:12Z")

</div>

Hi @CryptoMurphy thanks for raising the issue!

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

Good continuation!

---

<div class="post-metadata">

**Author:** ![CryptoMurphy](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/cryptomurphy/32/11898_2.png) [@CryptoMurphy](https://forum.openzeppelin.com/u/CryptoMurphy)\
**Post date:** [November 3, 2022, 1:03am UTC](https://forum.openzeppelin.com/t/create-autotask-from-code/32895/3 "2022-11-03T01:03:16Z")

</div>

Fixed, thanks @bjmrq!

I was starting to get crazy 😅
