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, #generate_tfstate, #name_from_tag, #normalize_module_name, #prettify_policy, #template_path, #tfstate_skeleton

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, tfstate_base: nil) ⇒ Object



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

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

Instance Method Details

#tfObject



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

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

#tfstate(tfstate_base) ⇒ Object



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
# File 'lib/terraforming/resource/ec2.rb', line 22

def tfstate(tfstate_base)
  resources = instances.inject({}) do |result, instance|
    attributes = {
      "ami"=> instance.image_id,
      "associate_public_ip_address"=> "true",
      "availability_zone"=> instance.placement.availability_zone,
      "ebs_block_device.#"=> instance.block_device_mappings.length.to_s,
      "ebs_optimized"=> instance.ebs_optimized.to_s,
      "ephemeral_block_device.#"=> "0",
      "id"=> instance.instance_id,
      "instance_type"=> instance.instance_type,
      "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.#"=> instance.root_device_name ? "1" : "0",
      "security_groups.#"=> instance.security_groups.length.to_s,
      "source_dest_check"=> instance.source_dest_check.to_s,
      "subnet_id"=> instance.subnet_id,
      "tenancy"=> instance.placement.tenancy
    }
    result["aws_instance.#{module_name_of(instance)}"] = {
      "type" => "aws_instance",
      "primary" => {
        "id" => instance.instance_id,
        "attributes" => attributes,
        "meta" => {
          "schema_version" => "1"
        }
      }
    }

    result
  end

  generate_tfstate(resources, tfstate_base)
end