kl_user_333
kl_user_333

Reputation: 143

Initiate EC2 instance with pack of comands

Is there a way to start AWS EC2 instance with pack of commands? So im creating a new instance and thing i wan't to achieve is run some linux commands automatically after starting it without connecting with machine and typing those commands manually.

Upvotes: 0

Views: 45

Answers (1)

Chris Williams
Chris Williams

Reputation: 35188

This is exactly the purpose of UserData.

You would list your script (bash for Linux, or Powershell for Windows), this will then run on the first time the instance runs.

An example user data taken from the documentation to perform the setup of a web server is below.

#!/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

In the event you need to debug take a look at the /var/log/cloud-init-output.log log once the instance has launched.

However, if there are a larger number of steps it might be preferable to create a pre-baked AMI which involves setting up a blank server with all the necessary services and configuration using a tool such as Ansible, Chef or Puppet.

Upvotes: 1

Related Questions