Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for go1.x lambda runtime #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v0.1.x
* Add support for go1.x lambda runtime
* Fix support for deploying lambda via S3 bucket

# v0.1.2
* Fix property namespaces, function/handler, and include role (#2)

Expand Down
33 changes: 21 additions & 12 deletions lib/sfn-lambda/control.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ class Control

DEFAULTS = {
:INLINE_MAX_SIZE => 4096,
:INLINE_RESTRICTED => ['java8'].freeze,
:INLINE_RESTRICTED => ['java8', 'go1.x'].freeze,
:BUILD_REQUIRED => {
'java8' => {
:build_command => 'mvn package',
:output_directory => './target',
:asset_extension => '.jar'
:asset_extension => 'jar'
}.freeze,
'go1.x' => {
:build_command => 'GOOS=linux CGO_ENABLED=0 GOARCH=amd64 go build -ldflags="-s -w" -o target/ && zip -j -r target/main.zip target/*',
:output_directory => './target',
:asset_extension => 'zip'
}.freeze
}.freeze
}.freeze
Expand Down Expand Up @@ -86,22 +91,25 @@ def format_content(info)
Smash.new(:raw => File.read(info[:path]))
else
apply_build!(info)
key_name = generate_key_name(info)
io = File.open(info[:path], 'rb')
file = bucket.files.build
file.name = key_name
file.name = generate_key_name(info)
file.body = io
file.save
io.close

version = nil
if(versioning_enabled?)
s3 = callback.api.connection.api_for(:storage)
result = s3.request(
:path => s3.file_path(file),
:path => s3.file_path(file.name),
:endpoint => s3.bucket_endpoint(file.bucket),
:method => :head
)
version = result[:headers][:x_amz_version_id]
end
Smash(:bucket => storage_bucket, :key => key_name, :version => version)

Smash.new(:bucket => bucket.name, :key => file.name, :version => version)
end
end

Expand All @@ -121,12 +129,12 @@ def apply_build!(info)
raise "Failed to build lambda asset for storage! (path: `#{info[:path]}`)"
end
end
file = Dir.glob(File.join(info[:path], build_info[:output_directory], "*.#{build_config[:asset_extension]}")).first
file = Dir.glob(File.join(info[:path], build_info[:output_directory], "*.#{build_info[:asset_extension]}")).first
if(file)
info[:path] = file
true
else
debug "Glob pattern used for build asset detection: `#{File.join(info[:path], build_info[:output_directory], "*.#{build_config[:asset_extension]}")}`"
callback.ui.debug "Glob pattern used for build asset detection: `#{File.join(info[:path], build_info[:output_directory], "*.#{build_info[:asset_extension]}")}`"
raise "Failed to locate generated build asset for storage! (path: `#{info[:path]}`)"
end
else
Expand All @@ -138,7 +146,7 @@ def apply_build!(info)
def bucket
storage_bucket = callback.config.fetch(:lambda, :upload, :bucket, callback.config[:nesting_bucket])
if(storage_bucket)
s3 = api.connection.api_for(:storage)
s3 = callback.api.connection.api_for(:storage)
l_bucket = s3.buckets.get(storage_bucket)
end
unless(l_bucket)
Expand All @@ -150,8 +158,8 @@ def bucket

# @return [TrueClass, FalseClass] bucket has versioning enabled
def versioning_enabled?
unless(@versioned.nil?)
s3 = api.connection.api_for(:storage)
if(@versioned.nil?)
s3 = callback.api.connection.api_for(:storage)
result = s3.request(
:path => '/',
:params => {
Expand All @@ -178,7 +186,8 @@ def generate_key_name(info)
checksum << content
end
end
"sfn.lambda/#{info[:runtime]}/#{File.basename(info[:path])}-#{checksum.base64digest}"
checksum = Base64.urlsafe_encode64(checksum.digest)
"sfn.lambda/#{info[:runtime]}/#{info[:name]}/#{checksum}/#{File.basename(info[:path])}"
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/sfn-lambda/inject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ def _lambda(*fn_args)
new_fn.properties.handler fn_handler
new_fn.properties.runtime lookup[:runtime]
new_fn.properties.function_name fn_function_name || fn_name
new_fn.properties.role fn_role
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving this up so we can set the role even when the lambda function can't be inlined

content = ::SfnLambda.control.format_content(lookup)
if(content[:raw])
new_fn.properties.code.zip_file content[:raw]
new_fn.properties.handler "index.#{fn_handler}"
new_fn.properties.role fn_role
else
new_fn.properties.code.s3_bucket content[:bucket]
new_fn.properties.code.s3_key content[:key]
Expand Down