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
405f5e44
Commit
405f5e44
authored
Sep 09, 2024
by
Ahmad
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
inikt
parent
0c742cc2
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
101 additions
and
0 deletions
+101
-0
create.bat
create.bat
+7
-0
create.js
create.js
+94
-0
No files found.
create.bat
0 → 100644
View file @
405f5e44
@echo off
title Running Node.js Create
echo Starting Node.js Create...
cd /d "%~dp0" // Ensures the script runs from the directory where the batch file is located
node create.js // Runs your Node.js application
pause // Keeps the command prompt open after the script finishes
create.js
0 → 100644
View file @
405f5e44
const
fs
=
require
(
'
fs
'
);
const
path
=
require
(
'
path
'
);
const
AWS
=
require
(
'
aws-sdk
'
);
const
{
Client
}
=
require
(
'
ssh2
'
);
const
axios
=
require
(
'
axios
'
);
const
downloadsPath
=
path
.
join
(
process
.
env
.
HOME
||
process
.
env
.
USERPROFILE
,
'
Downloads
'
);
const
rootKeyFilePath
=
path
.
join
(
downloadsPath
,
'
rootkey.csv
'
);
// Check if the rootkey.csv file exists
if
(
!
fs
.
existsSync
(
rootKeyFilePath
))
{
console
.
error
(
`File not found:
${
rootKeyFilePath
}
`
);
process
.
exit
(
1
);
}
// Function to read the second line and extract AWS credentials
function
extractAWSCredentials
(
filePath
)
{
const
fileContent
=
fs
.
readFileSync
(
filePath
,
'
utf8
'
);
const
lines
=
fileContent
.
split
(
'
\n
'
);
if
(
lines
.
length
<
2
)
{
throw
new
Error
(
'
The rootkey.csv file does not contain enough lines.
'
);
}
const
secondLine
=
lines
[
1
].
trim
();
const
[
accessKeyId
,
secretAccessKey
]
=
secondLine
.
split
(
'
,
'
);
if
(
accessKeyId
&&
secretAccessKey
)
{
return
{
accessKeyId
:
accessKeyId
.
trim
(),
secretAccessKey
:
secretAccessKey
.
trim
(),
};
}
else
{
throw
new
Error
(
'
Failed to extract AWS credentials from the second line of rootkey.csv
'
);
}
}
// Function to create Lightsail instances
async
function
createLightsailInstances
(
lightsail
,
region
,
zone
,
count
)
{
const
instanceParams
=
{
blueprintId
:
'
ubuntu_20_04
'
,
// Ubuntu 20.04
bundleId
:
'
large_2_0
'
,
// $44 USD plan for Lightsail
availabilityZone
:
`
${
region
}
a`
,
// Availability zone (usually 'a', 'b', or 'c')
instanceNames
:
[],
// Will be populated below
};
// Create instances with names Ubuntu-1, Ubuntu-2, etc.
for
(
let
i
=
1
;
i
<=
count
;
i
++
)
{
instanceParams
.
instanceNames
.
push
(
`Ubuntu-
${
i
}
`
);
}
try
{
const
result
=
await
lightsail
.
createInstances
(
instanceParams
).
promise
();
console
.
log
(
`Created
${
count
}
instances in region
${
region
}
, zone
${
zone
}
:`
,
result
);
}
catch
(
error
)
{
console
.
error
(
`Failed to create instances in
${
region
}
:`
,
error
.
message
);
}
}
// Function to add delay
const
sleep
=
(
ms
)
=>
new
Promise
((
resolve
)
=>
setTimeout
(
resolve
,
ms
));
async
function
main
()
{
try
{
// Extract credentials
const
credentials
=
extractAWSCredentials
(
rootKeyFilePath
);
console
.
log
(
'
AWS Credentials Loaded:
'
,
credentials
);
// Set AWS config globally
AWS
.
config
.
update
({
accessKeyId
:
credentials
.
accessKeyId
,
secretAccessKey
:
credentials
.
secretAccessKey
,
});
// Define the regions and zones to create instances in
const
regionsAndZones
=
[
{
region
:
'
us-west-2
'
,
zone
:
'
Oregon
'
,
count
:
2
},
{
region
:
'
us-east-2
'
,
zone
:
'
Ohio
'
,
count
:
2
},
{
region
:
'
us-east-1
'
,
zone
:
'
Virginia
'
,
count
:
2
},
];
// Loop through each region and zone to create instances
for
(
const
{
region
,
zone
,
count
}
of
regionsAndZones
)
{
const
lightsail
=
new
AWS
.
Lightsail
({
region
});
await
createLightsailInstances
(
lightsail
,
region
,
zone
,
count
);
console
.
log
(
`Waiting 30 seconds before creating instances in the next region...`
);
await
sleep
(
30000
);
// Wait for 30 seconds
}
}
catch
(
error
)
{
console
.
error
(
'
Error:
'
,
error
.
message
);
}
}
main
().
catch
(
console
.
error
);
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