#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import os
import click
import aws_lambda
CURRENT_DIR = os.getcwd()
logging.getLogger("pip").setLevel(logging.CRITICAL)
@click.group()
def cli():
pass
@click.command(help="Create a new function for Lambda.")
@click.option(
"--minimal",
default=False,
is_flag=True,
help="Exclude any unnecessary template files",
)
@click.argument(
"folder",
nargs=-1,
type=click.Path(file_okay=False, writable=True),
)
def init(folder, minimal):
path = CURRENT_DIR
if len(folder) > 0:
path = os.path.join(CURRENT_DIR, *folder)
if not os.path.exists(path):
os.makedirs(path)
aws_lambda.init(path, minimal=minimal)
@click.command(help="Bundles package for deployment.")
@click.option(
"--config-file",
default="config.yaml",
help="Alternate config file.",
)
@click.option(
"--profile",
help="AWS profile to use.",
)
@click.option(
"--requirements",
default=None,
type=click.Path(),
help="Install packages from supplied requirements file.",
)
@click.option(
"--local-package",
default=None,
type=click.Path(),
help="Install local package as well.",
multiple=True,
)
def build(requirements, local_package, config_file, profile):
aws_lambda.build(
CURRENT_DIR,
requirements=requirements,
local_package=local_package,
config_file=config_file,
profile_name=profile,
)
@click.command(help="Runs subprocess to build docker images for lambda")
@click.option(
"--profile",
help="AWS profile to use.",
)
@click.option(
"--config-file",
default="config.yaml",
help="Alternate config file.",
)
def build_image(config_file, profile):
aws_lambda.build_image(
CURRENT_DIR,
config_file=config_file,
profile_name=profile,
)
@click.command(help="Retags previous docker image for ECR")
@click.option(
"--profile",
help="AWS profile to use.",
)
@click.option(
"--config-file",
default="config.yaml",
help="Alternate config file.",
)
@click.option(
"--local-image",
default=None,
help="Local image to tag. Will find image from --tag in image_build_variables in yaml if not provided.",
)
@click.option(
"--lambda-image-tag",
default=None,
help="Tag to use for creating the ECR URI. Will find tag from image_build_variables in yaml if not provided.",
)
def tag_image(config_file, profile, local_image, lambda_image_tag):
aws_lambda.tag_image(
CURRENT_DIR,
config_file=config_file,
profile_name=profile,
local_image=local_image,
lambda_image_tag=lambda_image_tag,
)
@click.command(help="Pushes docker image to ECR")
@click.option(
"--profile",
help="AWS profile to use.",
)
@click.option(
"--config-file",
default="config.yaml",
help="Alternate config file.",
)
@click.option(
"--lambda-image-uri",
default=None,
help="Image uri to push. If not provided, will use lambda_image_uri from config.yaml or build from other variables.",
)
@click.option(
"--lambda-image-tag",
default=None,
help="Image tag to push, rest of image uri will be built from other variables.",
)
def push_image(config_file, profile):
aws_lambda.push_image(
CURRENT_DIR,
config_file=config_file,
profile_name=profile,
lambda_image_uri=None,
lamba_image_tag=None,
)
@click.command(help="Run a local test of your function.")
@click.option(
"--event-file",
default="event.json",
help="Alternate event file.",
)
@click.option(
"--config-file",
default="config.yaml",
help="Alternate config file.",
)
@click.option(
"--profile",
help="AWS profile to use.",
)
@click.option("--verbose", "-v", is_flag=True)
def invoke(event_file, config_file, profile, verbose):
aws_lambda.invoke(
CURRENT_DIR,
event_file=event_file,
config_file=config_file,
profile_name=profile,
verbose=verbose,
)
@click.command(help="Register and deploy your code to lambda.")
@click.option(
"--config-file",
default="config.yaml",
help="Alternate config file.",
)
@click.option(
"--profile",
help="AWS profile to use.",
)
@click.option(
"--requirements",
default=None,
type=click.Path(),
help="Install all packages defined in supplied requirements file",
)
@click.option(
"--local-package",
default=None,
type=click.Path(),
help="Install local package as well.",
multiple=True,
)
@click.option(
"--preserve-vpc",
default=False,
is_flag=True,
help="Preserve VPC configuration on existing functions",
)
def deploy(requirements, local_package, config_file, profile, preserve_vpc):
aws_lambda.deploy(
CURRENT_DIR,
requirements=requirements,
local_package=local_package,
config_file=config_file,
profile_name=profile,
preserve_vpc=preserve_vpc,
)
@click.command(help=("Deploy docker image to lambda."))
@click.option(
"--config-file",
default="config.yaml",
help="Alternate config file.",
)
@click.option(
"--profile",
help="AWS profile to use.",
)
@click.option(
"--preserve-vpc",
default=False,
is_flag=True,
help="Preserve VPC configuration on existing functions",
)
@click.option(
"--lambda-image-uri",
default=None,
help="Image uri to deploy with. If not provided, will use lambda_image_uri from config.yaml or build from other variables.",
)
@click.option(
"--lambda-image-tag",
default=None,
help="Image tag to deploy with, rest of image uri will be built from other variables.",
)
def deploy_image(
config_file, profile, preserve_vpc, lambda_image_uri, lambda_image_tag
):
aws_lambda.deploy_image(
CURRENT_DIR,
config_file=config_file,
profile_name=profile,
preserve_vpc=preserve_vpc,
lambda_image_uri=lambda_image_uri,
lambda_image_tag=lambda_image_tag,
)
@click.command(help="Upload your lambda to S3.")
@click.option(
"--config-file",
default="config.yaml",
help="Alternate config file.",
)
@click.option(
"--profile",
help="AWS profile to use.",
)
@click.option(
"--requirements",
default=None,
type=click.Path(),
help="Install all packages defined in supplied requirements file",
)
@click.option(
"--local-package",
default=None,
type=click.Path(),
help="Install local package as well.",
multiple=True,
)
def upload(requirements, local_package, config_file, profile):
aws_lambda.upload(
CURRENT_DIR,
requirements=requirements,
local_package=local_package,
config_file=config_file,
profile_name=profile,
)
@click.command(help="Deploy your lambda via S3.")
@click.option(
"--config-file",
default="config.yaml",
help="Alternate config file.",
)
@click.option(
"--profile",
help="AWS profile to use.",
)
@click.option(
"--requirements",
default=None,
type=click.Path(),
help="Install all packages defined in supplied requirements file",
)
@click.option(
"--local-package",
default=None,
type=click.Path(),
multiple=True,
help="Install local package as well.",
)
def deploy_s3(requirements, local_package, config_file, profile):
aws_lambda.deploy_s3(
CURRENT_DIR,
requirements=requirements,
local_package=local_package,
config_file=config_file,
profile_name=profile,
)
@click.command(help="Delete old versions of your functions")
@click.option(
"--config-file",
default="config.yaml",
help="Alternate config file.",
)
@click.option(
"--profile",
help="AWS profile to use.",
)
@click.option(
"--keep-last",
type=int,
prompt="Please enter the number of recent versions to keep",
)
def cleanup(keep_last, config_file, profile):
aws_lambda.cleanup_old_versions(
CURRENT_DIR,
keep_last,
config_file=config_file,
profile_name=profile,
)
if __name__ == "__main__":
cli.add_command(init)
cli.add_command(invoke)
cli.add_command(deploy)
cli.add_command(deploy_image)
cli.add_command(upload)
cli.add_command(deploy_s3)
cli.add_command(build)
cli.add_command(build_image)
cli.add_command(tag_image)
cli.add_command(push_image)
cli.add_command(cleanup)
cli()