Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
aws
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
aws
Commits
da399c73
Commit
da399c73
authored
Nov 04, 2024
by
Ahmad
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
inikt
parent
1a806cf4
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
20 deletions
+32
-20
create.js
create.js
+32
-20
No files found.
create.js
View file @
da399c73
...
@@ -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
'
];
{
region
:
'
us-west-2
'
,
zone
:
'
Oregon
'
,
instanceName
:
'
Ubuntu-1
'
},
// Loop through each region and check/create instances
{
region
:
'
us-east-1
'
,
zone
:
'
Virginia
'
,
instanceName
:
'
Ubuntu-1
'
},
for
(
const
region
of
regions
)
{
{
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
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment