bazaarvoice / cloudformation-ruby-dsl

Ruby DSL for creating Cloudformation templates
Apache License 2.0
210 stars 76 forks source link

Allow locals to be passed to interpolate. #39

Closed aquach closed 9 years ago

aquach commented 9 years ago

I found this pattern very useful for getting arbitrary parameters into files. For example, I have a cfn-init.sh script:

#!/bin/bash -xe

exec > >(tee -a /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1

echo BEGIN

apt-get install -y python-pip python-dev build-essential
pip install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz

cfn-init -v \
  --stack {{ ref('AWS::StackName') }} \
  --resource {{ locals[:resource_id] }} \
  --region {{ ref('AWS::Region') }}

cfn-signal -e $? \
  --stack {{ ref('AWS::StackName') }} \
  --resource {{ locals[:resource_id] }} \
  --region {{ ref('AWS::Region') }}

echo END

which requires the resource name. With this change, I can now do this:

interpolate(file('bootstrap-configs/cfn-init.sh'), { resource_id: 'MyInstance' })

The only way I could do this before was inlining the script into a heredoc in the ruby code, which is definitely less than ideal.

temujin9 commented 9 years ago

Sweet! I'm going to test this now; it will definitely streamline some of our messier stuff, if it works.