Skip to content

Instantly share code, notes, and snippets.

@troyready
Created September 9, 2020 00:40
Show Gist options
  • Save troyready/b7af7424e05d57d37f273e9ade53f1b6 to your computer and use it in GitHub Desktop.
Save troyready/b7af7424e05d57d37f273e9ade53f1b6 to your computer and use it in GitHub Desktop.
Runway module using locally stored lookups to get the current lambda function s3 key

Overview

.
├── dev-us-east-1.env
├── local_lookups
│   ├── awslambda.py
│   └── __init__.py
├── my_function
│   ├── index.py
│   └── __init__.py
├── stacks.yaml
└── templates
    └── function.yaml

Instructions

  1. Create the local_lookups directory into your CFN module, with an empty __init__.py file and the awslambda.py file detailed below.
  2. Ensure your stack configuration module has:
  • sys_path: ./ set
  • The 2 custom lookups (lambda_upload_bucket & lambda_upload_key) defined
  1. Use the lookups in conjunction with the upload_lambda_functions hook as shown in the example stacks.yaml config.

In this example, the function MyFunction is uploaded from the directory my_function with its upload information stored in the "data_key" lambda. The upload information is then referenced for the LambdaBucket/LambdaKey parameters in the form of data_key::function_name

#!/usr/bin/env python
"""Helper classes for Runway CFN AWS Lambda hook."""
def bucket(value, context, **kwargs): # pylint: disable=unused-argument
"""Extract the S3 bucket for a given package in hook_data.
Format of value:
<hook_name>::<key>
"""
try:
hook_name, hook_key = value.split("::")
except ValueError:
raise ValueError("Invalid value for hook_data: %s. Must be in "
"<hook_name>::<key> format." % value)
code = context.hook_data[hook_name][hook_key]
return code.to_dict()['S3Bucket']
def key(value, context, **kwargs): # pylint: disable=unused-argument
"""Extract the S3 key for a given package in hook_data.
Format of value:
<hook_name>::<key>
"""
try:
hook_name, hook_key = value.split("::")
except ValueError:
raise ValueError("Invalid value for hook_data: %s. Must be in "
"<hook_name>::<key> format." % value)
code = context.hook_data[hook_name][hook_key]
return code.to_dict()['S3Key']
namespace: ${namespace}
cfngin_bucket: ${cfngin_bucket_name}
sys_path: ./
lookups:
lambda_upload_bucket: local_lookups.awslambda.bucket
lambda_upload_key: local_lookups.awslambda.key
pre_build:
- path: runway.cfngin.hooks.aws_lambda.upload_lambda_functions
required: true
data_key: lambda
args:
functions:
MyFunction:
path: ./my_function
include:
- '*.py'
- '*.txt'
exclude:
- '*.pyc'
- test/
stacks:
- name: functionstack
template_path: templates/function.yaml
variables:
LambdaRole: arn...REPLACEME
LambdaBucket: ${lambda_upload_bucket lambda::MyFunction}
LambdaKey: ${lambda_upload_key lambda::MyFunction}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment