Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save codingforentrepreneurs/f40bc8be82e8f6208bf541ffb4f227c7 to your computer and use it in GitHub Desktop.
Save codingforentrepreneurs/f40bc8be82e8f6208bf541ffb4f227c7 to your computer and use it in GitHub Desktop.
Extract Endpoint from Serverless Framework

Extract Endpoint from Serverless Framework

The serverless framework lacks native support for outputting the endpoint url for a deployment:

serverless info --stage prod --region us-east-2

Outputs

DOTENV: Loading environment variables from .env, .env.prod:
         - SOME_VAR
service: my-service
stage: prod
region: us-east-2
stack: my-service-prod
endpoint: ANY - https://some-endoint.execute-api.some-region.amazonaws.com
functions:
  api: my-service-prod-api

The point of this script is to extract https://some-endoint.execute-api.some-region.amazonaws.com from this output.

Here's how it works:

Install Requirements

npm install --save-dev tsx js-yaml

Copy parseEndpoint.js Gist

Copy the script (parseEndpoint.js) locally and run it with a | suc as:

export ENDPOINT_URL=$(serverless info --stage prod --region us-east-2 | npx tsx parseEndpoint.js
echo $ENDPOINT_URL

There you go. A simple way to automate extracting this endpoint.

// usage:
// npx serverless info --stage prod --region us-east-2 | npx tsx parseEndpoint.js
// export ENDPOINT_URL=$(npx serverless info --stage prod --region us-east-2 | npx tsx parseEndpoint.js)
// install: npm install --save-dev tsx js-yaml
const yaml = require('js-yaml');
let inputData = '';
// Read data from stdin
process.stdin.on('data', function(data) {
inputData += data;
});
// When all data is received, parse the YAML and extract the endpoint
process.stdin.on('end', function() {
try {
const data = yaml.load(inputData);
const endpoint = data.endpoint.split(' - ')[1]; // Adjust as necessary
console.log(endpoint);
} catch (e) {
console.error(`Error parsing YAML: ${e}`);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment