Saturday 8 April 2017

Node JS AWS Security Group API Creation

I'm writing a script in NodeJS that will create security groups for some of my EC2 instances (or maybe for the VPC). I have the following script written so far:

'use strict';

const aws = require('aws-sdk');

var ec2 = new AWS.EC2({apiVersion: '2016-11-15'});

AWS.config.loadFromPath(..'/.json');

module.exports = {
//Exports creation of Security Groups
    CreateSecurityGroup: (req, res) => {
    ec2.createSecurityGroup(paramsSecurityGroup, function(err, data) {
        if (err) {
            console.log("Error", err);
        } else {
            var params = {
                GroupId: ,
                CidrIp: ,
                DryRun: true || false,
                FromPort: 0,
                IpPermissions: [{
                    IpProtocol: "tcp",
                    FromPort: 80,
                    ToPort: 80,
                    IpRanges: [{"CidrIp":"0.0.0.0/0"}]
                },
                {
                    IpProtocol: "tcp",
                    FromPort: 80,
                    ToPort: 80,
                    IpRanges: [{"CidrIp":"0.0.0.0/0"}]
                }],
                IpProtocol: ,
                SourceSecurityGroupName: ,
                SourceSecurityGroupOwnerId: ,
                ToPort: 0
            }
        };
        ec2.authorizeSecurityGroupEgress(params, function (err, data) {
            if (err) {
                res.serverError(err, err.stack);
            }   
            else {
                res.ok(data);
            }

        })
    }

}

My focus isn't the specific rules so much, but I want to tweak the script so that instead of adding the parameters of the security group in the script itself, that it does some sort of a get request or something of the sort from a .json file, that would have all the different security groups and rules associated with in it. How would I write that get request into this script?

I'm imagining the .json to look something like this.

{
    "SecurityGroups": [
        {
            "IpPermissionsEgress": [],
            "Description": "My security group",
            "IpPermissions": [
                {
                    "PrefixListIds": [],
                    "FromPort": 22,
                    "IpRanges": [
                        {
                            "CidrIp": "203.0.113.0/24"
                        }
                    ],
                    "ToPort": 22,
                    "IpProtocol": "tcp",
                    "UserIdGroupPairs": []
                }
            ],
            "GroupName": "MySecurityGroup",
            "OwnerId": "123456789012",
            "GroupId": "sg-903004f8",
        }
            {
            "IpPermissionsEgress": [],
            "Description": "My security group2",
            "IpPermissions": [
                {
                    "PrefixListIds": [],
                    "FromPort": 22,
                    "IpRanges": [
                        {
                            "CidrIp": "203.0.113.0/24"
                        }
                    ],
                    "ToPort": 22,
                    "IpProtocol": "tcp",
                    "UserIdGroupPairs": []
                }
            ],
            "GroupName": "MySecurityGroup2",
            "OwnerId": "123456789012",
            "GroupId": "sg-903004f28",
        }]
} 



via user2019182

No comments:

Post a Comment