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

Support ECR lambda docker images in lambda-layer-deps, working example of 'mono-repo' #24

Open
wants to merge 2 commits into
base: main
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
2 changes: 2 additions & 0 deletions modules/lambda-layer-deps/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
builds/
package.log
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
provider "aws" {
profile = "zamboni-development"
region = "us-west-1"
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
provider "aws" {
profile = "zamboni-development"
region = "us-west-1"
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[virtualenvs]
create = true
in-project = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[tool.poetry]
name = "shared"
version = "0.1.0"
description = ""
authors = ["Adam Tistler <[email protected]>"]

[tool.poetry.dependencies]
python = "^3.8"
requests = "^2.28.2"

[tool.poetry.dev-dependencies]
pytest = "^7.0.1"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
provider "aws" {
region = "us-west-1"
}

module "layer" {
source = "../../../../"
layer_name = "python-poetry-with-shared-package"
dependency_lock_file_path = "${path.module}/../../poetry.lock"
dependency_manager = "poetry"
use_ecr_image = true
}

module "lambda_function" {
source = "terraform-aws-modules/lambda/aws"
function_name = "python-poetry-with-shared-package"
description = "Python poetry with shared package example"
handler = "index.lambda_handler"
runtime = "python3.8"
source_path = "./src/"
layers = [module.layer.arn]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from shared import myip

def lambda_handler(event, context):
return myip.get()
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import requests

def get():
return requests.get('https://api.ipify.org?format=json').json()
1 change: 1 addition & 0 deletions modules/lambda-layer-deps/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ data "external" "build" {
dependency_manager = var.dependency_manager
runtime = var.runtime
dependency_lock_file = var.dependency_lock_file_path
use_ecr_image = var.use_ecr_image
pre_package_commands = jsonencode(var.pre_package_commands)
}
}
19 changes: 11 additions & 8 deletions modules/lambda-layer-deps/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,19 @@ def generate(self):

class Workflow(ABC):
def __init__(self, runtime, dependency_lock_file, dependency_file, docker_image=None,
pre_package_commands=[]):
pre_package_commands=[], use_ecr_image=False):
self.runtime = runtime
self.docker_image = docker_image
self.pre_package_commands = pre_package_commands
self.dependency_lock_file = dependency_lock_file
self.package_dir = path.dirname(self.dependency_lock_file)
self.dependency_file = path.join(self.package_dir, dependency_file)
self.build_dir = './builds'
self.use_ecr_image = use_ecr_image
if docker_image:
self.docker_image = docker_image
elif self.use_ecr_image:
self.docker_image = 'public.ecr.aws/sam/build-{}'.format(self.runtime)
else:
self.docker_image = 'lambci/lambda:build-{}'.format(self.runtime)

Expand Down Expand Up @@ -174,8 +177,8 @@ def install(self):


class PoetryWorkflow(Workflow):
def __init__(self, runtime, dependency_lock_file, docker_image=None, pre_package_commands=[]):
super().__init__(runtime, dependency_lock_file, 'pyproject.toml', docker_image, pre_package_commands)
def __init__(self, runtime, dependency_lock_file, docker_image=None, pre_package_commands=[], use_ecr_image=False):
super().__init__(runtime, dependency_lock_file, 'pyproject.toml', docker_image, pre_package_commands, use_ecr_image)

@property
def pip_cmd(self):
Expand All @@ -197,8 +200,8 @@ def get_runtime_dir(self, build_layer_dir):


class NpmWorkflow(Workflow):
def __init__(self, runtime, dependency_lock_file, docker_image=None, pre_package_commands=[]):
super().__init__(runtime, dependency_lock_file, 'package.json', docker_image, pre_package_commands)
def __init__(self, runtime, dependency_lock_file, docker_image=None, pre_package_commands=[], use_ecr_image=False):
super().__init__(runtime, dependency_lock_file, 'package.json', docker_image, pre_package_commands, use_ecr_image)

def install(self):
install_cmd = f'cd /var/task && npm install --production'
Expand All @@ -211,8 +214,8 @@ def get_runtime_dir(self, build_layer_dir):


class YarnWorkflow(Workflow):
def __init__(self, runtime, dependency_lock_file, docker_image=None, pre_package_commands=[]):
super().__init__(runtime, dependency_lock_file, 'package.json', docker_image, pre_package_commands)
def __init__(self, runtime, dependency_lock_file, docker_image=None, pre_package_commands=[], use_ecr_image=False):
super().__init__(runtime, dependency_lock_file, 'package.json', docker_image, pre_package_commands, use_ecr_image)

def install(self):
install_cmd = f'cd /var/task && yarn install --production'
Expand All @@ -238,7 +241,7 @@ def main():

archive_file = workflow(
query["runtime"], query["dependency_lock_file"], query.get("docker_image"),
json.loads(query.get("pre_package_commands"))).run()
json.loads(query.get("pre_package_commands")), query.get("use_ecr_image")).run()

print(json.dumps({'output_path': archive_file}))

Expand Down
6 changes: 6 additions & 0 deletions modules/lambda-layer-deps/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,10 @@ variable "pre_package_commands" {
description = "Command to run on docker image before packaging step"
type = list(string)
default = []
}

variable "use_ecr_image" {
description = "By default this module will use lambci docker image, if you would like to use ECR lambda images set this option to true (recommended)"
type = bool
default = false
}