Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
AWS_REGION=your_region_here
AWS_REGION=us-east-1
60 changes: 54 additions & 6 deletions scripts/03/create-ec2-instance.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
// Imports
// TODO: Import the ec2 client
const {
EC2Client,
AuthorizeSecurityGroupIngressCommand,
createKeyPairCommand,
CreateSecurityGroupCommand,
RunInstancesCommand
} = require('@aws-sdk/client-ec2')
const helpers = require('./helpers')

function sendCommand (command) {
// TODO: Create new client with region
// TODO: Return send command
const client = new EC2Client({ region: process.env.AWS_REGION})
return client.send(command)
}

// Declare local variables
Expand All @@ -26,13 +32,55 @@ async function execute () {

// Create functions
async function createSecurityGroup (sgName) {
// TODO: Implement sg creation & setting SSH rule
const sgParams = {
Description: sgName,
GroupName: sgName
}
const createCommand = new CreateSecurityGroupCommand(sgParams)
const data = await sendCommand(createCommand)

const rulesParams = {
GroupId: data.GroupId,
IpPermissions: [
{
IpProtocol: 'tcp',
FromPort: 22,
ToPort: 22,
IpRanges: [{ CidrIp: '0.0.0.0/0' }]
},
{
IpProtocol: 'tcp',
FromPort: 3000,
ToPort: 3000,
IpRanges: [{ CidrIp: '0.0.0.0/0' }]
}
]
}
const authCommand = new AuthorizeSecurityGroupIngressCommand(rulesParams)
return sendCommand(authCommand)
}

async function createKeyPair (keyName) {
// TODO: Create keypair
const params = {
KeyName: keyName
}
const command = new CreateKeyPairCommand(params)
return sendCommand(command)

}

async function createInstance (sgName, keyName) {
// TODO: create ec2 instance
const params = {
ImageId: 'ami-0d7a109bf30624c99',
InstanceType: 't2.micro',
KeyName: keyName,
MaxCount: 1,
MinCount: 1,
SecurityGroups: [ sgName ],
UserData: 'IyEvYmluL2Jhc2gKY3VybCAtLXNpbGVudCAtLWxvY2F0aW9uIGh0dHBzOi8vcnBtLm5vZGVzb3VyY2UuY29tL3NldHVwXzE2LnggfCBzdWRvIGJhc2ggLQpzdWRvIHl1bSBpbnN0YWxsIC15IG5vZGVqcwpzdWRvIHl1bSBpbnN0YWxsIC15IGdpdApjZCBob21lL2VjMi11c2VyCmdpdCBjbG9uZSBodHRwczovL2dpdGh1Yi5jb20vcnlhbm11cmFrYW1pL2hiZmwuZ2l0CmNkIGhiZmwKbnBtIGkKbnBtIHJ1biBzdGFydA=='
}
const command = new RunInstancesCommand(params)
return sendCommand(command)
}

execute()