AWS EC2 contd…
Options for deploying applications into ec2 instance
- Lets try to install php on apache server (ubuntu)
#!/bin/bash
apt update
apt install apache2 stress -y
apt install php libapache2-mod-php php-mysql -y
echo '<?php phpinfo(); ?>' > /var/www/html/info.php
systemctl restart apache2
- Options:
- Create an ec2 instance, ssh and execute manually:
- Create an ec2
- ssh into ec2
- become root user (sudo -i)
- While creating an ec2 instance use user data
- Create a reusable image (Amazon Machine Image)
- Create an ec2 instance, ssh and execute manually:
Instance MetaData Service v1
- Login into the ec2 instance
- Instance Metadata service helps in fetching metadata about ec2 instnaces by sending http requests on
169.254.169.254
- send a curl request
curl http://169.254.169.254/latest/meta-data/
- Refer Here for instance metadata categories
- Userdata configured on ec2 can be accessed over
http://169.254.169.254/latest/user-data
User Data
- In the Advanced section of ec2 instance creation for ubuntu 22 with 80 port opened copy the script into user data and launch instance
- User Data is the script that gets executed during ec2 creation which is also referred as provisioning.
- Accesing the application over
http://<publicip>/info.php
should give php info page.
Creating an AMI
- AMI id is used to create ec2 instances which is scoped to a Region.
-
If we want AMI in multiple regions, we need to copy and this leads to new AMI id.
-
Note:
- Learn JSON Refer Here