-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-img.sh
120 lines (105 loc) · 5.11 KB
/
create-img.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
#!/bin/bash
vpccidr=10.1.0.0/16
subnetcidr=10.1.1.0/24
instancetype="t3.micro"
userdat="user-data.txt"
aminame="AWSLinux2HttpEnabledJUWAMI"
amidesc="AWS Linux 2 HTTP Enabled JUWi AMI"
waitint=15
AWS_DEFAULT_REGION=eu-west-1
export AWS_DEFAULT_REGION
echo "Creating VPC with CID $vpccidr"
vpcid=$(aws ec2 create-vpc --cidr-block $vpccidr --query 'Vpc.VpcId' --output text)
echo "VPC ID: $vpcid"
echo "Creating subnet in VPC $vpcid with CID $subnetcidr"
subnetid=$(aws ec2 create-subnet --vpc-id $vpcid --cidr-block $subnetcidr --query 'Subnet.SubnetId' --output text)
echo "Subnet ID: $subnetid"
echo "Creating internet gateway"
igatewayid=$(aws ec2 create-internet-gateway --query 'InternetGateway.InternetGatewayId' --output text)
echo "Internet gateway ID: $igatewayid"
echo "Attaching internet gateway $igatewayid to VPC $vpcid"
aws ec2 attach-internet-gateway --vpc-id $vpcid --internet-gateway-id $igatewayid
echo "Creating route table in VPC $vpcid"
routetableid=$(aws ec2 create-route-table --vpc-id $vpcid --query 'RouteTable.RouteTableId' --output text)
echo "Route table ID: $routetableid"
echo "Creating default route to internet gateway $igatewayid in route table $routetableid"
aws ec2 create-route --route-table-id $routetableid --destination-cidr-block 0.0.0.0/0 --gateway-id $igatewayid > /dev/null
echo "Describe route table $routetableid"
aws ec2 describe-route-tables --route-table-id $routetableid
echo "Associate route table $routetableid with subnet $subnetid"
aws ec2 associate-route-table --subnet-id $subnetid --route-table-id $routetableid > /dev/null
echo "Set attribute \"map-public-ip-on-launch\" for subnet $subnetid"
aws ec2 modify-subnet-attribute --subnet-id $subnetid --map-public-ip-on-launch
echo "Create security group with SSH and HTTP access from 0.0.0.0/0"
secgroupid=$(aws ec2 create-security-group --group-name SSHHttpAccess --description "Security group for SSH and HTTP access" --vpc-id $vpcid --query 'GroupId' --output text)
echo "Security group ID: $secgroupid"
aws ec2 authorize-security-group-ingress --group-id $secgroupid --protocol tcp --port 22 --cidr 0.0.0.0/0
aws ec2 authorize-security-group-ingress --group-id $secgroupid --protocol tcp --port 80 --cidr 0.0.0.0/0
echo "Describe security group ID $secgroupid"
aws ec2 describe-security-groups --group-id $secgroupid
echo "Creating user data input file $userdat"
cat <<EOF > $userdat
#!/bin/bash
yum update -y
yum install -y httpd
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 {} \;
mv /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.conf_backup
systemctl restart httpd
curl https://gist.githubusercontent.com/youwalther65/e8788a80f03eac2e81d40421e4f21f7f/raw/380339de4bb0c05220a5e878e14ad38a47cf1081/index.html -o /var/www/html/index.html
curl https://aws.amazon.com/favicon.ico -o /var/www/html/favicon.ico -o /var/www/html/favicon.ico
EOF
echo "Determing latest AWS Linux 2 image in region"
amiid=$(aws ssm get-parameters --names /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2 --query 'Parameters[].Value' --output text)
echo "AMI ID: $amiid"
echo "Creating EC2 instance from AMI $amiid"
instanceid=$(aws ec2 run-instances --image-id $amiid --count 1 --instance-type $instancetype \
--subnet-id $subnetid --security-group-ids $secgroupid \
--user-data file://${userdat} --query 'Instances[].InstanceId' --output text)
echo "Instance ID: $instanceid"
echo "Wating for instance $instanceid to become available"
while [ `aws ec2 describe-instance-status --instance-ids $instanceid --query 'InstanceStatuses[].[SystemStatus.Status,InstanceStatus.Status]' --output json | egrep -c ok` -ne 2 ]
do
printf "."
sleep $waitint
done
pubip=$(aws ec2 describe-instances --instance-id $instanceid --query 'Reservations[].Instances[].PublicIpAddress' --output text)
echo "Public IP address: $pubip"
echo "Wating for Apache web server to become ready"
until $(curl -vs $pubip > /dev/null 2>&1)
do
printf "."
sleep $waitint
done
echo ""
echo "Fetching web page from instance $instanceid"
curl $pubip
echo "Stopping instance $instanceid to get create clean snapshot and AMI"
aws ec2 stop-instances --instance-id $instanceid
echo "Wating for $instanceid to be \"stopped\""
while [ `aws ec2 describe-instances --instance-ids $instanceid --query 'Reservations[].Instances[].State.Name' --output text` != "stopped" ]
do
printf "."
sleep $waitint
done
echo ""
echo "$instanceid is \"stopped\""
echo "Taking snapshot from stopped instance $instanceid"
newamiid=$(aws ec2 create-image --instance-id $instanceid --name $aminame --description "$amidesc" --query 'ImageId' --output text)
echo "AMI ID: $newamiid"
echo "Wating for $newamiid to be \"available\""
while [ `aws ec2 describe-images --image-ids $newamiid --query 'Images[].State' --output text` != "available" ]
do
printf "."
sleep $waitint
done
echo ""
echo "AMI $newamiid is \"available\""
echo "Making AMI $newamiid public"
aws ec2 modify-image-attribute --image-id $newamiid --launch-permission "Add=[{Group=all}]"
aws ec2 describe-images --image-id $newamiid