Class: Terraforming::Resource::EC2

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/terraforming/resource/ec2.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#apply_template, #name_from_tag, #normalize_module_name, #prettify_policy, #template_path

Constructor Details

#initialize(client) ⇒ EC2

Returns a new instance of EC2.



14
15
16
# File 'lib/terraforming/resource/ec2.rb', line 14

def initialize(client)
  @client = client
end

Class Method Details

.tf(client: Aws::EC2::Client.new) ⇒ Object



6
7
8
# File 'lib/terraforming/resource/ec2.rb', line 6

def self.tf(client: Aws::EC2::Client.new)
  self.new(client).tf
end

.tfstate(client: Aws::EC2::Client.new) ⇒ Object



10
11
12
# File 'lib/terraforming/resource/ec2.rb', line 10

def self.tfstate(client: Aws::EC2::Client.new)
  self.new(client).tfstate
end

Instance Method Details

#tfObject



18
19
20
# File 'lib/terraforming/resource/ec2.rb', line 18

def tf
  apply_template(@client, "tf/ec2")
end

#tfstateObject



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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/terraforming/resource/ec2.rb', line 22

def tfstate
  instances.inject({}) do |resources, instance|
    in_vpc = in_vpc?(instance)
    block_devices = block_devices_of(instance)

    attributes = {
      "ami" => instance.image_id,
      "associate_public_ip_address" => associate_public_ip?(instance).to_s,
      "availability_zone" => instance.placement.availability_zone,
      "ebs_block_device.#" => ebs_block_devices_in(block_devices, instance).length.to_s,
      "ebs_optimized" => instance.ebs_optimized.to_s,
      "ephemeral_block_device.#" => "0", # Terraform 0.6.1 cannot fetch this field from AWS
      "id" => instance.instance_id,
      "instance_type" => instance.instance_type,
      "monitoring" => monitoring_state(instance).to_s,
      "private_dns" => instance.private_dns_name,
      "private_ip" => instance.private_ip_address,
      "public_dns" => instance.public_dns_name,
      "public_ip" => instance.public_ip_address,
      "root_block_device.#" => root_block_devices_in(block_devices, instance).length.to_s,
      "security_groups.#" => in_vpc ? "0" : instance.security_groups.length.to_s,
      "source_dest_check" => instance.source_dest_check.to_s,
      "tenancy" => instance.placement.tenancy,
      "vpc_security_group_ids.#" => in_vpc ? instance.security_groups.length.to_s : "0",
    }

    placement_group = instance.placement.group_name
    attributes["placement_group"] = placement_group unless placement_group.empty?

    attributes["subnet_id"] = instance.subnet_id if in_vpc?(instance)

    resources["aws_instance.#{module_name_of(instance)}"] = {
      "type" => "aws_instance",
      "primary" => {
        "id" => instance.instance_id,
        "attributes" => attributes,
        "meta" => {
          "schema_version" => "1"
        }
      }
    }

    resources
  end
end