Skip to content
This repository has been archived by the owner on Jun 5, 2020. It is now read-only.

Add the option to enable termination protection on ec2 instances. #527

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/puppet/provider/ec2_instance/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ def self.prefetch(resources)
end

def self.instance_to_hash(region, instance, subnets)
name = extract_name_from_tag(instance)
ec2 = ec2_client(region)
name = name_from_tag(instance)
return {} unless name
tags = {}
subnet_name = nil
Expand Down Expand Up @@ -90,6 +91,9 @@ def self.instance_to_hash(region, instance, subnets)
}
end

# Find the setting for termination protection
term_att = ec2.describe_instance_attribute({ attribute: 'disableApiTermination', instance_id: instance.image_id})

config = {
name: name,
instance_type: instance.instance_type,
Expand All @@ -104,6 +108,7 @@ def self.instance_to_hash(region, instance, subnets)
region: region,
tenancy: instance.placement.tenancy,
hypervisor: instance.hypervisor,
termination_protection: term_att,
iam_instance_profile_arn: instance.iam_instance_profile ? instance.iam_instance_profile.arn : nil,
virtualization_type: instance.virtualization_type,
security_groups: instance.security_groups.collect(&:group_name),
Expand Down Expand Up @@ -302,6 +307,7 @@ def create
instance_type: resource[:instance_type],
user_data: data,
ebs_optimized: resource[:ebs_optimized].to_s,
disable_api_termination: resource[:termination_protection],
instance_initiated_shutdown_behavior: resource[:instance_initiated_shutdown_behavior].to_s,
iam_instance_profile: resource[:iam_instance_profile_arn] ?
Hash['arn' => resource[:iam_instance_profile_arn]] :
Expand Down
9 changes: 9 additions & 0 deletions lib/puppet/type/ec2_instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,15 @@ def insync?(is)
end
end

newproperty(:termination_protection) do
desc 'Whether or not tom enable termination protection for this instance.'
defaultto :false
newvalues(:true, :'false')
def insync?(is)
is.to_s == should.to_s
end
end

newparam(:instance_initiated_shutdown_behavior) do
desc 'Whether the instance stops or terminates when you initiate shutdown from the instance.'
defaultto :stop
Expand Down