<p>AWS AMIs come with a default username - which username it is depends on the specific AMI that is launched. Amazon Linux AMIs use <code>ec2-user</code>, CentOS uses <code>centos</code>, and there's other combinations too.</p>

<p>If you want to create your own user on instance launch, you can use <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html" rel="nofollow noreferrer">EC2 User Data</a> for this, which essentially entails passing a custom script that will be run at launch time.</p>

<p>The <a href="https://www.terraform.io/docs/providers/aws/r/instance.html#user_data" rel="nofollow noreferrer"><code>user_data</code> argument</a> is supported by Terraform's <code>aws_instance</code> resource. You can specify a script inline, but for most cases you'll probably want to use an external file or template.</p>

<p>For example:</p>

<p><strong>create-user.sh</strong>:</p>

<pre><code>#!/usr/bin/env bash

useradd mytestuser

# Anything else you want to do to set up your user account
</code></pre>

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

<pre><code>resource "aws_instance" "main" {
  ...
  user_data = "${file("create-user.sh")}"
}
</code></pre>

<p>Alternatively, there's an example on rendering a template <a href="http://roshpr.net/blog/2016/10/terraform-using-user-data-in-launch-configuration/" rel="nofollow noreferrer">here</a>, which is useful if you want to pass through custom values rather than just provide a static file.</p>

<p>Or, as an alternative to writing your own shell script, you could try rendering a cloudinit config file using <a href="https://www.terraform.io/docs/providers/template/d/cloudinit_config.html" rel="nofollow noreferrer"><code>template_cloudinit_config</code></a>. Some resources that might be useful for this include <a href="https://ash.berlintaylor.com/writings/2017/08/reusable-terraform-modules-extending-userdata/" rel="nofollow noreferrer">this blog post</a> and <a href="http://cloudinit.readthedocs.io/en/latest/topics/examples.html" rel="nofollow noreferrer">this part of the cloudinit documentation</a>.</p>

CC BY-SA 4.0
2018-06-30T08:36:07.093Z
Thanks,this is working..But i want know hoe we can pass the arguments in terraform while executing bash script>beacause i want to pass the RSA key through arguments.e.g.provisioner "local-exec" {
    command = "user2.sh '${aws_key_pair.key.public_key}' "
    }
MeetGadhiya
CC BY-SA 4.0
2018-06-30T14:03:54.690Z
I covered that in the second last para - see the link there to the example on rendering a template.
Tim Malone
CC BY-SA 4.0
2018-06-29T06:13:59.033Z
2018-06-29T06:13:59.033Z
Tim Malone
<p>I want to make new instance with "user1" rather than ec2-user(AWS) default.i want achieve this by terraform. Like in azure terraform it can be done by "os_profile{}", but how can i do in aws by terraform scripts?</p>

CC BY-SA 4.0
2018-06-30T08:10:12.217Z
Cross-posted at https://stackoverflow.com/questions/51082225/make-instance-with-newuser-by-terraform
Tim Malone
<amazon-ec2><terraform>
Make instance with newuser by terraform
MeetGadhiya
# Make instance with newuser by terraform

(Asked by: username_0 on 2018-06-29)

I want to make new instance with "user1" rather than ec2-user(AWS) default.i want achieve this by terraform. Like in azure terraform it can be done by "os\_profile{}", but how can i do in aws by terraform scripts?

**Tags:** amazon-ec2, terraform

> **Comments:**
> **username_1**: Cross-posted at https://stackoverflow.com/questions/51082225/make-instance-with-newuser-by-terraform  

---

## 1 answers

### Answer by username_1 on 2018-06-29. Score: 2

AWS AMIs come with a default username - which username it is depends on the specific AMI that is launched. Amazon Linux AMIs use ec2-user, CentOS uses centos, and there's other combinations too.
If you want to create your own user on instance launch, you can use EC2 User Data for this, which essentially entails passing a custom script that will be run at launch time.
The user_data argument is supported by Terraform's aws_instance resource. You can specify a script inline, but for most cases you'll probably want to use an external file or template.
For example:
create-user.sh:
#!/usr/bin/env bash

useradd mytestuser

# Anything else you want to do to set up your user account

main.tf:
resource "aws_instance" "main" {
  ...
  user_data = "${file("create-user.sh")}"
}

Alternatively, there's an example on rendering a template here, which is useful if you want to pass through custom values rather than just provide a static file.
Or, as an alternative to writing your own shell script, you could try rendering a cloudinit config file using template_cloudinit_config. Some resources that might be useful for this include this blog post and this part of the cloudinit documentation.

> **Comments:**
> **username_0**: Thanks,this is working..But i want know hoe we can pass the arguments in terraform while executing bash script>beacause i want to pass the RSA key through arguments.e.g.provisioner "local-exec" {     command = "user2.sh '${aws_key_pair.key.public_key}' "     }  
> **username_1**: I covered that in the second last para - see the link there to the example on rendering a template.  

---

