ruby/example_code/ec2/ec2-ruby-example-create-security-group.rb
# Prerequisites:
#
# - A VPC in Amazon Virtual Private Cloud (Amazon VPC).
#
# @param ec2_resource [Aws::EC2::Resource] An initialized
#   Amazon EC2 resource object.
# @param group_name [String] A name for the security group.
# @param description [String] A description for the security group.
# @param vpc_id [String] The ID of the VPC for the security group.
# @param protocol [String] The network protocol for the outbound rule.
# @param from_port [String] The originating port for the outbound rule.
# @param to_port [String] The destination port for the outbound rule.
# @param cidr_ip_range [String] The CIDR IP range for the outbound rule.
# @return [Boolean] true if the security group was created and the outbound
#   rule was added; otherwise, false.
# @example
#   exit 1 unless security_group_created_with_egress?(
#     Aws::EC2::Resource.new(region: 'us-west-2'),
#     'my-security-group',
#     'This is my security group.',
#     'vpc-6713dfEX',
#     'tcp',
#     '22',
#     '22',
#     '0.0.0.0/0'
#   )

A VPC in Amazon Virtual Private Cloud (Amazon VPC).
A VPC in Amazon Virtual Private Cloud ( Amazon VPC ) .
def security_group_created_with_egress?(
  ec2_resource,
  group_name,
  description,
  vpc_id,
  ip_protocol,
  from_port,
  to_port,
  cidr_ip_range
)
  security_group = ec2_resource.create_security_group(
    group_name: group_name,
    description: description,
    vpc_id: vpc_id
  )
  puts "Created security group '#{group_name}' with ID " \
    "'#{security_group.id}' in VPC with ID '#{vpc_id}'."
  security_group.authorize_egress(
    ip_permissions: [
      {
        ip_protocol: ip_protocol,
        from_port: from_port,
        to_port: to_port,
        ip_ranges: [
          {
            cidr_ip: cidr_ip_range
          }
        ]
      }
    ]
  )
  puts "Granted egress to security group '#{group_name}' for protocol " \
    "'#{ip_protocol}' from port '#{from_port}' to port '#{to_port}' " \
    "with CIDR IP range '#{cidr_ip_range}'."
  return true
rescue StandardError => e
  puts "Error creating security group or granting egress: #{e.message}"
  return false
end
def security_group_created_with_egress? ( ec2_resource , group_name , description , vpc_id , ip_protocol , from_port , to_port , cidr_ip_range ) security_group = ec2_resource . create_security_group ( group_name : group_name , description : description , vpc_id : vpc_id ) puts "Created security group '#{group_name}' with ID " "'#{security_group.id}' in VPC with ID '#{vpc_id}'." security_group . authorize_egress ( ip_permissions : [ { ip_protocol : ip_protocol , from_port : from_port , to_port : to_port , ip_ranges : [ { cidr_ip : cidr_ip_range } ] } ] ) puts "Granted egress to security group '#{group_name}' for protocol " "'#{ip_protocol}' from port '#{from_port}' to port '#{to_port}' " "with CIDR IP range '#{cidr_ip_range}'." return true rescue StandardError => e puts "Error creating security group or granting egress: #{e.message}" return false end
Prerequisites:
 A VPC in Amazon Virtual Private Cloud (Amazon VPC).
Prerequisites : A VPC in Amazon Virtual Private Cloud ( Amazon VPC ) .
true if the security group was created and the outbound rule was added; otherwise, false.
true if the security group was created and the outbound rule was added ; otherwise false .
Boolean
ec2_resource
An initialized Amazon EC2 resource object.
An initialized Amazon EC2 resource object .
group_name
A name for the security group.
A name for the security group .
description
A description for the security group.
A description for the security group .
vpc_id
The ID of the VPC for the security group.
The ID of the VPC for the security group .
ip_protocol
from_port
The originating port for the outbound rule.
The originating port for the outbound rule .
to_port
The destination port for the outbound rule.
The destination port for the outbound rule .
cidr_ip_range
The CIDR IP range for the outbound rule.
The CIDR IP range for the outbound rule .
protocol
The network protocol for the outbound rule.
The network protocol for the outbound rule .
example
None