Skip to content

Commit 731de41

Browse files
committed
feat: show instance states and launch dates, only show pending/running instances
1 parent d0c17f7 commit 731de41

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

index.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ async function getAmiName(ec2, ImageId) {
1919
if (!name) throw new Error(`failed to get name for image: ${ImageId}`)
2020
return name
2121
}
22-
2322
function getUser(ami) {
2423
if (/ubuntu|nanostack/i.test(ami)) return 'ubuntu'
2524
if (/debian/i.test(ami)) return 'admin'
@@ -56,15 +55,41 @@ function signalCode(signal) {
5655
return 0
5756
}
5857

59-
async function ec2ssh({ ec2 = new AWS.EC2() } = {}) {
60-
const instance = await selectEC2Instance({ ec2 })
58+
async function ec2ssh({ ec2 = new AWS.EC2(), ssm = new AWS.SSM() } = {}) {
59+
const instance = await selectEC2Instance({
60+
ec2,
61+
Filters: [
62+
{
63+
Name: 'instance-state-name',
64+
Values: ['pending', 'running'],
65+
},
66+
],
67+
})
6168

62-
const { ImageId, KeyName, PrivateDnsName, PublicDnsName } = instance
69+
const {
70+
InstanceId,
71+
ImageId,
72+
KeyName,
73+
PrivateDnsName,
74+
PublicDnsName,
75+
} = instance
6376
let host = PrivateDnsName || PublicDnsName
6477
if (!host)
6578
throw new Error(`instance doesn't have a PrivateDnsName or PublicDnsName`)
6679

67-
const user = ImageId ? getUser(await getAmiName(ec2, ImageId)) : null
80+
let user = ImageId ? getUser(await getAmiName(ec2, ImageId)) : null
81+
82+
if (InstanceId) {
83+
const { InstanceInformationList: [{ PlatformName } = {}] = [] } = await ssm
84+
.describeInstanceInformation({
85+
Filters: [{ Key: 'InstanceIds', Values: [InstanceId] }],
86+
})
87+
.promise()
88+
if (PlatformName) {
89+
const userFromPlatform = getUser(PlatformName)
90+
if (userFromPlatform !== 'ec2-user') user = userFromPlatform
91+
}
92+
}
6893

6994
if (user) host = `${user}@${host}`
7095

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
"semantic-release": "^17.1.2"
9595
},
9696
"dependencies": {
97-
"@jcoreio/aws-select-cli-prompts": "^1.0.1",
97+
"@jcoreio/aws-select-cli-prompts": "^1.1.0",
9898
"aws-sdk": "^2.829.0"
9999
}
100100
}

yarn.lock

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,14 @@
198198
update-notifier "^2.2.0"
199199
yargs "^8.0.2"
200200

201-
"@jcoreio/aws-select-cli-prompts@^1.0.1":
202-
version "1.0.1"
203-
resolved "https://registry.npmjs.org/@jcoreio/aws-select-cli-prompts/-/aws-select-cli-prompts-1.0.1.tgz#a5b92055e59ba74758b01968c7e17605cf67ff9b"
204-
integrity sha512-ycean9jyEOP6awi0Ay4XmOmp9QNjkUpsu7Cbg/iXI9t6JE+wPA7+9ze+ZyyCN6ic1y5iFLefbKWCwqGi/IAHGg==
201+
"@jcoreio/aws-select-cli-prompts@^1.1.0":
202+
version "1.1.0"
203+
resolved "https://registry.yarnpkg.com/@jcoreio/aws-select-cli-prompts/-/aws-select-cli-prompts-1.1.0.tgz#43a70afd021335977275ea2c1d60a0bf5f0f6deb"
204+
integrity sha512-iSxhElIPfUTFK0QS72HQlgeN+OeHW5eTrFCwt8/0YNsjj5lFv+gPGKbUbyNa5XGj0dh3cVOGp+tAlG8WEpRMeA==
205205
dependencies:
206206
"@babel/runtime" "^7.1.5"
207207
async-autocomplete-cli "^1.0.0"
208+
chalk "^4.0.0"
208209
fs-extra "^9.1.0"
209210

210211
"@jedwards1211/commitlint-config@^1.0.1":

0 commit comments

Comments
 (0)