0

I'd like to be able to deploy new endpoints for my api from the command line, ideally with a single npm script.

The hangup is that you first have to use create-resource supplying a parent id, and then put-method supplying the id returned from create-resource.

This carves up deploying into a series of small steps that I can't string together, because I need these unique id's.

Any way around this? I'm using npm scripts exclusively so far with getting lambdas up, which has been fairly pleasant.

Costa Michailidis
  • 7,691
  • 15
  • 72
  • 124

1 Answers1

1

In your package.json a script can point to a javascript file. The file in turn could contain a more robust script.

"scripts": {
  "createEndpoint": "node ./myscript.js"
}

To do it all in one command/script you might want to look at how to process arguments this question/answer talks about how it works https://stackoverflow.com/a/14404223/10555693. Additionally, NodeJS docs for argv NodeJS: process.argv

The more robust script could make use of the JS AWS SDK, take in arguments, and create the resource, method, and deployment.

Some specific method references you might find useful:

Example (Just a start):

const AWS = require('aws-sdk');

async function createEndPoint() {
  const apiGateway = new AWS.APIGateway();

  const resourceParams = {
    parentId: '',
    pathPart: '',
    restApiId: '',
  };

  const newResource = await apiGateway.createResource(resourceParams).promise();

  const methodParams = {
    resourceId: newResource.id,
    . /* other params */
    .
    .
  };

  const newMethod = await apiGateway.putMethod(methodParams);
}

createEndPoint();
ajb3ck
  • 146
  • 4
  • Lovin' the await/async stuff here : ) Thanks for the advice. Was trying to keep it all in the npm scripts, but perhaps that's wishful thinking. I have a deploy script like this for my static site. Maybe I'll need one for lambdas running backend services as well. – Costa Michailidis Oct 25 '18 at 23:49