Skip to content

Commit

Permalink
Add listing for latest ubuntu 20.04 image
Browse files Browse the repository at this point in the history
  • Loading branch information
AHarmlessPyro committed Aug 6, 2022
1 parent fdac515 commit ee77fe1
Show file tree
Hide file tree
Showing 3 changed files with 1,126 additions and 30 deletions.
5 changes: 4 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@
"dev-jobs": "nodemon src/jobs.ts",
"start": "node dist/src/index.js",
"start-jobs": "node dist/src/jobs.js",
"format": "prettier --write './src/**/*.{ts,tsx}'"
"format": "prettier --write './src/**/*.{ts,tsx}'",
"rnd": "nodemon src/aws-services/create-ec2-instance.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@aws-sdk/client-ec2": "^3.142.0",
"aws-sdk": "^2.1189.0",
"body-parser": "^1.20.0",
"dotenv": "^16.0.1",
"express": "^4.18.1",
Expand Down
51 changes: 51 additions & 0 deletions backend/src/aws-services/create-ec2-instance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {
EC2Client,
DescribeImagesCommand,
DescribeImagesCommandInput,
DescribeImagesCommandOutput,
Filter,
EC2ClientConfig,
} from "@aws-sdk/client-ec2";
import { Config } from "aws-sdk";
import { response } from "express";
// Set the AWS Region.
// TODO : Get info from params

async function get_all_images(
img_names: Array<string>,
config: EC2ClientConfig
): Promise<DescribeImagesCommandOutput["Images"]> {
// Create anAmazon EC2 service client object.
const ec2Client = new EC2Client(config);
const input: DescribeImagesCommandInput = {
Filters: [
{ Name: "architecture", Values: ["x86_64"] },
{ Name: "is-public", Values: ["true"] },
{ Name: "image-type", Values: ["machine"] },
{ Name: "state", Values: ["available"] },
{
Name: "name",
Values: img_names,
},
],
Owners: ["099720109477"],
IncludeDeprecated: false,
};
const command = new DescribeImagesCommand(input);
const response = await ec2Client.send(command);
return response.Images.sort(
(a, b) =>
new Date(a.CreationDate).getTime() - new Date(b.CreationDate).getTime()
);
}

async function get_latest_image(
config: EC2ClientConfig,
img_names: Array<string> = [
"ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-????????",
]
) {
let resp = (await get_all_images(img_names, config)).pop();
console.log(resp);
return resp;
}
Loading

0 comments on commit ee77fe1

Please sign in to comment.