<p>When you create an Auto Scaling group with Terraform, you can specify the <code>user_data</code> to be used by instances created by this ASG. Documented here - <a href="https://www.terraform.io/docs/providers/aws/r/launch_configuration.html#user_data" rel="noreferrer">https://www.terraform.io/docs/providers/aws/r/launch_configuration.html#user_data</a></p>

<p>You can also create a single EC2 instance, and provide <code>user_data</code> to be used - <a href="https://www.terraform.io/docs/providers/aws/r/instance.html#user_data" rel="noreferrer">https://www.terraform.io/docs/providers/aws/r/instance.html#user_data</a></p>

<p>The AWS EC2 documentation explains how <code>user_data</code> is passed to the <code>cloud-init</code> service which is running on most Linux distributions available as AMIs on AWS - <a href="http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html#user-data-cloud-init" rel="noreferrer">http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html#user-data-cloud-init</a></p>

CC BY-SA 3.0
2017-02-28T17:04:51.597Z
2017-02-28T17:04:51.597Z
Evgeny Zislis
<p>Cloud-init files are essentially bootstrap codes, that run before each startup, and can - among others - modify files, set up services, create users, etc.</p>

<p>Not all types of droplets support all functionalities of cloud-init, for example CoreOS uses it's own implementation, with a very limited subset of valid values.</p>

<p>To use this in terraform, simply provide the cloud-init file during droplet creation:</p>

<p><code>main.tf</code>:</p>

<pre><code>resource "digitalocean_droplet" "web" {
  image              = "coreos-stable"
  name               = "web"
  region             = "lon1"
  size               = "2gb"
  private_networking = true
  ssh_keys           = ["${digitalocean_ssh_key.dodemo.id}"]
  user_data          = "${file("web.conf")}"
}
</code></pre>

<p><code>web.conf</code>:</p>

<pre><code>#cloud-config
coreos:
  units:
    - name: "etcd2.service"
      command: "start"
    - name: "fleet.service"
      command: "start"
</code></pre>

<p>This will for example create a droplet, where CoreOS will run etcd2 and fleet during startup</p>

<p>You can find some more examples <a href="https://github.com/sztupy/doclusterdemo/tree/master/terraform" rel="noreferrer">in this repository</a>, where I show how one can use these configuration options to set up some simple docker based services on CoreOS</p>

CC BY-SA 3.0
2017-02-28T17:21:28.930Z
2017-02-28T17:21:28.930Z
SztupY
<p>I am working with Digital Ocean and Terraform and I already can automate the domain, subdomain, network preferences and the host but there is a section called <code>User data</code> that looks like this:</p>

<p><a href="https://i.sstatic.net/fPCZ4.png" rel="noreferrer"><img src="https://i.sstatic.net/fPCZ4.png" alt="User data"></a></p>

<p>The description of that field says <code>Allows the use of Cloud-init to configure your droplet</code>. Looking around I found the <a href="https://cloudinit.readthedocs.io/en/latest/" rel="noreferrer">documentation</a>.</p>

<p>How to take advantage of this while using <a href="https://www.terraform.io/" rel="noreferrer">Terraform</a>?</p>

<terraform><cloud-init>
How to use cloud-init with Terraform?
Gepser Hoil
# How to use cloud-init with Terraform?

(Asked by: username_0 on 2017-02-28)

I am working with Digital Ocean and Terraform and I already can automate the domain, subdomain, network preferences and the host but there is a section called `User data` that looks like this:

[![User data](https://i.sstatic.net/fPCZ4.png)](https://i.sstatic.net/fPCZ4.png)

The description of that field says `Allows the use of Cloud-init to configure your droplet`. Looking around I found the [documentation](https://cloudinit.readthedocs.io/en/latest/).

How to take advantage of this while using [Terraform](https://www.terraform.io/)?

**Tags:** terraform, cloud-init


---

## 2 answers

### Answer by username_2 on 2017-02-28. Score: 25 | Accepted answer

Cloud-init files are essentially bootstrap codes, that run before each startup, and can - among others - modify files, set up services, create users, etc.
Not all types of droplets support all functionalities of cloud-init, for example CoreOS uses it's own implementation, with a very limited subset of valid values.
To use this in terraform, simply provide the cloud-init file during droplet creation:
main.tf:
resource "digitalocean_droplet" "web" {
  image              = "coreos-stable"
  name               = "web"
  region             = "lon1"
  size               = "2gb"
  private_networking = true
  ssh_keys           = ["${digitalocean_ssh_key.dodemo.id}"]
  user_data          = "${file("web.conf")}"
}

web.conf:
#cloud-config
coreos:
  units:
    - name: "etcd2.service"
      command: "start"
    - name: "fleet.service"
      command: "start"

This will for example create a droplet, where CoreOS will run etcd2 and fleet during startup
You can find some more examples in this repository, where I show how one can use these configuration options to set up some simple docker based services on CoreOS


---

### Answer by username_1 on 2017-02-28. Score: 6

When you create an Auto Scaling group with Terraform, you can specify the `user_data` to be used by instances created by this ASG. Documented here - <https://www.terraform.io/docs/providers/aws/r/launch_configuration.html#user_data>

You can also create a single EC2 instance, and provide `user_data` to be used - <https://www.terraform.io/docs/providers/aws/r/instance.html#user_data>

The AWS EC2 documentation explains how `user_data` is passed to the `cloud-init` service which is running on most Linux distributions available as AMIs on AWS - <http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html#user-data-cloud-init>


---

