---
# System prepended metadata

title: Query 268

---

To manage security groups when configuring an AWS Elastic Load Balancer (ELB) using JavaScript, you can use the AWS SDK for JavaScript, commonly known as AWS Amplify. Below are JavaScript code examples for managing security groups in the context of an Elastic Load Balancer:
1. Create a Security Group for Your Elastic Load Balancer: You can create a security group for your ELB, specify the inbound and outbound rules, and attach it to the ELB. This example uses the AWS SDK for JavaScript (AWS Amplify) to perform these tasks.
    ```
    const AWS = require('aws-sdk');

    AWS.config.update({ region: 'us-east-1' }); // Set your desired region

    const elbv2 = new AWS.ELBv2();

    // Define your security group parameters
    const securityGroupName = 'MyELBSecurityGroup';
    const description = 'My ELB Security Group Description';
    const vpcId = 'your-vpc-id';

    const params = {
      Description: description,
      GroupName: securityGroupName,
      VpcId: vpcId,
    };

    elbv2.createSecurityGroup(params, function(err, data) {
      if (err) {
        console.error('Error creating security group:', err);
      } else {
        console.log('Security group created:', data.SecurityGroups[0]);

        // Define inbound rules (add more rules as needed)
        const inboundParams = {
          GroupId: data.SecurityGroups[0].GroupId,
          IpPermissions: [
            {
              IpProtocol: 'TCP',
              FromPort: 80,
              ToPort: 80,
              IpRanges: [{ CidrIp: '0.0.0.0/0' }], // Allow HTTP traffic from anywhere
            },
          ],
        };

        elbv2.authorizeSecurityGroupIngress(inboundParams, function(inboundErr) {
          if (inboundErr) {
            console.error('Error authorizing ingress rules:', inboundErr);
          } else {
            console.log('Ingress rules authorized.');
          }
        });
      }
    });
    ```    
2. Attach the Security Group to an Elastic Load Balancer: You can attach a security group to your ELB by specifying its ARN (Amazon Resource Name).
    ```
    // Define your ELB and security group information
    const loadBalancerName = 'MyLoadBalancer';
    const securityGroupArn = 'arn:aws:ec2:us-east-1:123456789012:security-group/sg-0123456789abcdef0';

    const params = {
      LoadBalancerName: loadBalancerName,
      SecurityGroups: [securityGroupArn],
    };

    elbv2.setSecurityGroups(params, function(err) {
      if (err) {
        console.error('Error attaching security group:', err);
      } else {
        console.log('Security group attached to ELB.');
      }
    });
    ```
    These JavaScript code examples demonstrate how to create a security group for your Elastic Load Balancer and attach it to the ELB. Make sure to customize the parameters and security group rules to match your specific requirements.