UNPKG

2.89 kBJavaScriptView Raw
1var AWS = require('aws-sdk');
2var utils = require('../lib/utils');
3var Response = require('../lib/response');
4
5module.exports = StackOutputs;
6
7/**
8 * Represents a CloudFormation stack's outputs
9 * @param {string} name - the name of the CloudFormation stack
10 * @param {string} region - the region the CloudFormation stack is in
11 */
12function StackOutputs(stackName, region) {
13 if (!stackName)
14 throw new Error('Missing Parameter StackName');
15 if (!region)
16 throw new Error('Missing Parameter Region');
17
18 this.stackName = stackName;
19 this.region = region;
20 this.cfn = new AWS.CloudFormation({ region: region });
21}
22
23/**
24 * A Lambda function to find a CloudFormation stack's outputs
25 * @static
26 * @param {object} event - a Lambda invocation event sent from a custom CloudFormation resource
27 * @param {object} context - the Lambda invocation context
28 * @example
29 * // a custom CloudFormation resource that is backed by this Lambda function must
30 * // provide the ARN for this Lambda function, the name and region of an existing
31 * // CloudFormation stack
32 * {
33 * "Type": "Custom::StackOutputs",
34 * "Properties": {
35 * "ServiceToken": "arn:aws:lambda:us-east-1:123456789012:function/stack-outputs",
36 * "Name": "my-stack",
37 * "Region": "us-east-1"
38 * }
39 * }
40 */
41StackOutputs.manage = function(event, context) {
42 if (!utils.validCloudFormationEvent(event))
43 return context.done(null, 'ERROR: Invalid CloudFormation event');
44
45 var response = new Response(event, context);
46
47 var requestType = event.RequestType.toLowerCase();
48 var message;
49 try {
50 message = new StackOutputs(
51 event.ResourceProperties.StackName,
52 event.ResourceProperties.Region
53 );
54 } catch (err) {
55 return response.send(err);
56 }
57
58 message[requestType](function(err, data) {
59 response.send(err, data);
60 });
61};
62
63/**
64 * A create event looks up the stack's outputs
65 * @param {function} callback - a function to handle the response
66 */
67StackOutputs.prototype.create = function(callback) {
68 var params = { StackName: this.stackName };
69 var stackName = this.stackName;
70 var region = this.region;
71
72 this.cfn.describeStacks(params, function(err, data) {
73 if (err) return callback(err);
74 if (!data.Stacks[0]) return callback(new Error('No stack named %s was found in %s'), stackName, region);
75
76 var outputs = data.Stacks[0].Outputs.reduce(function(outputs, o) {
77 outputs[o.OutputKey] = o.OutputValue;
78 return outputs;
79 }, {});
80
81 callback(null, outputs);
82 });
83};
84
85/**
86 * An update event looks up the stack's outputs
87 * @param {function} callback - a function to handle the response
88 */
89StackOutputs.prototype.update = StackOutputs.prototype.create;
90
91/**
92 * Nothing to do when an StackOutputs resource is deleted
93 * @param {function} callback - a function to handle the response
94 */
95StackOutputs.prototype.delete = function(callback) {
96 callback();
97};