wizard
wizard

Reputation: 1554

LaunchConfiguration Userdata vs AWS::CloudFormation::Init

In a cloudformation template, which are the differences between defining the initializazion script into the Userdata section of a LaunchConfiguration resource, or by using AWS::CloudFormation::Init metadata? In which cases should we prefer one over the other? Let's suppose I have to setup the EC2 instaces, based on this LaunchConfiguration, installing tomcat and defining some config file, and maybe copying some packages from an S3 bucket. It's better doing it via a Userdata bash script or via an AWS::CloudFormation::Init section?

Thanks.

Upvotes: 3

Views: 2484

Answers (2)

Seb
Seb

Reputation: 5842

AWS suggest Init over UserData, reference here:

Unlike the application specification coded in an EC2 user data script, the application configuration specified in AWS::CloudFormation::Init is updatable. This is handy, for example, when you want to install a new version of a package, without recreating a running instance. AWS::CloudFormation::Init supports securely downloading application packages and other data.

PS: appears it can work across reboots too (ref above), which is something UserData does not.

Upvotes: 0

Anton
Anton

Reputation: 4062

At the end of the day, all these approaches achieve the same goal - run some user-defined actions upon the instance initialization.

Launch Configuration and Launch Template allow you to specify the configuration of your instance once and then reuse it in multiple places. With or without CloudFormation. Launch Configuration is specific to the AutoScaling group. If you need to spin up an instance, that is not in the autoscaling group, use Launch Template to achieve the same result.

Now, in both cases above you can use either Bash script in UserData or AWS::CloudFormation::Init.

Bash in UserData is just that - Bash script. If you are familiar with it and feel confident that you can achieve what you need in just Bash - go for it. AWS::CloudFormation::Init is a higher-level abstract, simplifying bunch of things, such as file creation, permissions etc. Nothing you can't do with just bash, but surely making it easier and more maintainable.

One thing to keep in mind - bash+userdata approach would work as is on all cloud providers, not limited to AWS. Google, Azure. -they would let you run the same scripts with maybe minor modifications. AWS::CloudFormation::Init is AWS - specific.

Upvotes: 4

Related Questions