AWS CLI를 이용한 EC2 create & terminate 예시
2020-10-06
.
$ aws ec2 run-instances --image-id ami-03b42693dc6a7dc35 --count 1 --instance-type t3.small --key-name pms-seoul-key --security-group-ids sg-xxxxxxxxxxxx --subnet-id subnet-xxxxxxxxxxxxx --associate-public-ip-address --user-data '#!/bin/bash -xe
exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1
yum -y update' --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=pms-airflow-test},{Key=owner,Value=pms},{Key=expiry-date,Value=2020-10-07}]' --block-device-mappings 'DeviceName=/dev/xvda,Ebs={VolumeSize=30,DeleteOnTermination=true}'
{
"Groups": [],
"Instances": [
{
"AmiLaunchIndex": 0,
"ImageId": "ami-03b42693dc6a7dc35",
"InstanceId": "i-014f19da3d8d83f74",
"InstanceType": "t3.small",
"KeyName": "xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"LaunchTime": "2020-10-07T01:51:19.000Z",
"Monitoring": {
"State": "disabled"
},
"Placement": {
"AvailabilityZone": "ap-northeast-2a",
"GroupName": "",
"Tenancy": "default"
},
"PrivateDnsName": "ip-10-1-10-8.ap-northeast-2.compute.internal",
"PrivateIpAddress": "10.1.10.8",
"ProductCodes": [],
"PublicDnsName": "",
"State": {
"Code": 0,
"Name": "pending"
},
"StateTransitionReason": "",
"SubnetId": "subnet-xxxxxxxxxxxxxxxxxxxxx",
"VpcId": "vpc-xxxxxxxxxxxxxxxxxxxxxxxx",
"Architecture": "x86_64",
"BlockDeviceMappings": [],
"ClientToken": "",
"EbsOptimized": false,
"Hypervisor": "xen",
"NetworkInterfaces": [
{
"Attachment": {
"AttachTime": "2020-10-07T01:51:19.000Z",
"AttachmentId": "eni-attach-xxxxxxxxxxxxxxxxxxx",
"DeleteOnTermination": true,
"DeviceIndex": 0,
"Status": "attaching"
},
"Description": "",
"Groups": [
{
"GroupName": "default",
"GroupId": "sg-xxxxxxxxxxxxxxx"
}
],
"Ipv6Addresses": [],
"MacAddress": "xxxxxxxxxxxxxxxxxxxxxxxxxxx",
"NetworkInterfaceId": "eni-xxxxxxxxxxxxxxxxxxx",
"OwnerId": "xxxxxxxxxxxxxxxxxxxxxx",
"PrivateDnsName": "ip-10-1-10-8.ap-northeast-2.compute.internal",
"PrivateIpAddress": "10.1.10.8",
"PrivateIpAddresses": [
{
"Primary": true,
"PrivateDnsName": "ip-10-1-10-8.ap-northeast-2.compute.internal",
"PrivateIpAddress": "10.1.10.8"
}
],
"SourceDestCheck": true,
"Status": "in-use",
"SubnetId": "subnet-xxxxxxxxxxxxxxxxxxxx",
"VpcId": "vpc-xxxxxxxxxxxxxxxx",
"InterfaceType": "interface"
}
],
"RootDeviceName": "/dev/xvda",
"RootDeviceType": "ebs",
"SecurityGroups": [
{
"GroupName": "default",
"GroupId": "sg-xxxxxxxxxxxxxxx"
}
],
"SourceDestCheck": true,
"StateReason": {
"Code": "pending",
"Message": "pending"
},
"Tags": [
{
"Key": "Name",
"Value": "xxxxxxxxxxxxxxxxxxx"
},
{
"Key": "owner",
"Value": "pms"
},
{
"Key": "expiry-date",
"Value": "2020-10-07"
}
],
"VirtualizationType": "hvm",
"CpuOptions": {
"CoreCount": 1,
"ThreadsPerCore": 2
},
"CapacityReservationSpecification": {
"CapacityReservationPreference": "open"
},
"MetadataOptions": {
"State": "pending",
"HttpTokens": "optional",
"HttpPutResponseHopLimit": 1,
"HttpEndpoint": "enabled"
}
}
],
"OwnerId": "xxxxxxxxxxxxxx",
"ReservationId": "r-xxxxxxxxxxxxxxxxxxxx"
}
## 해당 ec2에 접속해서 'cat /var/log/user-data.log' 명령어를 실행하면 부트스트랩으로 실행한 내역들을 로그로 확인할 수 있을 것이다.
## 부트스르탭 옵션관련 참고자료 : https://aws.amazon.com/ko/premiumsupport/knowledge-center/ec2-linux-log-user-data/
$ aws ec2 terminate-instances --instance-ids i-014f19da3d8d83f74
{
"TerminatingInstances": [
{
"CurrentState": {
"Code": 32,
"Name": "shutting-down"
},
"InstanceId": "i-014f19da3d8d83f74",
"PreviousState": {
"Code": 16,
"Name": "running"
}
}
]
}