--user-data parametresi ile EC2 Instance oluşturmak için ufak bir not. Amazon Machine Images olarak Amazon Linux kullanacağım. Id'sini buraya bırakıyorum.
Amazon Linux 2 Kernel 5.10 AMI 2.0.20220805.0 x86_64 HVM gp2 AMI ID
ami-05fa00d4c63e32376
Instance oluştururken user-data için kullanacağımı script ise şu şekilde:
#!/bin/bash
yum --help &> /dev/null
if [ $? -eq 0 ]
then
# Set Variables for CentOS
PACKAGE="httpd"
SVC="httpd"
echo "Running Setup on CentOS"
# Installing Dependencies
echo "########################################"
echo "Installing packages."
echo "########################################"
sudo yum install $PACKAGE -y > /dev/null
echo
# Start & Enable Service
echo "########################################"
echo "Start & Enable HTTPD Service"
echo "########################################"
sudo systemctl start $SVC
sudo systemctl enable $SVC
echo
echo "<h1>Created for CentOS</h1>" > /var/www/html/index.html
echo
# Bounce Service
echo "########################################"
echo "Restarting HTTPD service"
echo "########################################"
systemctl restart $SVC
echo
sudo systemctl status $SVC
ls /var/www/html/
else
# Set Variables for Ubuntu
PACKAGE="apache2"
SVC="apache2"
echo "Running Setup on CentOS"
# Installing Dependencies
echo "########################################"
echo "Installing packages."
echo "########################################"
sudo apt update
sudo apt install $PACKAGE -y > /dev/null
echo
# Start & Enable Service
echo "########################################"
echo "Start & Enable HTTPD Service"
echo "########################################"
sudo systemctl start $SVC
sudo systemctl enable $SVC
echo
echo "<h1>Created for Ubuntu</h1>" > /var/www/html/index.html
echo
# Bounce Service
echo "########################################"
echo "Restarting HTTPD service"
echo "########################################"
systemctl restart $SVC
echo
sudo systemctl status $SVC
ls /var/www/html/
fi
Script çok basit bir script olmakla beraber, multiOS bir script. Son olarak EC2 Instance oluşturmak için gerekli olan komut:
$ aws ec2 run-instances \
--image-id ami-05fa00d4c63e32376 \
--instance-type t2.micro \
--key-name blogkeypair \
--security-group-ids sg-02fa260207296790e \
--user-data file://apache_multios.sh
{
"Groups": [],
"Instances": [
{
"AmiLaunchIndex": 0,
"ImageId": "ami-05fa00d4c63e32376",
"InstanceId": "i-0dbe239d838e2c2af",
"InstanceType": "t2.micro",
"KeyName": "blogkeypair",
...
...
...
Instance oluşturduğumuza göre bir request isteğinde bulunalım.
$ aws ec2 describe-instances | grep PublicIpAddress
"PublicIpAddress": "3.88.13.40"
$ curl 3.88.13.40
<h1>Created for CentOS</h1>
CentOS için Instance kurulduğunu görüyoruz. Son olarak Instance için describe komutunu çalıştıralım ve notu sonlandıralım.
$ aws ec2 describe-instances > instances.txt
$ cat instances.txt
{
"Reservations": [
{
"Groups": [],
"Instances": [
{
"AmiLaunchIndex": 0,
"ImageId": "ami-05fa00d4c63e32376",
"InstanceId": "i-0dbe239d838e2c2af",
"InstanceType": "t2.micro",
"KeyName": "blogkeypair",
"LaunchTime": "2022-08-28T13:46:14+00:00",
"Monitoring": {
"State": "disabled"
},
"Placement": {
"AvailabilityZone": "us-east-1c",
"GroupName": "",
"Tenancy": "default"
},
"PrivateDnsName": "ip-172-31-27-116.ec2.internal",
"PrivateIpAddress": "172.31.27.116",
"ProductCodes": [],
"PublicDnsName": "ec2-3-88-13-40.compute-1.amazonaws.com",
"PublicIpAddress": "3.88.13.40",
"State": {
"Code": 16,
"Name": "running"
},
"StateTransitionReason": "",
"SubnetId": "subnet-0e28c937d93c1c2ee",
"VpcId": "vpc-0d5f861820a8489a3",
"Architecture": "x86_64",
"BlockDeviceMappings": [
{
"DeviceName": "/dev/xvda",
"Ebs": {
"AttachTime": "2022-08-28T13:46:14+00:00",
"DeleteOnTermination": true,
"Status": "attached",
"VolumeId": "vol-02a0b6b7369e1060c"
}
}
],
"ClientToken": "ab10647e-1067-466a-bbe1-bab95e3e3019",
"EbsOptimized": false,
"EnaSupport": true,
"Hypervisor": "xen",
"NetworkInterfaces": [
{
"Association": {
"IpOwnerId": "amazon",
"PublicDnsName": "ec2-3-88-13-40.compute-1.amazonaws.com",
"PublicIp": "3.88.13.40"
},
"Attachment": {
"AttachTime": "2022-08-28T13:46:14+00:00",
"AttachmentId": "eni-attach-070ab46ffcf4e303f",
"DeleteOnTermination": true,
"DeviceIndex": 0,
"Status": "attached",
"NetworkCardIndex": 0
},
"Description": "",
"Groups": [
{
"GroupName": "blog-sg",
"GroupId": "sg-02fa260207296790e"
}
],
"Ipv6Addresses": [],
"MacAddress": "0a:ae:8a:7f:27:79",
"NetworkInterfaceId": "eni-0cc5f06dd92cce3aa",
"OwnerId": "627113922449",
"PrivateDnsName": "ip-172-31-27-116.ec2.internal",
"PrivateIpAddress": "172.31.27.116",
"PrivateIpAddresses": [
{
"Association": {
"IpOwnerId": "amazon",
"PublicDnsName": "ec2-3-88-13-40.compute-1.amazonaws.com",
"PublicIp": "3.88.13.40"
},
"Primary": true,
"PrivateDnsName": "ip-172-31-27-116.ec2.internal",
"PrivateIpAddress": "172.31.27.116"
}
],
"SourceDestCheck": true,
"Status": "in-use",
"SubnetId": "subnet-0e28c937d93c1c2ee",
"VpcId": "vpc-0d5f861820a8489a3",
"InterfaceType": "interface"
}
],
"RootDeviceName": "/dev/xvda",
"RootDeviceType": "ebs",
"SecurityGroups": [
{
"GroupName": "blog-sg",
"GroupId": "sg-02fa260207296790e"
}
],
"SourceDestCheck": true,
"VirtualizationType": "hvm",
"CpuOptions": {
"CoreCount": 1,
"ThreadsPerCore": 1
},
"CapacityReservationSpecification": {
"CapacityReservationPreference": "open"
},
"HibernationOptions": {
"Configured": false
},
"MetadataOptions": {
"State": "applied",
"HttpTokens": "optional",
"HttpPutResponseHopLimit": 1,
"HttpEndpoint": "enabled",
"HttpProtocolIpv6": "disabled",
"InstanceMetadataTags": "disabled"
},
"EnclaveOptions": {
"Enabled": false
},
"PlatformDetails": "Linux/UNIX",
"UsageOperation": "RunInstances",
"UsageOperationUpdateTime": "2022-08-28T13:46:14+00:00",
"PrivateDnsNameOptions": {
"HostnameType": "ip-name",
"EnableResourceNameDnsARecord": false,
"EnableResourceNameDnsAAAARecord": false
},
"MaintenanceOptions": {
"AutoRecovery": "default"
}
}
],
"OwnerId": "627113922449",
"ReservationId": "r-0b4beef74bbf2bfe6"
}
]
}
İletişime geçmek, yorum bırakmak veya hatalarımı düzetlmek istersen mail atabilirsin.