aws-insight 0.14.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.rspec +2 -0
  4. data/.rubocop.yml +84 -0
  5. data/.travis.yml +19 -0
  6. data/CHANGELOG.md +336 -0
  7. data/CODE_OF_CONDUCT.md +13 -0
  8. data/CONTRIBUTING.md +62 -0
  9. data/Gemfile +19 -0
  10. data/Guardfile +12 -0
  11. data/LICENSE +22 -0
  12. data/README.md +393 -0
  13. data/Rakefile +6 -0
  14. data/bin/terraforming +5 -0
  15. data/contrib/zsh-completion/_terraforming +65 -0
  16. data/lib/terraforming.rb +54 -0
  17. data/lib/terraforming/cli.rb +340 -0
  18. data/lib/terraforming/resource/alb.rb +98 -0
  19. data/lib/terraforming/resource/auto_scaling_group.rb +90 -0
  20. data/lib/terraforming/resource/cloud_watch_alarm.rb +87 -0
  21. data/lib/terraforming/resource/config.rb +98 -0
  22. data/lib/terraforming/resource/db_parameter_group.rb +58 -0
  23. data/lib/terraforming/resource/db_security_group.rb +57 -0
  24. data/lib/terraforming/resource/db_subnet_group.rb +52 -0
  25. data/lib/terraforming/resource/ec2.rb +130 -0
  26. data/lib/terraforming/resource/efs_file_system.rb +60 -0
  27. data/lib/terraforming/resource/eip.rb +66 -0
  28. data/lib/terraforming/resource/elasti_cache_cluster.rb +80 -0
  29. data/lib/terraforming/resource/elasti_cache_subnet_group.rb +56 -0
  30. data/lib/terraforming/resource/elb.rb +203 -0
  31. data/lib/terraforming/resource/iam_group.rb +54 -0
  32. data/lib/terraforming/resource/iam_group_membership.rb +63 -0
  33. data/lib/terraforming/resource/iam_group_policy.rb +71 -0
  34. data/lib/terraforming/resource/iam_instance_profile.rb +54 -0
  35. data/lib/terraforming/resource/iam_policy.rb +63 -0
  36. data/lib/terraforming/resource/iam_policy_attachment.rb +86 -0
  37. data/lib/terraforming/resource/iam_role.rb +56 -0
  38. data/lib/terraforming/resource/iam_role_policy.rb +71 -0
  39. data/lib/terraforming/resource/iam_user.rb +55 -0
  40. data/lib/terraforming/resource/iam_user_policy.rb +71 -0
  41. data/lib/terraforming/resource/internet_gateway.rb +54 -0
  42. data/lib/terraforming/resource/kms_alias.rb +55 -0
  43. data/lib/terraforming/resource/kms_key.rb +84 -0
  44. data/lib/terraforming/resource/launch_configuration.rb +106 -0
  45. data/lib/terraforming/resource/nat_gateway.rb +57 -0
  46. data/lib/terraforming/resource/network_acl.rb +83 -0
  47. data/lib/terraforming/resource/network_interface.rb +68 -0
  48. data/lib/terraforming/resource/rds.rb +74 -0
  49. data/lib/terraforming/resource/redshift.rb +67 -0
  50. data/lib/terraforming/resource/route53_record.rb +106 -0
  51. data/lib/terraforming/resource/route53_zone.rb +89 -0
  52. data/lib/terraforming/resource/route_table.rb +122 -0
  53. data/lib/terraforming/resource/route_table_association.rb +59 -0
  54. data/lib/terraforming/resource/s3.rb +69 -0
  55. data/lib/terraforming/resource/security_group.rb +188 -0
  56. data/lib/terraforming/resource/sns_topic.rb +75 -0
  57. data/lib/terraforming/resource/sns_topic_subscription.rb +83 -0
  58. data/lib/terraforming/resource/sqs.rb +70 -0
  59. data/lib/terraforming/resource/subnet.rb +55 -0
  60. data/lib/terraforming/resource/vpc.rb +67 -0
  61. data/lib/terraforming/resource/vpn_gateway.rb +55 -0
  62. data/lib/terraforming/template/tf/alb.erb +28 -0
  63. data/lib/terraforming/template/tf/auto_scaling_group.erb +28 -0
  64. data/lib/terraforming/template/tf/cloud_watch_alarm.erb +33 -0
  65. data/lib/terraforming/template/tf/db_parameter_group.erb +17 -0
  66. data/lib/terraforming/template/tf/db_security_group.erb +26 -0
  67. data/lib/terraforming/template/tf/db_subnet_group.erb +8 -0
  68. data/lib/terraforming/template/tf/ec2.erb +56 -0
  69. data/lib/terraforming/template/tf/eip.erb +11 -0
  70. data/lib/terraforming/template/tf/elasti_cache_cluster.erb +22 -0
  71. data/lib/terraforming/template/tf/elasti_cache_subnet_group.erb +8 -0
  72. data/lib/terraforming/template/tf/elastic_filesystem.erb +18 -0
  73. data/lib/terraforming/template/tf/elb.erb +51 -0
  74. data/lib/terraforming/template/tf/iam_group.erb +7 -0
  75. data/lib/terraforming/template/tf/iam_group_membership.erb +8 -0
  76. data/lib/terraforming/template/tf/iam_group_policy.erb +10 -0
  77. data/lib/terraforming/template/tf/iam_instance_profile.erb +8 -0
  78. data/lib/terraforming/template/tf/iam_policy.erb +12 -0
  79. data/lib/terraforming/template/tf/iam_policy_attachment.erb +10 -0
  80. data/lib/terraforming/template/tf/iam_role.erb +10 -0
  81. data/lib/terraforming/template/tf/iam_role_policy.erb +10 -0
  82. data/lib/terraforming/template/tf/iam_user.erb +7 -0
  83. data/lib/terraforming/template/tf/iam_user_policy.erb +10 -0
  84. data/lib/terraforming/template/tf/internet_gateway.erb +14 -0
  85. data/lib/terraforming/template/tf/kms_alias.erb +7 -0
  86. data/lib/terraforming/template/tf/kms_key.erb +13 -0
  87. data/lib/terraforming/template/tf/launch_configuration.erb +68 -0
  88. data/lib/terraforming/template/tf/nat_gateway.erb +9 -0
  89. data/lib/terraforming/template/tf/network_acl.erb +43 -0
  90. data/lib/terraforming/template/tf/network_interface.erb +22 -0
  91. data/lib/terraforming/template/tf/rds.erb +25 -0
  92. data/lib/terraforming/template/tf/redshift.erb +23 -0
  93. data/lib/terraforming/template/tf/route53_record.erb +52 -0
  94. data/lib/terraforming/template/tf/route53_zone.erb +18 -0
  95. data/lib/terraforming/template/tf/route_table.erb +34 -0
  96. data/lib/terraforming/template/tf/route_table_association.erb +9 -0
  97. data/lib/terraforming/template/tf/s3.erb +12 -0
  98. data/lib/terraforming/template/tf/security_group.erb +56 -0
  99. data/lib/terraforming/template/tf/sns_topic.erb +17 -0
  100. data/lib/terraforming/template/tf/sns_topic_subscription.erb +23 -0
  101. data/lib/terraforming/template/tf/sqs.erb +21 -0
  102. data/lib/terraforming/template/tf/subnet.erb +15 -0
  103. data/lib/terraforming/template/tf/vpc.erb +15 -0
  104. data/lib/terraforming/template/tf/vpn_gateway.erb +14 -0
  105. data/lib/terraforming/util.rb +30 -0
  106. data/lib/terraforming/version.rb +3 -0
  107. data/script/console +14 -0
  108. data/script/generate +97 -0
  109. data/script/setup +7 -0
  110. data/templates/resource.erb.erb +3 -0
  111. data/templates/resource.rb.erb +31 -0
  112. data/templates/resource_spec.rb.erb +39 -0
  113. data/terraforming.gemspec +31 -0
  114. metadata +271 -0
@@ -0,0 +1,55 @@
1
+ module Terraforming
2
+ module Resource
3
+ class KMSAlias
4
+ include Terraforming::Util
5
+
6
+ def self.tf(client: Aws::KMS::Client.new)
7
+ self.new(client).tf
8
+ end
9
+
10
+ def self.tfstate(client: Aws::KMS::Client.new)
11
+ self.new(client).tfstate
12
+ end
13
+
14
+ def initialize(client)
15
+ @client = client
16
+ end
17
+
18
+ def tf
19
+ apply_template(@client, "tf/kms_alias")
20
+ end
21
+
22
+ def tfstate
23
+ aliases.inject({}) do |resources, als|
24
+ resources["aws_kms_alias.#{module_name_of(als)}"] = {
25
+ "type" => "aws_kms_alias",
26
+ "primary" => {
27
+ "id" => als.alias_name,
28
+ "attributes" => {
29
+ "arn" => als.alias_arn,
30
+ "id" => als.alias_name,
31
+ "name" => als.alias_name,
32
+ "target_key_id" => als.target_key_id,
33
+ },
34
+ },
35
+ }
36
+ resources
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def aliases
43
+ @client.list_aliases.aliases.reject { |als| managed_master_key_alias?(als) }
44
+ end
45
+
46
+ def managed_master_key_alias?(als)
47
+ als.alias_name =~ %r{\Aalias/aws/}
48
+ end
49
+
50
+ def module_name_of(als)
51
+ normalize_module_name(als.alias_name.gsub(%r{\Aalias/}, ""))
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,84 @@
1
+ module Terraforming
2
+ module Resource
3
+ class KMSKey
4
+ include Terraforming::Util
5
+
6
+ def self.tf(client: Aws::KMS::Client.new)
7
+ self.new(client).tf
8
+ end
9
+
10
+ def self.tfstate(client: Aws::KMS::Client.new)
11
+ self.new(client).tfstate
12
+ end
13
+
14
+ def initialize(client)
15
+ @client = client
16
+ end
17
+
18
+ def tf
19
+ apply_template(@client, "tf/kms_key")
20
+ end
21
+
22
+ def tfstate
23
+ keys.inject({}) do |resources, key|
24
+ resources["aws_kms_key.#{module_name_of(key)}"] = {
25
+ "type" => "aws_kms_key",
26
+ "primary" => {
27
+ "id" => key.key_id,
28
+ "attributes" => {
29
+ "arn" => key.arn,
30
+ "description" => key.description,
31
+ "enable_key_rotation" => key_rotation_status_of(key).key_rotation_enabled.to_s,
32
+ "id" => key.key_id,
33
+ "is_enabled" => key.enabled.to_s,
34
+ "key_id" => key.key_id,
35
+ "key_usage" => key_usage_of(key),
36
+ "policy" => key_policy_of(key),
37
+ },
38
+ },
39
+ }
40
+ resources
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def aliases
47
+ @aliases ||= @client.list_aliases.aliases
48
+ end
49
+
50
+ def keys
51
+ @client
52
+ .list_keys
53
+ .keys
54
+ .reject { |key| managed_master_key?(key) }
55
+ .map { |key| @client.describe_key(key_id: key.key_id) }
56
+ .map(&:key_metadata)
57
+ end
58
+
59
+ def key_policy_of(key)
60
+ policies = @client.list_key_policies(key_id: key.key_id).policy_names
61
+
62
+ return "" if policies.empty?
63
+
64
+ @client.get_key_policy(key_id: key.key_id, policy_name: policies[0]).policy
65
+ end
66
+
67
+ def key_rotation_status_of(key)
68
+ @client.get_key_rotation_status(key_id: key.key_id)
69
+ end
70
+
71
+ def key_usage_of(key)
72
+ key.key_usage.tr("_", "/")
73
+ end
74
+
75
+ def managed_master_key?(key)
76
+ !aliases.select { |a| a.target_key_id == key.key_id && a.alias_name =~ %r{\Aalias/aws/} }.empty?
77
+ end
78
+
79
+ def module_name_of(key)
80
+ normalize_module_name(key.key_id)
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,106 @@
1
+ module Terraforming
2
+ module Resource
3
+ class LaunchConfiguration
4
+ include Terraforming::Util
5
+
6
+ def self.tf(client: Aws::AutoScaling::Client.new)
7
+ self.new(client).tf
8
+ end
9
+
10
+ def self.tfstate(client: Aws::AutoScaling::Client.new)
11
+ self.new(client).tfstate
12
+ end
13
+
14
+ def initialize(client)
15
+ @client = client
16
+ end
17
+
18
+ def tf
19
+ apply_template(@client, "tf/launch_configuration")
20
+ end
21
+
22
+ def tfstate
23
+ launch_configurations.inject({}) do |resources, lc|
24
+ attributes = {
25
+ "name" => lc.launch_configuration_name,
26
+ "image_id" => lc.image_id,
27
+ "instance_type" => lc.instance_type,
28
+ "key_name" => lc.key_name,
29
+ "security_groups.#" => lc.security_groups.length.to_s,
30
+ "associate_public_ip_address" => lc.associate_public_ip_address.to_s,
31
+ "user_data" => lc.user_data,
32
+ "enable_monitoring" => lc.instance_monitoring.enabled.to_s,
33
+ "ebs_optimized" => lc.ebs_optimized.to_s,
34
+ "root_block_device.#" => root_block_device_count(lc).to_s,
35
+ "ebs_block_device.#" => ebs_block_device_count(lc).to_s,
36
+ "ephemeral_block_device.#" => ephemeral_block_device_count(lc).to_s
37
+ }
38
+
39
+ lc.security_groups.each do |sg|
40
+ hash = hash_security_group(sg)
41
+ attributes["security_groups.#{hash}"] = sg
42
+ end
43
+
44
+ attributes["iam_instance_profile"] = lc.iam_instance_profile if lc.iam_instance_profile
45
+ attributes["spot_price"] = lc.spot_price if lc.spot_price
46
+ attributes["placement_tenancy"] = lc.placement_tenancy if lc.placement_tenancy
47
+
48
+ resources["aws_launch_configuration.#{module_name_of(lc)}"] = {
49
+ "type" => "aws_launch_configuration",
50
+ "primary" => {
51
+ "id" => lc.launch_configuration_name,
52
+ "attributes" => attributes
53
+ }
54
+ }
55
+
56
+ resources
57
+ end
58
+ end
59
+
60
+ private
61
+
62
+ # Taken from http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/device_naming.html
63
+ def root_block_device?(block_device)
64
+ %w(/dev/sda1 /dev/xvda).include? block_device.device_name
65
+ end
66
+
67
+ def root_block_device_count(launch_configuration)
68
+ launch_configuration.block_device_mappings.select do |volume|
69
+ root_block_device?(volume)
70
+ end.length
71
+ end
72
+
73
+ def ebs_block_device?(block_device)
74
+ block_device.virtual_name.nil? && block_device.ebs
75
+ end
76
+
77
+ def ebs_block_device_count(launch_configuration)
78
+ launch_configuration.block_device_mappings.select do |volume|
79
+ ebs_block_device?(volume) && !root_block_device?(volume)
80
+ end.length
81
+ end
82
+
83
+ def ephemeral_block_device?(block_device)
84
+ block_device.virtual_name != nil
85
+ end
86
+
87
+ def ephemeral_block_device_count(launch_configuration)
88
+ launch_configuration.block_device_mappings.select do |volume|
89
+ ephemeral_block_device?(volume)
90
+ end.length
91
+ end
92
+
93
+ def hash_security_group(name)
94
+ Zlib.crc32(name)
95
+ end
96
+
97
+ def launch_configurations
98
+ @client.describe_launch_configurations.map(&:launch_configurations).flatten
99
+ end
100
+
101
+ def module_name_of(launch_configuration)
102
+ normalize_module_name(launch_configuration.launch_configuration_name)
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,57 @@
1
+ module Terraforming
2
+ module Resource
3
+ class NATGateway
4
+ include Terraforming::Util
5
+
6
+ def self.tf(client: Aws::EC2::Client.new)
7
+ self.new(client).tf
8
+ end
9
+
10
+ def self.tfstate(client: Aws::EC2::Client.new)
11
+ self.new(client).tfstate
12
+ end
13
+
14
+ def initialize(client)
15
+ @client = client
16
+ end
17
+
18
+ def tf
19
+ apply_template(@client, "tf/nat_gateway")
20
+ end
21
+
22
+ def tfstate
23
+ nat_gateways.inject({}) do |resources, nat_gateway|
24
+ next resources if nat_gateway.nat_gateway_addresses.empty?
25
+
26
+ attributes = {
27
+ "id" => nat_gateway.nat_gateway_id,
28
+ "allocation_id" => nat_gateway.nat_gateway_addresses[0].allocation_id,
29
+ "subnet_id" => nat_gateway.subnet_id,
30
+ "network_inferface_id" => nat_gateway.nat_gateway_addresses[0].network_interface_id,
31
+ "private_ip" => nat_gateway.nat_gateway_addresses[0].private_ip,
32
+ "public_ip" => nat_gateway.nat_gateway_addresses[0].public_ip,
33
+ }
34
+ resources["aws_nat_gateway.#{module_name_of(nat_gateway)}"] = {
35
+ "type" => "aws_nat_gateway",
36
+ "primary" => {
37
+ "id" => nat_gateway.nat_gateway_id,
38
+ "attributes" => attributes
39
+ }
40
+ }
41
+
42
+ resources
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ def nat_gateways
49
+ @client.describe_nat_gateways.nat_gateways
50
+ end
51
+
52
+ def module_name_of(nat_gateway)
53
+ normalize_module_name(nat_gateway.nat_gateway_id)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,83 @@
1
+ module Terraforming
2
+ module Resource
3
+ class NetworkACL
4
+ include Terraforming::Util
5
+
6
+ def self.tf(client: Aws::EC2::Client.new)
7
+ self.new(client).tf
8
+ end
9
+
10
+ def self.tfstate(client: Aws::EC2::Client.new)
11
+ self.new(client).tfstate
12
+ end
13
+
14
+ def initialize(client)
15
+ @client = client
16
+ end
17
+
18
+ def tf
19
+ apply_template(@client, "tf/network_acl")
20
+ end
21
+
22
+ def tfstate
23
+ network_acls.inject({}) do |resources, network_acl|
24
+ attributes = {
25
+ "egress.#" => egresses_of(network_acl).length.to_s,
26
+ "id" => network_acl.network_acl_id,
27
+ "ingress.#" => ingresses_of(network_acl).length.to_s,
28
+ "subnet_ids.#" => subnet_ids_of(network_acl).length.to_s,
29
+ "tags.#" => network_acl.tags.length.to_s,
30
+ "vpc_id" => network_acl.vpc_id,
31
+ }
32
+ resources["aws_network_acl.#{module_name_of(network_acl)}"] = {
33
+ "type" => "aws_network_acl",
34
+ "primary" => {
35
+ "id" => network_acl.network_acl_id,
36
+ "attributes" => attributes
37
+ }
38
+ }
39
+
40
+ resources
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def default_entry?(entry)
47
+ entry.rule_number == default_rule_number
48
+ end
49
+
50
+ def default_rule_number
51
+ 32767
52
+ end
53
+
54
+ def egresses_of(network_acl)
55
+ network_acl.entries.select { |entry| entry.egress && !default_entry?(entry) }
56
+ end
57
+
58
+ def from_port_of(entry)
59
+ entry.port_range ? entry.port_range.from : 0
60
+ end
61
+
62
+ def ingresses_of(network_acl)
63
+ network_acl.entries.select { |entry| !entry.egress && !default_entry?(entry) }
64
+ end
65
+
66
+ def module_name_of(network_acl)
67
+ normalize_module_name(name_from_tag(network_acl, network_acl.network_acl_id))
68
+ end
69
+
70
+ def network_acls
71
+ @client.describe_network_acls.map(&:network_acls).flatten
72
+ end
73
+
74
+ def subnet_ids_of(network_acl)
75
+ network_acl.associations.map { |association| association.subnet_id }
76
+ end
77
+
78
+ def to_port_of(entry)
79
+ entry.port_range ? entry.port_range.to : 0
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,68 @@
1
+ module Terraforming
2
+ module Resource
3
+ class NetworkInterface
4
+ include Terraforming::Util
5
+
6
+ def self.tf(client: Aws::EC2::Client.new)
7
+ self.new(client).tf
8
+ end
9
+
10
+ def self.tfstate(client: Aws::EC2::Client.new)
11
+ self.new(client).tfstate
12
+ end
13
+
14
+ def initialize(client)
15
+ @client = client
16
+ end
17
+
18
+ def tf
19
+ apply_template(@client, "tf/network_interface")
20
+ end
21
+
22
+ def tfstate
23
+ network_interfaces.inject({}) do |resources, network_interface|
24
+ attributes = {
25
+ "attachment.#" => attachment_of(network_interface) ? "1" : "0",
26
+ "id" => network_interface.network_interface_id,
27
+ "private_ips.#" => private_ips_of(network_interface).length.to_s,
28
+ "security_groups.#" => security_groups_of(network_interface).length.to_s,
29
+ "source_dest_check" => network_interface.source_dest_check.to_s,
30
+ "subnet_id" => network_interface.subnet_id,
31
+ "tags.#" => network_interface.tag_set.length.to_s,
32
+ }
33
+ resources["aws_network_interface.#{module_name_of(network_interface)}"] = {
34
+ "type" => "aws_network_interface",
35
+ "primary" => {
36
+ "id" => network_interface.network_interface_id,
37
+ "attributes" => attributes
38
+ }
39
+ }
40
+
41
+ resources
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def attachment_of(network_interface)
48
+ network_interface.attachment
49
+ end
50
+
51
+ def private_ips_of(network_interface)
52
+ network_interface.private_ip_addresses.map { |addr| addr.private_ip_address }
53
+ end
54
+
55
+ def security_groups_of(network_interface)
56
+ network_interface.groups.map { |group| group.group_id }
57
+ end
58
+
59
+ def module_name_of(network_interface)
60
+ network_interface.network_interface_id
61
+ end
62
+
63
+ def network_interfaces
64
+ @client.describe_network_interfaces.map(&:network_interfaces).flatten
65
+ end
66
+ end
67
+ end
68
+ end