weixu365 / serverless-scriptable-plugin

Adding script support to Serverless 1.x which enables you to customize Serverless behavior without writing a plugin.
MIT License
112 stars 11 forks source link

How to reference serverless variables in bash script #30

Closed grayaii closed 5 years ago

grayaii commented 5 years ago

This is a question, not an issue. I see that this plugin supports running bash scripts.

My serverless.yml creates some AWS resources. I want my bash script to know the ARN's of those various resources.

Is this possible? Is there an example of this?

Thanks!

weixu365 commented 5 years ago

I'm not sure if there's a simple way to get the ARNs, what I tried before is:

andreimcristof commented 5 years ago

hi, I was trying to find out how to do this too (I need to get the names and urls that are created for funcitons, api gateway etc, in an "after" script when everything is done), I see the README example referencess serverless variable:

const resources = serverless.service.provider.compiledCloudFormationTemplate.Resources;

but in the .js script it throws with serverless undefined. could you please shed light on how to expose the serverless variable to fetch all configs created? Thanks,

weixu365 commented 5 years ago

@andreimcristof

You can try the following config and script:

serverless.yml

service: demo

provider:
  name: aws
  runtime: nodejs8.10
  region: ap-southeast-2

plugins:
  - serverless-scriptable-plugin

custom:
  scriptHooks:
    after:aws:deploy:finalize:cleanup: ./geturl.js

functions:
  test:
    handler: handler.handle

geturl.js

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

// You can find out all the resources, filter by type
const resources = serverless.service.provider.compiledCloudFormationTemplate.Resources;
Object.keys(resources)
  // .filter(name => resources[name].Type === 'AWS::Logs::LogGroup')
  .forEach(r => console.log(r));

// Using aws sdk to find out the resource info, I'm using lambda function as an example.
// You can change to API Gateway to get the physical id
let cloudformation = new AWS.CloudFormation({ region: 'ap-southeast-2' });
cloudformation.describeStackResource({
  LogicalResourceId: 'TestLambdaFunction',
  StackName: `${serverless.service.service}-${serverless.service.provider.stage}`,
}).promise()
  .then(response => console.log(response['StackResourceDetail']));

Output on my machine:

Running javascript file: ./geturl.js
ServerlessDeploymentBucket
TestLogGroup
IamRoleLambdaExecution
TestLambdaFunction
TestLambdaVersionZ8Wrs8X2xzZU1YAYTIxrQOLG7KCbTrioAWqTUNLWqVc
{ StackName: 'demo-dev',
  StackId: 'arn:aws:cloudformation:ap-southeast-2:xxxx:stack/demo-dev/010dfda0-d47f-11e9-bc6d-0230e8e86e24',
  LogicalResourceId: 'TestLambdaFunction',
  PhysicalResourceId: 'demo-dev-test',
  ResourceType: 'AWS::Lambda::Function',
  LastUpdatedTimestamp: 2019-09-11T10:53:56.807Z,
  ResourceStatus: 'UPDATE_COMPLETE',
  Metadata: '{}',
  DriftInformation: { StackResourceDriftStatus: 'NOT_CHECKED' } }

Available Serverless lifecycle events: https://github.com/weixu365/serverless-scriptable-plugin#hooks

weixu365 commented 5 years ago

The other option is to add Output of the resources, you can reference here: http://www.goingserverless.com/blog/api-gateway-url

andreimcristof commented 5 years ago

@weixu365 you're an absolute rockstar. Thanks!