Skip to content

Commit bf51e23

Browse files
committed
init lambda component
0 parents  commit bf51e23

File tree

7 files changed

+288
-0
lines changed

7 files changed

+288
-0
lines changed

.gitignore

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
*.gem
2+
*.rbc
3+
/.config
4+
/coverage/
5+
/InstalledFiles
6+
/pkg/
7+
/spec/reports/
8+
/spec/examples.txt
9+
/test/tmp/
10+
/test/version_tmp/
11+
/tmp/
12+
13+
# Used by dotenv library to load environment variables.
14+
# .env
15+
16+
## Specific to RubyMotion:
17+
.dat*
18+
.repl_history
19+
build/
20+
*.bridgesupport
21+
build-iPhoneOS/
22+
build-iPhoneSimulator/
23+
24+
## Specific to RubyMotion (use of CocoaPods):
25+
#
26+
# We recommend against adding the Pods directory to your .gitignore. However
27+
# you should judge for yourself, the pros and cons are mentioned at:
28+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29+
#
30+
# vendor/Pods/
31+
32+
## Documentation cache and generated files:
33+
/.yardoc/
34+
/_yardoc/
35+
/doc/
36+
/rdoc/
37+
38+
## Environment normalization:
39+
/.bundle/
40+
/vendor/bundle
41+
/lib/bundler/man/
42+
43+
# for a library or gem, you might want to ignore these files since the code is
44+
# intended to run in multiple environments; otherwise, check them in:
45+
# Gemfile.lock
46+
# .ruby-version
47+
# .ruby-gemset
48+
49+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50+
.rvmrc
51+
52+
out/

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: ruby
2+
rvm:
3+
- 2.3
4+
script:
5+
- gem install cfhighlander
6+
- if [ "${TRAVIS_PULL_REQUEST}" != "false" ]; then cfhighlander cfcompile ; else cfhighlander cfcompile --validate; fi

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 theonestack
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
![build-status](https://travis-ci.com/theonestack/hl-component-lambda.svg?branch=master)
2+
3+
### Cfhighlander lambda component
4+
5+
```bash
6+
7+
# install highlander gem
8+
$ gem install cfhighlander
9+
10+
# build and validate standalone component
11+
$ cfcompile --validate
12+
13+
```
14+
15+
16+
### Parameters
17+
18+
TBD
19+
20+
### Configuration options
21+
22+
TBD
23+
24+
### Outputs
25+
26+
TBD

lambda.cfhighlander.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
CfhighlanderTemplate do
2+
Name 'lambda'
3+
ComponentVersion component_version
4+
Description "#{component_name} - #{component_version}"
5+
6+
functions.each do |function_name, lambda_config|
7+
if (lambda_config.has_key? 'enable_eni') && (lambda_config['enable_eni'])
8+
DependsOn 'vpc'
9+
break
10+
end
11+
end if defined? functions
12+
13+
Parameters do
14+
ComponentParam 'EnvironmentName', 'dev', isGlobal: true
15+
ComponentParam 'EnvironmentType', 'development', isGlobal: true, allowedValues: ['development', 'production']
16+
17+
functions.each do |function_name, lambda_config|
18+
if (lambda_config.has_key? 'enable_eni') && (lambda_config['enable_eni'])
19+
ComponentParam 'VPCId', type: 'AWS::EC2::VPC::Id'
20+
maximum_availability_zones.times do |az|
21+
ComponentParam "SubnetCompute#{az}"
22+
end
23+
break
24+
end
25+
end if defined? functions
26+
27+
end
28+
29+
end

lambda.cfndsl.rb

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
CloudFormation do
2+
3+
functions.each do |function_name, lambda_config|
4+
if (lambda_config.has_key? 'enable_eni') && (lambda_config['enable_eni'])
5+
az_conditions_resources('SubnetCompute', maximum_availability_zones)
6+
break
7+
end
8+
end if defined? functions
9+
10+
tags = []
11+
tags << { Key: 'Environment', Value: Ref(:EnvironmentName) }
12+
tags << { Key: 'EnvironmentType', Value: Ref(:EnvironmentType) }
13+
14+
extra_tags.each { |key,value| tags << { Key: key, Value: value } } if defined? extra_tags
15+
16+
functions.each do |function_name, lambda_config|
17+
18+
policies = []
19+
lambda_config['policies'].each do |name,policy|
20+
policies << iam_policy_allow(name,policy['action'],policy['resource'] || '*')
21+
end if lambda_config.has_key?('policies')
22+
23+
IAM_Role("#{function_name}Role") do
24+
AssumeRolePolicyDocument service_role_assume_policy('lambda')
25+
Path '/'
26+
Policies policies if policies.any?
27+
ManagedPolicyArns lambda_config['managed_policies'] if lambda_config.has_key?('managed_policies')
28+
end
29+
30+
if (lambda_config.has_key? 'enable_eni') && (lambda_config['enable_eni'])
31+
EC2_SecurityGroup("#{function_name}SecurityGroup") do
32+
GroupDescription FnSub("${EnvironmentName}-lambda-#{function_name}")
33+
VpcId Ref('VPCId')
34+
Tags tags
35+
end
36+
37+
Output("#{function_name}SecurityGroup") {
38+
Value(Ref("#{function_name}SecurityGroup"))
39+
Export FnSub("${EnvironmentName}-#{component_name}-#{function_name}SecurityGroup")
40+
}
41+
end
42+
43+
environment = lambda_config['environment'] || {}
44+
45+
# Create Lambda function
46+
Lambda_Function(function_name) do
47+
Code({
48+
S3Bucket: distribution['bucket'],
49+
S3Key: FnSub("#{distribution['prefix']}/#{lambda_config['code_uri']}")
50+
})
51+
52+
Environment(Variables: Hash[environment.collect { |k, v| [k, v] }])
53+
54+
Handler(lambda_config['handler'] || 'index.handler')
55+
MemorySize(lambda_config['memory'] || 128)
56+
Role(FnGetAtt("#{function_name}Role", 'Arn'))
57+
Runtime(lambda_config['runtime'])
58+
Timeout(lambda_config['timeout'] || 10)
59+
if (lambda_config.has_key? 'enable_eni') && (lambda_config['enable_eni'])
60+
VpcConfig({
61+
SecurityGroupIds: [
62+
Ref("#{function_name}SecurityGroup")
63+
],
64+
SubnetIds: az_conditional_resources('SubnetCompute', maximum_availability_zones)
65+
})
66+
end
67+
68+
if !lambda_config['named'].nil? && lambda_config['named']
69+
FunctionName(function_name)
70+
end
71+
Tags tags
72+
end
73+
74+
lambda_config['events'].each do |name,event|
75+
76+
case event['type']
77+
when 'schedule'
78+
Events_Rule("#{function_name}Schedule#{name}") do
79+
ScheduleExpression event['expression']
80+
State event['disable'] ? 'DISABLED' : 'ENABLED'
81+
target = {
82+
Arn: FnGetAtt(function_name, 'Arn'),
83+
Id: "lambda#{function_name}"
84+
}
85+
target['Input'] = event['payload'] if event.key?('payload')
86+
Targets([target])
87+
end
88+
89+
Lambda_Permission("#{function_name}Permissions") do
90+
FunctionName Ref(function_name)
91+
Action 'lambda:InvokeFunction'
92+
Principal 'events.amazonaws.com'
93+
SourceArn FnGetAtt("#{function_name}Schedule#{name}", 'Arn')
94+
end
95+
96+
when 'sns'
97+
SNS_Topic("#{function_name}Sns#{name}") do
98+
Subscription([
99+
{
100+
Endpoint: FnGetAtt(function_name, 'Arn'),
101+
Protocol: 'lambda'
102+
}
103+
])
104+
end
105+
106+
Lambda_Permission("#{function_name}Permissions") do
107+
FunctionName Ref(function_name)
108+
Action 'lambda:InvokeFunction'
109+
Principal 'sns.amazonaws.com'
110+
SourceArn Ref("#{function_name}Sns#{name}")
111+
end
112+
end
113+
114+
end if lambda_config.has_key?('events')
115+
116+
end if defined? functions
117+
118+
119+
120+
end

lambda.config.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
component_version: 1.0.0
2+
maximum_availability_zones: 5
3+
4+
# Demo Config
5+
6+
# distribution:
7+
# bucket: source.example.dev
8+
# key: /lambda
9+
#
10+
# functions:
11+
# myfunction:
12+
# handler: handler.lambda_handler
13+
# runtime: python3.6
14+
# code_uri: myfunction/${MyFunctionVersion}/src.zip
15+
# timeout: 30
16+
# environment:
17+
# Environment:
18+
# Ref: Environment
19+
# policies:
20+
# logs:
21+
# Action:
22+
# - logs:PutLogEvents
23+
# - logs:DescribeLogStreams
24+
# - logs:DescribeLogGroups
25+
# Resource:
26+
# - '*'
27+
# enable_eni: true
28+
# events:
29+
# cron:
30+
# type: schedule
31+
# expression: cron(0 12 * * ? *)
32+
# payload: "{ 'a': 1, 'b': 2 }"
33+
# trigger:
34+
# type: sns

0 commit comments

Comments
 (0)