module Formatron::CloudFormation::Resources::EC2

Generates CloudFormation template EC2 resources rubocop:disable Metrics/ModuleLength

Constants

BLOCK_DEVICE_MAPPINGS

Public Class Methods

block_device_mapping(device:, size:, type:, iops:) click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/formatron/cloud_formation/resources/ec2.rb, line 238
def self.block_device_mapping(device:, size:, type:, iops:)
  mapping = {
    DeviceName: device,
    Ebs: {
      VolumeSize: size
    }
  }
  mapping[:Ebs][:VolumeType] = type unless type.nil?
  mapping[:Ebs][:Iops] = iops unless iops.nil?
  mapping
end
instance( instance_profile:, availability_zone:, instance_type:, key_name:, administrator_name:, administrator_password:, subnet:, name:, wait_condition_handle:, security_group:, logical_id:, source_dest_check:, os:, ami: ) click to toggle source

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/ParameterLists rubocop:disable Metrics/AbcSize

# File lib/formatron/cloud_formation/resources/ec2.rb, line 277
def self.instance(
  instance_profile:,
  availability_zone:,
  instance_type:,
  key_name:,
  administrator_name:,
  administrator_password:,
  subnet:,
  name:,
  wait_condition_handle:,
  security_group:,
  logical_id:,
  source_dest_check:,
  os:,
  ami:
)
  ami = Template.find_in_map(
    Template::REGION_MAP,
    Template.ref('AWS::Region'),
    os
  ) if ami.nil?
  if os.eql? 'windows'
    user_data = Template.base_64(
      Template.join(
        # rubocop:disable Metrics/LineLength
        "<powershell>\n",
        "try\n",
        "{\n",
        Scripts.windows_administrator(
          name: administrator_name,
          password: administrator_password
        ),
        'winrm quickconfig -q', "\n",
        "winrm set winrm/config/winrs '@{MaxMemoryPerShellMB=\"1024\"}'", "\n",
        "winrm set winrm/config '@{MaxTimeoutms=\"1800000\"}'", "\n",
        "winrm set winrm/config/service '@{AllowUnencrypted=\"true\"}'", "\n",
        "winrm set winrm/config/service/auth '@{Basic=\"true\"}'", "\n",
        'netsh advfirewall firewall add rule name="WinRM 5985" protocol=TCP dir=in localport=5985 action=allow', "\n",
        'netsh advfirewall firewall add rule name="WinRM 5986" protocol=TCP dir=in localport=5986 action=allow', "\n",
        'Stop-Service winrm', "\n",
        'Set-Service winrm -startuptype "automatic"', "\n",
        'Start-Service winrm', "\n",
        'cfn-init.exe -v -s ', Template.ref('AWS::StackName'),
        " -r #{logical_id}",
        ' --region ', Template.ref('AWS::Region'), "\n",
        "}\n",
        "catch\n",
        "{\n",
        'cfn-signal.exe -e 1 ',
        Template.base_64(Template.ref(wait_condition_handle)), "\n",
        "}\n",
        '</powershell>'
      # rubocop:enable Metrics/LineLength
      )
    )
  else
    user_data = Template.base_64(
      Template.join(
        # rubocop:disable Metrics/LineLength
        "#!/bin/bash -v\n",
        "apt-get -y update\n",
        "apt-get -y install python-setuptools\n",
        "easy_install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz\n",
        "export PATH=$PATH:/opt/aws/bin\n",
        'cfn-init --region ', Template.ref('AWS::Region'),
        '    -v -s ', Template.ref('AWS::StackName'), " -r #{logical_id}\n",
        "cfn-signal -e $? -r 'Formatron instance configuration complete' '", Template.ref(wait_condition_handle), "'\n"
      # rubocop:enable Metrics/LineLength
      )
    )
  end
  {
    Type: 'AWS::EC2::Instance',
    Properties: {
      IamInstanceProfile: Template.ref(instance_profile),
      AvailabilityZone: Template.join(
        Template.ref('AWS::Region'),
        availability_zone
      ),
      ImageId: ami,
      SourceDestCheck: source_dest_check,
      InstanceType: instance_type,
      KeyName: key_name,
      SubnetId: Template.ref(subnet),
      SecurityGroupIds: [Template.ref(security_group)],
      Tags: [{
        Key: 'Name',
        Value: name
      }],
      UserData: user_data
    }
  }
end
internet_gateway() click to toggle source
# File lib/formatron/cloud_formation/resources/ec2.rb, line 24
def self.internet_gateway
  {
    Type: 'AWS::EC2::InternetGateway'
  }
end
network_acl(vpc:) click to toggle source
# File lib/formatron/cloud_formation/resources/ec2.rb, line 106
def self.network_acl(vpc:)
  {
    Type: 'AWS::EC2::NetworkAcl',
    Properties: {
      VpcId: Template.ref(vpc)
    }
  }
end
network_acl_entry( network_acl:, cidr:, egress:, protocol:, action:, icmp_code: nil, icmp_type: nil, start_port: nil, end_port: nil, number: ) click to toggle source

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/ParameterLists

# File lib/formatron/cloud_formation/resources/ec2.rb, line 127
def self.network_acl_entry(
  network_acl:,
  cidr:,
  egress:,
  protocol:,
  action:,
  icmp_code: nil,
  icmp_type: nil,
  start_port: nil,
  end_port: nil,
  number:
)
  resource = {
    Type: 'AWS::EC2::NetworkAclEntry',
    Properties: {
      NetworkAclId: Template.ref(network_acl),
      CidrBlock: cidr,
      Egress: egress,
      Protocol: protocol,
      RuleAction: action,
      RuleNumber: number
    }
  }
  resource[:Properties][:Icmp] = {
    Code: icmp_code,
    Type: icmp_type
  } unless icmp_code.nil?
  resource[:Properties][:PortRange] = {
    From: start_port,
    To: end_port
  } unless start_port.nil?
  resource
end
route( route_table:, instance: nil, internet_gateway: nil, vpc_gateway_attachment: nil ) click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/formatron/cloud_formation/resources/ec2.rb, line 50
def self.route(
  route_table:,
  instance: nil,
  internet_gateway: nil,
  vpc_gateway_attachment: nil
)
  properties = {
    RouteTableId: Template.ref(route_table),
    DestinationCidrBlock: '0.0.0.0/0'
  }
  properties[:GatewayId] =
    Template.ref internet_gateway unless internet_gateway.nil?
  properties[:InstanceId] =
    Template.ref instance unless instance.nil?
  route = {
    Type: 'AWS::EC2::Route',
    Properties: properties
  }
  route[:DependsOn] =
    vpc_gateway_attachment unless vpc_gateway_attachment.nil?
  route
end
route_table(vpc:) click to toggle source
# File lib/formatron/cloud_formation/resources/ec2.rb, line 40
def self.route_table(vpc:)
  {
    Type: 'AWS::EC2::RouteTable',
    Properties: {
      VpcId: Template.ref(vpc)
    }
  }
end
security_group( group_description:, vpc:, egress:, ingress: ) click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/formatron/cloud_formation/resources/ec2.rb, line 164
def self.security_group(
  group_description:,
  vpc:,
  egress:,
  ingress:
)
  {
    Type: 'AWS::EC2::SecurityGroup',
    Properties: {
      GroupDescription: group_description,
      VpcId: Template.ref(vpc),
      SecurityGroupEgress: egress.collect do |rule|
        {
          CidrIp: rule[:cidr],
          IpProtocol: rule[:protocol],
          FromPort: rule[:from_port],
          ToPort: rule[:to_port]
        }
      end,
      SecurityGroupIngress: ingress.collect do |rule|
        {
          CidrIp: rule[:cidr],
          IpProtocol: rule[:protocol],
          FromPort: rule[:from_port],
          ToPort: rule[:to_port]
        }
      end
    }
  }
end
security_group_egress( security_group:, cidr:, protocol:, from_port:, to_port: ) click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/formatron/cloud_formation/resources/ec2.rb, line 197
def self.security_group_egress(
  security_group:,
  cidr:,
  protocol:,
  from_port:,
  to_port:
)
  {
    Type: 'AWS::EC2::SecurityGroupEgress',
    Properties: {
      GroupId: Template.ref(security_group),
      CidrIp: cidr,
      IpProtocol: protocol,
      FromPort: from_port,
      ToPort: to_port
    }
  }
end
security_group_ingress( security_group:, cidr:, protocol:, from_port:, to_port: ) click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/formatron/cloud_formation/resources/ec2.rb, line 218
def self.security_group_ingress(
  security_group:,
  cidr:,
  protocol:,
  from_port:,
  to_port:
)
  {
    Type: 'AWS::EC2::SecurityGroupIngress',
    Properties: {
      GroupId: Template.ref(security_group),
      CidrIp: cidr,
      IpProtocol: protocol,
      FromPort: from_port,
      ToPort: to_port
    }
  }
end
subnet( vpc:, cidr:, availability_zone:, map_public_ip_on_launch: ) click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/formatron/cloud_formation/resources/ec2.rb, line 75
def self.subnet(
  vpc:,
  cidr:,
  availability_zone:,
  map_public_ip_on_launch:
)
  {
    Type: 'AWS::EC2::Subnet',
    Properties: {
      VpcId: Template.ref(vpc),
      CidrBlock: cidr,
      MapPublicIpOnLaunch: map_public_ip_on_launch,
      AvailabilityZone: Template.join(
        Template.ref('AWS::Region'),
        availability_zone
      )
    }
  }
end
subnet_network_acl_association(subnet:, network_acl:) click to toggle source
# File lib/formatron/cloud_formation/resources/ec2.rb, line 115
def self.subnet_network_acl_association(subnet:, network_acl:)
  {
    Type: 'AWS::EC2::SubnetNetworkAclAssociation',
    Properties: {
      SubnetId: Template.ref(subnet),
      NetworkAclId: Template.ref(network_acl)
    }
  }
end
subnet_route_table_association(route_table:, subnet:) click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/formatron/cloud_formation/resources/ec2.rb, line 96
def self.subnet_route_table_association(route_table:, subnet:)
  {
    Type: 'AWS::EC2::SubnetRouteTableAssociation',
    Properties: {
      RouteTableId: Template.ref(route_table),
      SubnetId: Template.ref(subnet)
    }
  }
end
volume(size:, type:, iops:, availability_zone:) click to toggle source
# File lib/formatron/cloud_formation/resources/ec2.rb, line 250
def self.volume(size:, type:, iops:, availability_zone:)
  volume = {
    Type: 'AWS::EC2::Volume',
    Properties: {
      AvailabilityZone: availability_zone,
      Size: size
    }
  }
  volume[:Properties][:VolumeType] = type unless type.nil?
  volume[:Properties][:Iops] = iops unless iops.nil?
  volume
end
volume_attachment(device:, instance:, volume:) click to toggle source
# File lib/formatron/cloud_formation/resources/ec2.rb, line 263
def self.volume_attachment(device:, instance:, volume:)
  {
    Type: 'AWS::EC2::VolumeAttachment',
    Properties: {
      Device: device,
      InstanceId: Template.ref(instance),
      VolumeId: Template.ref(volume)
    }
  }
end
vpc(cidr:) click to toggle source
# File lib/formatron/cloud_formation/resources/ec2.rb, line 12
def self.vpc(cidr:)
  {
    Type: 'AWS::EC2::VPC',
    Properties: {
      CidrBlock: cidr,
      EnableDnsSupport: true,
      EnableDnsHostnames: true,
      InstanceTenancy: 'default'
    }
  }
end
vpc_gateway_attachment(vpc:, gateway:) click to toggle source
# File lib/formatron/cloud_formation/resources/ec2.rb, line 30
def self.vpc_gateway_attachment(vpc:, gateway:)
  {
    Type: 'AWS::EC2::VPCGatewayAttachment',
    Properties: {
      InternetGatewayId: Template.ref(gateway),
      VpcId: Template.ref(vpc)
    }
  }
end