-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlambda.cfndsl.rb
149 lines (118 loc) · 4.86 KB
/
lambda.cfndsl.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
CloudFormation do
functions.each do |function_name, lambda_config|
if (lambda_config.has_key? 'enable_eni') && (lambda_config['enable_eni'])
az_conditions_resources('SubnetCompute', maximum_availability_zones)
break
end
end if defined? functions
tags = []
tags << { Key: 'Environment', Value: Ref(:EnvironmentName) }
tags << { Key: 'EnvironmentType', Value: Ref(:EnvironmentType) }
extra_tags.each { |key,value| tags << { Key: key, Value: value } } if defined? extra_tags
functions.each do |function_name, lambda_config|
policies = []
lambda_config['policies'].each do |name,policy|
policies << iam_policy_allow(name,policy['action'],policy['resource'] || '*')
end if lambda_config.has_key?('policies')
IAM_Role("#{function_name}Role") do
AssumeRolePolicyDocument service_role_assume_policy('lambda')
Path '/'
Policies policies if policies.any?
ManagedPolicyArns lambda_config['managed_policies'] if lambda_config.has_key?('managed_policies')
end
if (lambda_config.has_key? 'enable_eni') && (lambda_config['enable_eni'])
EC2_SecurityGroup("#{function_name}SecurityGroup") do
GroupDescription FnSub("${EnvironmentName}-lambda-#{function_name}")
VpcId Ref('VPCId')
Tags tags
end
Output("#{function_name}SecurityGroup") {
Value(Ref("#{function_name}SecurityGroup"))
Export FnSub("${EnvironmentName}-#{component_name}-#{function_name}SecurityGroup")
}
end
environment = lambda_config['environment'] || {}
# Create Lambda function
Lambda_Function(function_name) do
Code({
S3Bucket: FnSub(distribution['bucket']),
S3Key: FnSub("#{distribution['prefix']}/#{lambda_config['code_uri']}")
})
Environment(Variables: Hash[environment.collect { |k, v| [k, v] }])
Handler(lambda_config['handler'] || 'index.handler')
MemorySize(lambda_config['memory'] || 128)
Role(FnGetAtt("#{function_name}Role", 'Arn'))
Runtime(lambda_config['runtime'])
Timeout(lambda_config['timeout'] || 10)
if (lambda_config.has_key? 'enable_eni') && (lambda_config['enable_eni'])
VpcConfig({
SecurityGroupIds: [
Ref("#{function_name}SecurityGroup")
],
SubnetIds: az_conditional_resources('SubnetCompute', maximum_availability_zones)
})
end
if !lambda_config['named'].nil? && lambda_config['named']
FunctionName(function_name)
end
Tags tags
end
Output("#{function_name}Arn") {
Value FnGetAtt(function_name , 'Arn')
Export FnSub("${EnvironmentName}-#{external_parameters[:component_name]}-#{function_name}-arn")
}
Logs_LogGroup("#{function_name}LogGroup") do
LogGroupName FnSub("/aws/lambda/${EnvironmentName}/#{function_name}")
RetentionInDays lambda_config['log_retention'] if lambda_config.has_key? 'log_retention'
end
lambda_config['events'].each do |name,event|
case event['type']
when 'schedule'
Events_Rule("#{function_name}Schedule#{name}") do
ScheduleExpression event['expression']
State event['disable'] ? 'DISABLED' : 'ENABLED'
target = {
Arn: FnGetAtt(function_name, 'Arn'),
Id: "lambda#{function_name}"
}
target['Input'] = event['payload'] if event.key?('payload')
Targets([target])
end
Lambda_Permission("#{function_name}#{name}Permissions") do
FunctionName Ref(function_name)
Action 'lambda:InvokeFunction'
Principal 'events.amazonaws.com'
SourceArn FnGetAtt("#{function_name}Schedule#{name}", 'Arn')
end
when 'sns'
SNS_Topic("#{function_name}Sns#{name}") do
Subscription([
{
Endpoint: FnGetAtt(function_name, 'Arn'),
Protocol: 'lambda'
}
])
end
Lambda_Permission("#{function_name}#{name}Permissions") do
FunctionName Ref(function_name)
Action 'lambda:InvokeFunction'
Principal 'sns.amazonaws.com'
SourceArn Ref("#{function_name}Sns#{name}")
end
when 'filter'
Logs_SubscriptionFilter("#{function_name}SubscriptionFilter#{name}") do
DestinationArn FnGetAtt(function_name, 'Arn')
FilterPattern event['pattern']
LogGroupName Ref(event['log_group'])
end
Lambda_Permission("#{function_name}#{name}Permissions") do
FunctionName Ref(function_name)
Action 'lambda:InvokeFunction'
Principal FnSub('logs.${AWS::Region}.amazonaws.com')
SourceAccount Ref('AWS::AccountId')
SourceArn FnSub("arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/#{event['log_group']}:*")
end
end
end if lambda_config.has_key?('events')
end if defined? functions
end