AWS cloudformation, meta data and local file templates

Gert-Jan van de Streek

Gert-Jan van de Streek

Published: 28 August, 2015

I have been trying to use mustache templates to create files on EC2 instances via cloudformation. Huh? I lost you already. Ok you might have to do some reading here, here and specifically for this post here and here.

If you are still here, it's most probably, because you ran into an error message like this one:

Failed to retrieve file: No connection adapters were found for 'file:///tmp/ddb-transactor.mustache'

All the examples use an http:// url to fetch a mustache template from a public s3 bucket. I was looking to use a local file. But apparently file:// url's are not supported.

I ended up using an s3 bucket anyways, but not a public one. Here is the snippet from my UserData:

"aws s3 cp s3://third-party-releases/ddb-transactor.mustache .", "\n",
"python -m SimpleHTTPServer 8000 &", "\n",
"/opt/aws/bin/cfn-init -v ",
" --stack ", { "Ref" : "AWS::StackName" },
" --resource TransactorLaunchConfig ",
" --region ", { "Ref" : "AWS::Region" }, "\n",
"killall python", "\n",

What this does is copy a file over from s3, store it locally, then serve it via http with a litte python magic. Now you are ready to run cfn-init, which creates the files on your instance that you specified in MetaData:

"files" : {
 "/tmp/ddb-transactor.properties" : {
 "source" : "http://localhost:8000/ddb-transactor.mustache",
 "context" : {
   "aws-dynamodb-table" : "RelationsForJira",
   "aws-dynamodb-region" : { "Ref" : "AWS::Region" },
   "aws-transactor-role" : { "Ref" : "TransactorRole" },
   "aws-peer-role" : { "Ref" : "ApplicationRole" }
 }
}

You can see that you can use http:// urls now, referring to the locally running http server. After cfn-init is done, the python script is killed with killall.

Hope this helps and if you find a better way, let me know.

Related blogs

Did you enjoy reading?

Share this blog with your audience!