Skip to content

Commit

Permalink
add get-stack-instances command
Browse files Browse the repository at this point in the history
  • Loading branch information
tavisrudd committed Aug 24, 2017
1 parent b0b7244 commit 779fb90
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
38 changes: 38 additions & 0 deletions src/cfn/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,44 @@ export async function describeStackMain(argv: Arguments): Promise<number> {
return 0;
}

export async function getStackInstancesMain(argv: Arguments): Promise<number> {
await configureAWS(argv.profile, argv.region);
const StackName = argv.stackname;
const region = getCurrentAWSRegion();

const ec2 = new aws.EC2();
const instances = await ec2.describeInstances(
{Filters:[{Name:'tag:aws:cloudformation:stack-name',
Values:[StackName]}]})
.promise();

for (const reservation of instances.Reservations || []) {
for (const instance of reservation.Instances || []) {
if (argv.short) {
console.log(instance.PublicDnsName ? instance.PublicDnsName : instance.PrivateIpAddress);
} else {
const state = instance.State ? instance.State.Name : 'unknown';
const placement = instance.Placement ? instance.Placement.AvailabilityZone : '';
console.log(sprintf(
'%-42s %-15s %s %-11s %s %s %s',
instance.PublicDnsName,
instance.PrivateIpAddress,
instance.InstanceId,
instance.InstanceType,
state,
placement,
formatTimestamp(renderTimestamp(instance.LaunchTime as Date))
));
}
}
}

console.log(
cli.blackBright(
`https://console.aws.amazon.com/ec2/v2/home?region=${region}#Instances:tag:aws:cloudformation:stack-name=${StackName};sort=desc:launchTime`));
return 0;
}

export async function getStackTemplateMain(argv: Arguments): Promise<number> {
await configureAWS(argv.profile, argv.region);

Expand Down
15 changes: 14 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ interface Commands {
watchStackMain: Handler
describeStackMain: Handler
getStackTemplateMain: Handler
getStackInstancesMain: Handler
deleteStackMain: Handler

estimateCost: Handler
Expand Down Expand Up @@ -84,6 +85,7 @@ const lazy: Commands = {
watchStackMain: lazyLoad('watchStackMain'),
describeStackMain: lazyLoad('describeStackMain'),
getStackTemplateMain: lazyLoad('getStackTemplateMain'),
getStackInstancesMain: lazyLoad('getStackInstancesMain'),
deleteStackMain: lazyLoad('deleteStackMain'),

createUpdateChangesetMain: lazyLoad('createUpdateChangesetMain'),
Expand Down Expand Up @@ -143,7 +145,7 @@ export async function main() {
wrapMainHandler(commands.estimateCost))

.command('\t', '') // fake command to add a line-break to the help output

.command(
'create-changeset <changesetName> <argsfile>',
description('create a cfn changeset based on stack-args.yaml'),
Expand Down Expand Up @@ -226,6 +228,17 @@ export async function main() {
.usage('Usage: iidy get-stack-template <stackname>'),
wrapMainHandler(commands.getStackTemplateMain))

.command(
'get-stack-instances <stackname>',
description('list the ec2 instances of a live stack'),
(args) => args
.demandCommand(0, 0)
.option('short', {
type: 'boolean', default: false,
description: 'Show only instance dns names'})
.usage('Usage: iidy get-stack-instances <stackname>'),
wrapMainHandler(commands.getStackInstancesMain))

.command(
'list-stacks',
description('list all stacks within a region'),
Expand Down

0 comments on commit 779fb90

Please sign in to comment.