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 the ability to customize lambda function upload name #49

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
20 changes: 20 additions & 0 deletions docs/lambdas.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ The following is the anatomy of a lambda in gordon.
timeout: { NUMBER }
runtime: { RUNTIME_NAME }
description: { STRING }
function-name: { STRING }
build: { STRING }
role: { MAP }
vpc: { STRING }
Expand Down Expand Up @@ -307,6 +308,25 @@ Description Human-readable description for your lambda.
description: This is a really simple function which says hello


function-name
^^^^^^^^^^^^^^^^^^^^^^

=========================== ============================================================================================================
Name ``function-name``
Required No
Default *Empty*
Valid types ``string``, ``reference``
Description Base function name to upload to AWS
=========================== ============================================================================================================

.. code-block:: yaml

lambdas:
hello_world:
code: functions.py
function-name: goodbye_world


.. _lambda-build:

build
Expand Down
4 changes: 4 additions & 0 deletions gordon/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,7 @@ class LambdaNotFound(BaseGordonException):
class ValidationError(BaseGordonException):
hint = u" Validation Error: {}"
code = 24

class InvalidLambdaFunctionName(BaseGordonException):
hint = u"Lambda {} cannot have name {}"
code = 25
45 changes: 45 additions & 0 deletions gordon/resources/lambdas.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,48 @@ def get_role(self):
Policies=self._get_policies()
)

def get_function_name(self):
"""Returns the optional FunctionName this lambda should use for
deployment. If the ``function-name` field is provided in the lambda's
settings, the name uploaded will be in the format of:
"Fn::Join": [
"-",
[
``function-name``,
{
"Ref":"Stage"
},
{
"Ref":"AWS::Region"
}
]
]
``function-name`` can be a ref or string"""

function_name = self.settings.get('function-name')

def _join_env_refs(*values):
values = list(values)
values.append(troposphere.Ref("Stage"))
values.append(troposphere.Ref(troposphere.AWS_REGION))
return troposphere.Join("-", values)

if isinstance(function_name, six.string_types):
return _join_env_refs(
utils.valid_cloudformation_name(self.app.name),
utils.valid_cloudformation_name(function_name))
elif isinstance(function_name, troposphere.Ref):
return _join_env_refs(
utils.valid_cloudformation_name(self.app.name),
function_name)
elif function_name is None:
pass
else:
raise exceptions.InvalidLambdaFunctionName(self.name, function_name)




def get_bucket_key(self):
"""Return the S3 bucket key for this lambda."""
filename = '_'.join(self.in_project_name.split(':')[1:])
Expand Down Expand Up @@ -258,6 +300,9 @@ def register_resources_template(self, template):
SecurityGroupIds=vpc.settings['security-groups'],
SubnetIds=vpc.settings['subnet-ids']
)
function_name = self.get_function_name()
if function_name is not None:
extra["FunctionName"] = function_name

function = template.add_resource(
awslambda.Function(
Expand Down