Giter Site home page Giter Site logo

Support for userData Script about cfndsl HOT 8 CLOSED

cfndsl avatar cfndsl commented on August 20, 2024
Support for userData Script

from cfndsl.

Comments (8)

howech avatar howech commented on August 20, 2024

cfndsl implements a very crude templating system through its FnFormat call. It takes a string, followed by a hash of key-value pairs, and it takes care of generating cfn json templates that contain the replacements.

For instance...

FnFormat( <<<EOF,
#!/bin/bash
echo "%{greeting} world!"
EOF
           { :greeting => 'hello' } )

will actually generate template json that looks like this

{"Fn::Join": [ "", "#!/bin/bash\necho \", "hello", "\" world"\n"]]}

which ends up resolving at template invocation time to the script you want.

Note that if you are using Resource(...) { Property(...) } you are missing out on some of the coolness that cfndsl provides. Instead you could say:

CloudFormation {
  Instance(:instance1) {
    UserData FnBase64("userdata here")
  }
  Instance(:instance2) {
    UserData FnBase64( FnFormat( <<EOF,
#!/bin/sh
cat
echo "instance1 has instance id %{instance1}" > %{file}
EOF
        { :file => '/tmp/inst1_id'}) 
  }
}

Ok, so there is a lot going on here. First, you are invoking the deeper part of the dsl, where the
"Instance(:resource_name)" method takes care declaring a resource of type AWS::EC2::Instance. The block you pass to it gets evaluated in the context of an object representing the resource, and it has setter methods for all of the things declared as Parameters for the AWS::EC2::Instance type. There is some other syntactic sugar for doing this. You can pass a symbol like :instance1 to FnRef, and it will actually check to see that you declared such a resource when you try to compile the template json. Also, if you use resource names as template variables in an FnFormat call, they get resolved to an FnRef in the giant Fn::Join that cfndsl generates.

from cfndsl.

howech avatar howech commented on August 20, 2024

I guess if you have a specific way to improve on what cfndsl already does, we would love to hear about it, but for now I am marking this issue closed.

from cfndsl.

fedecz avatar fedecz commented on August 20, 2024

Hi Chris, thanks for taking the time to write such a good reply. I didn't know about FnFormat(). I was achieving the same thing by calling FnBase64(FnJoin(...)) in order to get the Ref() values.

I didn't know about Instance() syntax either. There is no example using that (nor FnFormat() ) in the examples, or Readme. When I'm done with this, I'll make a pull request with some more examples if you don't mind.

Thanks again

Fede

from cfndsl.

fedecz avatar fedecz commented on August 20, 2024

Where can I find the different resource types (such as Instance() ) that are supported by cfndsl ?
thanks

from cfndsl.

fedecz avatar fedecz commented on August 20, 2024

How would you write an Instance type, when it has metadata "AWS::CloudFormation::Init " ?

from cfndsl.

allinwonder avatar allinwonder commented on August 20, 2024

In my development, I address the Metadata as one of properties of
resources, so I use Property method with parameter "
AWS::CloudFormation::Init" and some json data to specify metadata.

On Mon, Jul 14, 2014 at 11:39 AM, Federico Czerwinski <
[email protected]> wrote:

How would you write an Instance type, when it has metadata
"AWS::CloudFormation::Init " ?


Reply to this email directly or view it on GitHub
#57 (comment).

from cfndsl.

fedecz avatar fedecz commented on August 20, 2024

Thanks Allinwonder, I do the same thing. My question was actually how do you specify a Cloudformatio::init parameter using the 'instance()' syntax. (not using resource() )

cheers

Fede.

from cfndsl.

allinwonder avatar allinwonder commented on August 20, 2024

Hi Fede, I think to use "AWS::Cloudformation::Init" inside "Instance()"
method is the same way as you use in other resources.

I prefer to declare resource using the "Resource()" method with resource
name and use "Type()" method inside to define the resource type.

For example

Resource("MyInstance") do
  Type("AWS::EC2::Instance")

  # add more properties ...

  MetaData("AWS::CloudFormation::Init",
    { :config => {
        :packages => {
          :yum => {
            'ruby-devel' => []
          }
        }
      }
    })
end

from cfndsl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.