-
Notifications
You must be signed in to change notification settings - Fork 1
/
ec2instance.sh
executable file
·130 lines (100 loc) · 3.29 KB
/
ec2instance.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/bash
set -xe
clear
aws configure
echo
echo
echo "Welcome! This script will Launch a Ec2 instance"
echo
echo "Please enter the name of security group: "
# Ask the user for aws security group name to be
read g_name
g_id=`aws ec2 create-security-group --group-name $g_name --description "security group for $g_name development environment in EC2" | grep sg | cut -d '"' -f4`
echo
echo
echo "The security group has been created. "
echo
echo
echo "security group id is $g_id"
# Command to add open ports in Above created security group
#enable ssh
aws ec2 authorize-security-group-ingress --group-name $g_name --protocol tcp --port 22 --cidr 111.93.125.26/32
aws ec2 authorize-security-group-ingress --group-name $g_name --protocol tcp --port 22 --cidr 182.74.105.34/32
#enable HTTP
aws ec2 authorize-security-group-ingress --group-name $g_name --protocol tcp --port 80 --cidr 0.0.0.0/0
#enable HTTPS
aws ec2 authorize-security-group-ingress --group-name $g_name --protocol tcp --port 443 --cidr 0.0.0.0/0
clear
###
### Create pem file
###
echo -e "\n\n\n"
echo "Do You want create new pem file Yes/no ?"
read x
function f1()
{
read y
if [[ $y =~ ^[Yy][eE][sS]$ ]]
then
echo "Please enter the name of pem file to be created:"
read kp
aws ec2 create-key-pair --key-name $kp --query 'KeyMaterial' --output text > $kp.pem
echo -e "\n\n\n"
echo "pem file Created SuccessFully.\n "
chmod 400 $kp.pem
mv $kp.pem ~/
elif [[ $y =~ ^[nN][oO] ]]
then
echo "enter name of pem file | Just name - don't include .pem extension"
read kp
echo $kp.pem
else
echo 'Please type either "yes" or "no"'
f1
fi
}
echo # (optional) move to a new line
if [[ $x =~ ^[Yy][eE][sS]$ ]]
then
echo "Please enter the name of key-pair to be created:"
read kp
aws ec2 create-key-pair --key-name $kp --query 'KeyMaterial' --output text > $kp.pem
echo "\n\n\n"
echo "Key-pair Created SuccessFully.\n "
chmod 400 $kp.pem
mv $kp.pem ~/
elif [[ $x =~ ^[nN][oO] ]]
then
echo "enter name of pem file | Just name - don't include .pem extension"
read kp
echo $kp.pem
else
echo 'Please type either "yes" or "no"'
f1
fi
cd ~
wget https://raw.githubusercontent.com/sagespidy/shell-scripts/master/non-interactive-apache2.sh
# finally Launch instance with ubuntu
echo -e " \n\n\n\ "
echo "Please enter the type of instance (Example format : t2.small)" :
read i_type
echo "Enter the ami of Operating system you want to lauch"
read ami
# Launch ec2 with exiting sec grp and key pair
dev=`aws ec2 run-instances --image-id $ami --security-group-ids $g_id --count 1 --instance-type $i_type --key-name $kp --query 'Instances[0].InstanceId' --block-device-mappings '{"DeviceName": "/dev/sda1","Ebs": {"VolumeSize": 30}}' --user-data file://~/non-interactive-apache2.sh | cut -d '"' -f2`
#Create Name tag
aws ec2 create-tags --resources $dev --tags Key=Name,Value=ec2-dev
echo "instance created."
#Assign new Elastic Ip address
eip_dev=`aws ec2 allocate-address |grep Public | cut -d '"' -f4`
echo "Elastic ip allocated. :)"
echo "Waiting 60 seconds ..."
for ((i=60;i>=1;i--))
do
echo -e " $i \n"
sleep 1
done
# Assign Elatic ip to dev server
aws ec2 associate-address --instance-id $dev --public-ip $eip_dev
# Print details
echo "Instance id is : $dev and Elastic-IP is : $eip_dev "