Have you ever thought about configuring your servers before you even log in? If not, this blog will definitely help you out. Here we go !
When you launch an instance in Amazon EC2, you have the option of passing user data to the instance that can be used to perform common automated configuration tasks and even run scripts after the instance starts. Two types of userdata can be passed to EC2 instances : shell scripts and cloud-init directives.
Userdata : Userdata is the data supplied while launching your instance.For example, if you run web servers for various small businesses, they can all use the same AMI and retrieve their content from the Amazon S3 bucket you specify in the user data at launch.User data is executed only at launch. If you stop an instance, modify the user data, and start the instance, the new user data is not executed automatically. There are two types of userdata that can be supplied :-
Shell scripts : These are simple bash script that contains a bunch of commands to be executed in sequential manner.
Cloud-init : It is the program that pulls the metadata about the server’s configuration and environment, such as network addresses, hostname etc to help configure essential services. Amazon Linux contains a customized version of cloud-init. It enables you to specify actions that should happen to your instance at boot time.
Here, we are taking the example of passing userdata as a shell script.
In the advance details panel while launching your instance, there is an option of passing userdata to EC2 instance.You can pass userdata either as text or as file :-
We have passed userdata which is simple shell script to install LAMP server on corresponding instance, so that we don’t have to manually configure lamp on each and every instance we launch. You can either directly pass this script as a text or upload it as a file depending on your choice for it won’t make any difference.
1 2 3 4 5 6 |
#!/bin/bash DEBIAN_FRONTEND=noninteractive apt-get -y update DEBIAN_FRONTEND=noninteractive apt-get -y install lamp-server^ DEBIAN_FRONTEND=noninteractive sudo apt-get -y install mcrypt php5-mcrypt curl php5-curl php5-gd php5enmod mcrypt service apache2 restart echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php |
After the instance has been successfully launched, we have to check if these command were executed or not. For this, copy the public dns of your instance and hit it on browser to check if apache is installed :-
For checking php :-
Hence, you can bootstap your EC2 instance and configure it even before it is successfully launched.Hope this helps !