Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

Usage:

@devdoshi.authorizeIpIngress({ securityGroupId: "sg-123", ip: "1.2.3.4", port: 8080, aws: { region: "us-east-2", secretAccessKey: "hunter2", accessKeyId: "ABC123", }, });
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { validators as validators2 } from "https://esm.town/v/devdoshi/validators";
export const authorizeIpIngress = async (rawInput) => {
const validators = await validators2();
const inputSchema = validators.inputSchema();
const input = inputSchema.safeParse(rawInput);
console.log(input.success);
if (input.success) {
try {
const {
EC2Client,
CreateSecurityGroupCommand,
AuthorizeSecurityGroupIngressCommand,
} = await import("https://esm.sh/@aws-sdk/client-ec2");
const { port, ip, aws, securityGroupId } = input.data;
const ec2Client = new EC2Client({
region: aws.region,
credentials: {
accessKeyId: aws.accessKeyId,
secretAccessKey: aws.secretAccessKey,
},
});
const authorizeCommand = new AuthorizeSecurityGroupIngressCommand({
GroupId: securityGroupId,
IpPermissions: [
{
FromPort: port,
ToPort: port,
IpProtocol: "tcp",
IpRanges: [{ CidrIp: `${ip}/32` }],
},
],
});
try {
await ec2Client.send(authorizeCommand);
}
catch (e) {
console.error(e);
if (!e.message.endsWith("already exists")) {
throw new Error(e.message);
}
}
return { success: true };
}
catch (e) {
console.error(e);
return { success: false };
}
}
};
October 23, 2023