-
Notifications
You must be signed in to change notification settings - Fork 1
/
lambda_function.rb
84 lines (67 loc) · 3.02 KB
/
lambda_function.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# frozen_string_literal: true
# Load all Ruby Gems stored in the local ./vendor/bundle directory
load_paths = Dir['./vendor/bundle/ruby/**/gems/**/lib']
$LOAD_PATH.unshift(*load_paths)
require 'aws_cloudformation_helper'
require 'securerandom'
require_relative './aws/cli.rb'
require_relative './aws/eks/actions.rb'
require_relative './aws/eks/cluster.rb'
require_relative './aws/eks/kubeconfig.rb'
require_relative './aws/lambda/helper.rb'
require_relative './kubernetes/kubectl.rb'
def create
raise "Cluster #{@cluster_name} is not in an active state or does not exist." unless @cluster.active?
initialize_kubectl
apply_configuration
end
def delete
@cfn_helper.logger.info('Delete event is not implemented for this resource. Skipping.')
end
def update
raise "Cluster #{@cluster_name} is not in an active state or does not exist." unless @cluster.active?
initialize_kubectl
apply_configuration
end
def apply_configuration
if @cfn_helper.event.resource_properties.include?('Action')
actions = AWS::EKS::Actions.new(@cfn_helper, @kubectl)
actions.execute_action(@cfn_helper.event.resource_properties['Action'].to_s.strip)
else
if @cfn_helper.event.resource_properties.include?('ConfigMapURL')
@config_map_file = @cfn_helper.event.resource_properties['ConfigMapURL']
@cfn_helper.logger.info("Using Config Map file from URL #{@config_map_file}")
elsif @cfn_helper.event.resource_properties.include?('ConfigMap')
@config_yaml = @cfn_helper.event.resource_properties['ConfigMap']
@cfn_helper.logger.info("Config Map YAML: \n#{@config_yaml}")
write_yaml_config_file
end
@kubectl.apply(@config_map_file)
end
end
def initialize_kubectl
return unless @cluster.exist?
kubeconfig = AWS::EKS::Kubeconfig.new(@cfn_helper)
kubectl_config_file = kubeconfig.generate_kubeconfig(@cluster_name)
@kubectl = Kubernetes::Kubectl.new(@cfn_helper, kubectl_config_file)
end
def write_yaml_config_file
@cfn_helper.logger.info("Writing k8s cluster config map file to #{@config_map_file}...")
::File.open(@config_map_file, 'w') { |file| file.write(@config_yaml) }
raise 'Config map file failed to write to filesystem.' unless ::File.exist?(@config_map_file)
end
def lambda_handler(event:, context:)
# Initializes CloudFormation Helper library
@cfn_helper = AWS::CloudFormation::Helper.new(self, event, context)
# Add additional initialization code here
@cfn_helper.logger.info("Begin execution for CloudFormation resource #{@cfn_helper.event.logical_resource_id}")
@config_map_file = "/tmp/k8s-config-map-#{::SecureRandom.uuid}.yml"
@cluster_name = @cfn_helper.event.resource_properties['ClusterName']
@cfn_helper.logger.info("Cluster Name: #{@cluster_name}")
@cluster = AWS::EKS::Cluster.new(@cfn_helper, @cluster_name)
lambda_helper = AWS::Lambda::Helper.new
# Executes the event method
@cfn_helper.event.execute
@cfn_helper.logger.info("Execution completed for CloudFormation resource #{@cfn_helper.event.logical_resource_id}")
lambda_helper.success('Completed successfully.')
end