Skip to content

Commit

Permalink
Merge pull request #116 from depot/fix/multiple-instances-created
Browse files Browse the repository at this point in the history
fix: prevent creating duplicate machine ids
  • Loading branch information
goller authored Nov 5, 2024
2 parents d53fbf1 + ffc5a68 commit 5ef57c3
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/utils/aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
TerminateInstancesCommand,
Volume,
paginateDescribeInstances,
waitUntilInstanceExists,
} from '@aws-sdk/client-ec2'
import {
GetDesiredStateResponse,
Expand Down Expand Up @@ -306,7 +307,7 @@ systemctl start machine-agent.service
}

console.log(`Creating new instance for machine ID ${machine.id}`)
await client.send(
const instance = await client.send(
new RunInstancesCommand({
LaunchTemplate: {
LaunchTemplateId:
Expand Down Expand Up @@ -339,6 +340,27 @@ systemctl start machine-agent.service
UserData: Buffer.from(userData).toString('base64'),
}),
)

if (!instance.Instances || instance.Instances.length === 0 || !instance.Instances[0].InstanceId) {
// TODO: Will this happen?
// If this does happen you'll need to run a different kind of description
// with a filter on the tag machine id.
console.log(`No instances created for machine ID ${machine.id}`)
return
}

const instanceID = instance.Instances[0].InstanceId

const MAX_WAIT = 30
try {
await waitUntilInstanceExists({client, maxWaitTime: MAX_WAIT}, {InstanceIds: [instanceID]})
} catch (caught) {
if (caught instanceof Error && caught.name === 'TimeoutError') {
console.log(`instance ${instanceID} did not exist after ${MAX_WAIT} seconds`)
} else {
console.log(`Error waiting for instance ${instanceID} to exist`, caught)
}
}
}

function currentMachineState(instance: Instance): GetDesiredStateResponse_MachineState {
Expand Down

0 comments on commit 5ef57c3

Please sign in to comment.