User Data

The “user_data” attribute allows you to add bootstrap script/commands to an EC2 instance.

By default, user data scripts run only during the first boot cycle when an instance is launched.

However, you can configure your user data scripts to run every time the instance is restarted from a stopped state.

In each example, the following tasks are executed by the user data:

  • Distribution software packages are updated.
  • Necessary web server, php and mariadb packages are installed.
  • Started the httpd service and turned on via systemctl.
  • The ec2-user is added to the apache group.
  • The appropriate ownership and file permissions are set for the web directory and the files contained within it.
  • A simple web page is created to test the web server and php engine.

User Data : 

#!/bin/bash
yum update -y
amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2
yum install -y httpd mariadb-server
systemctl start httpd
systemctl enable httpd
usermod -a -G apache ec2-user
chown -R ec2-user:apache /var/www
chmod 2775 /var/www
find /var/www -type d -exec chmod 2775 {} \; 
find /var/www -type f -exec chmod 0664 {} \;
echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php

User data shell scripts must start with the #! characters and the path to the interpreter you want to read the script (commonly /bin/bash).

Scripts entered as user data are executed as the root user, so do not use the sudo command in the script.

Terraform configuration file :

provider "aws" {
  region ="us-east-1"
}

resource "aws_instance" "webserver" {
  ami ="ami-009d6802948d06e52"
  instance_type ="t2.micro"
  key_name = "baston-key"
  associate_public_ip_address = true
  root_block_device {
    volume_type = "gp2"
    volume_size = "30"
    delete_on_termination = false
  }
 
  user_data = <<EOF
#!/bin/bash
yum update -y
amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2
yum install -y httpd mariadb-server
systemctl start httpd
systemctl enable httpd
usermod -a -G apache ec2-user
chown -R ec2-user:apache /var/www
chmod 2775 /var/www
find /var/www -type d -exec chmod 2775 {} \; 
find /var/www -type f -exec chmod 0664 {} \;
echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php
EOF
  
  tags = {
    Name = "webserver"
  }
}

output "IPAddress" {
  value = "${aws_instance.webserver.public_ip}"
}

You can also add user data to the instance configuration using template files.

Create a file with .tpl extension and add the script to the file

Template configuration :

data "template_file" "userdata" {
  template = "${file("userdata.tpl")}"
}

 Usage in the instance configuration :

provider "aws" {
  region ="us-east-1"
}

data "template_file" "userdata" {
  template = "${file("userdata.tpl")}"
}

resource "aws_instance" "webserver" {
  ami ="ami-009d6802948d06e52"
  instance_type ="t2.micro"
  key_name = "baston-key"
  associate_public_ip_address = true
  root_block_device {
    volume_type = "gp2"
    volume_size = "30"
    delete_on_termination = false
  }
  user_data = "${data.template_file.userdata.rendered}"
  
  tags = {
    Name = "webserver"
  }
}

output "IPAddress" {
  value = "${aws_instance.webserver.public_ip}"
}
$ tree
.
├── instance.tf
├── terraform.tfstate
├── terraform.tfstate.backup
└── userdata.tpl

0 directories, 4 files