【发布时间】:2021-06-08 14:52:36
【问题描述】:
如何获取 InstanceId 值? 注意 - 我在 Win10 上这样做
我试过了
jq '.Instances[] .InstanceId[]' ec2.json
但我明白了
Error - jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Windows cmd shell quoting issues?) at <top-level>, line 1:
'.Instances[]
jq: 1 compile error
数据如下:
{
"Groups": [],
"Instances": [
{
"AmiLaunchIndex": 0,
"ImageId": "ami-0c1a7f89451184c8b",
"InstanceId": "i-0144deb73abecac7c",
"InstanceType": "t2.micro",
"KeyName": "SaasPectKeyPair",
"LaunchTime": "2021-06-08T14:22:47+00:00",
"Monitoring": {
"State": "disabled"
},
"Placement": {
"AvailabilityZone": "ap-south-1b",
"GroupName": "",
"Tenancy": "default"
},
"PrivateDnsName": "ip-172-31-9-94.ap-south-1.compute.internal",
"PrivateIpAddress": "172.31.9.94",
"ProductCodes": [],
"PublicDnsName": "",
"State": {
"Code": 0,
"Name": "pending"
},
"StateTransitionReason": "",
"SubnetId": "subnet-b3134aff",
"VpcId": "vpc-210ccf4a",
"Architecture": "x86_64",
"BlockDeviceMappings": [],
"ClientToken": "7b867508-3a09-4fb5-ab54-9ef763417995",
"EbsOptimized": false,
"EnaSupport": true,
"Hypervisor": "xen",
"NetworkInterfaces": [
{
"Attachment": {
"AttachTime": "2021-06-08T14:22:47+00:00",
"AttachmentId": "eni-attach-01c465a1559542bf5",
"DeleteOnTermination": true,
"DeviceIndex": 0,
"Status": "attaching",
"NetworkCardIndex": 0
},
"Description": "",
"Groups": [
{
"GroupName": "default",
"GroupId": "sg-04472279"
}
],
"Ipv6Addresses": [],
"MacAddress": "0a:d4:9d:f4:48:a2",
"NetworkInterfaceId": "eni-0d2272b6397ad6cf3",
"OwnerId": "891999723090",
"PrivateDnsName": "ip-172-31-9-94.ap-south-1.compute.internal",
"PrivateIpAddress": "172.31.9.94",
"PrivateIpAddresses": [
{
"Primary": true,
"PrivateDnsName": "ip-172-31-9-94.ap-south-1.compute.internal",
"PrivateIpAddress": "172.31.9.94"
}
],
"SourceDestCheck": true,
"Status": "in-use",
"SubnetId": "subnet-b3134aff",
"VpcId": "vpc-210ccf4a",
"InterfaceType": "interface"
}
],
"RootDeviceName": "/dev/sda1",
"RootDeviceType": "ebs",
"SecurityGroups": [
{
"GroupName": "default",
"GroupId": "sg-04472279"
}
],
"SourceDestCheck": true,
"StateReason": {
"Code": "pending",
"Message": "pending"
},
"VirtualizationType": "hvm",
"CpuOptions": {
"CoreCount": 1,
"ThreadsPerCore": 1
},
"CapacityReservationSpecification": {
"CapacityReservationPreference": "open"
},
"MetadataOptions": {
"State": "pending",
"HttpTokens": "optional",
"HttpPutResponseHopLimit": 1,
"HttpEndpoint": "enabled"
},
"EnclaveOptions": {
"Enabled": false
}
}
],
"OwnerId": "891999723090",
"ReservationId": "r-03c1c9762da6ec46b"
}
另外,请您告诉我命令是否正确,这将非常有帮助。
#!/bin/bash
#Create EC2 instance
aws ec2 run-instances --image-id ami-0c1a7f89451184c8b --count 1 --instance-type t2.micro --region ap-south-1 --key-name KeyPair --security-group-ids sg-04472279 --subnet-id subnet-b3134aff >
ec2.json
#Create a tag for EC2 instance
aws ec2 create-tags --resources i-0626755e35fd0b29d --tags Key=Name,Value=Adam
#S3
aws s3 ls
#SSH
ssh -i "KeyPair.pem" ubuntu@ec2-13-232-137-231.ap-south-1.compute.amazonaws.com -y
#Install Apache Web Server and change Index file
sudo apt update -y
sudo apt install apache2 -y
sudo ufw allow 'Apache'
sudo systemctl restart apache2
sudo cat /var/www/html/index.html <<- _EOF_
<html>
<body>
<h1>Welcome To Index Page</h1>
</body>
</html>
_EOF_
#Stop EC2 instance
#aws ec2 stop-instances --instance-ids i-0626755e35fd0b29d
#Stat EC2 instance
#aws ec2 start-instances --instance-ids i-0626755e35fd0b29d
#Terminate EC2 instance
#aws ec2 terminate-instances --instance-ids i-0626755e35fd0b29d
【问题讨论】:
-
试试:
jq ".Instances[].InstanceId" < ec2.json。注意:您也可以使用 awscli 内置过滤,例如aws ec2 describe-instances --query "Reservations[].Instances[].InstanceId" -
您可以浏览许多类似的问题以找到您需要的答案:stackoverflow.com/q/22704831/7939871
-
谢谢@LéaGris,我试过谷歌搜索,上面显示了最好的结果,但有错误。
标签: json bash amazon-web-services jq