Commit da399c73 authored by Ahmad's avatar Ahmad

inikt

parent 1a806cf4
...@@ -34,8 +34,19 @@ function extractAWSCredentials(filePath) { ...@@ -34,8 +34,19 @@ function extractAWSCredentials(filePath) {
} }
} }
// Function to check if instance exists
async function checkInstanceExists(lightsail, instanceName) {
try {
const instances = await lightsail.getInstances().promise();
return instances.instances.some(instance => instance.name === instanceName);
} catch (error) {
console.error(`Error fetching instances: ${error.message}`);
return false;
}
}
// Function to create a single Lightsail instance // Function to create a single Lightsail instance
async function createLightsailInstance(lightsail, region, zone, instanceName) { async function createLightsailInstance(lightsail, region, instanceName) {
const instanceParams = { const instanceParams = {
blueprintId: 'ubuntu_20_04', // Ubuntu 20.04 blueprintId: 'ubuntu_20_04', // Ubuntu 20.04
bundleId: 'large_2_0', // $44 USD plan for Lightsail bundleId: 'large_2_0', // $44 USD plan for Lightsail
...@@ -45,7 +56,7 @@ async function createLightsailInstance(lightsail, region, zone, instanceName) { ...@@ -45,7 +56,7 @@ async function createLightsailInstance(lightsail, region, zone, instanceName) {
try { try {
const result = await lightsail.createInstances(instanceParams).promise(); const result = await lightsail.createInstances(instanceParams).promise();
console.log(`Created instance ${instanceName} in region ${region}, zone ${zone}:`, result); console.log(`Created instance ${instanceName} in region ${region}:`, result);
} catch (error) { } catch (error) {
console.error(`Failed to create instance ${instanceName} in ${region}:`, error.message); console.error(`Failed to create instance ${instanceName} in ${region}:`, error.message);
} }
...@@ -66,27 +77,28 @@ async function main() { ...@@ -66,27 +77,28 @@ async function main() {
secretAccessKey: credentials.secretAccessKey, secretAccessKey: credentials.secretAccessKey,
}); });
// Define the regions and zones to create instances in // Define the regions and required instances
const regionsAndZones = [ const regions = ['us-west-2', 'us-east-1', 'us-east-2'];
const requiredInstances = ['Ubuntu-1', 'Ubuntu-2'];
// Loop through each region and check/create instances
for (const region of regions) {
{ region: 'us-west-2', zone: 'Oregon', instanceName: 'Ubuntu-1' },
{ region: 'us-east-1', zone: 'Virginia', instanceName: 'Ubuntu-1' },
{ region: 'us-east-2', zone: 'Ohio', instanceName: 'Ubuntu-2' },
{ region: 'us-west-2', zone: 'Oregon', instanceName: 'Ubuntu-2' },
{ region: 'us-east-1', zone: 'Virginia', instanceName: 'Ubuntu-2' },
];
// Loop through each region and create one instance at a time
for (const { region, zone, instanceName } of regionsAndZones) {
const lightsail = new AWS.Lightsail({ region }); const lightsail = new AWS.Lightsail({ region });
await createLightsailInstance(lightsail, region, zone, instanceName);
console.log(`Waiting 30 seconds before creating the next instance...`); for (const instanceName of requiredInstances) {
await sleep(60000); // Wait for 30 seconds const exists = await checkInstanceExists(lightsail, instanceName);
if (!exists) {
console.log(`Instance ${instanceName} does not exist in region ${region}, creating...`);
await createLightsailInstance(lightsail, region, instanceName);
console.log(`Waiting 60 seconds before creating the next instance...`);
await sleep(60000); // Wait for 60 seconds to avoid hitting rate limits
} else {
console.log(`Instance ${instanceName} already exists in region ${region}, skipping creation.`);
}
}
} }
await sleep(60000);
console.log('All Done') console.log('All Done');
} catch (error) { } catch (error) {
console.error('Error:', error.message); console.error('Error:', error.message);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment