rumdrums
rumdrums

Reputation: 1382

Reference Existing Cloudformation Stack Outputs in Serverless Framework

We're evaluating whether to use Serverless for some of our new AWS application infrastructure. We heavily use Cloudformation (deployed by Ansible), so we would need to be able to cleanly reference the outputs of existing Cloudformation stacks -- one immediate example would be for getting the subnet IDs of our existing AWS network infrastructure for use by a lambda function.

After a lot of browsing, I haven't seen an out-of-the-box way to do this. Our existing Cloudformation stacks are named such that if I could just key in the name of the stack and the desired output variable, I could reliably get the desired outputs across various environments. One possible solution I see is to pull the variables using aws cli and pass them as environment variables to serverless, but I would like a cleaner way if possible.

Upvotes: 2

Views: 2573

Answers (2)

wjordan
wjordan

Reputation: 20390

If the Serverless Framework allows you to use Intrinsic Functions within your CloudFormation templates, you can create cross-stack references within a CloudFormation template by exporting stack output values from one stack (using the Exports property in the Outputs section), and using the Fn::ImportValue intrinsic function in another stack to reference the exported value.

Upvotes: 5

asdf
asdf

Reputation: 3067

The easiest way I can think to handle your example case is to have the lambda use boto3 to call boto3.client('cloudformation', region_name=*specified region*).describe_stacks(StackName=*specified stack*)['Stacks']. This list contains all stacks that match the StackName specified, if all of your network infrastructure shares a subset of their names, you can list all of them by specifying the StackName to that substring. Each Stack object contains an 'Outputs' block. See here.

If you'd like to expose this for easy use from anywhere, you can attach an API Gateway GET method to the lambda and expose it to an HTML form.

Upvotes: 0

Related Questions