Skip to main content

Hatch plugin for building AWS Lambda functions with SAM

Project description

hatch-aws

PyPI - Version PyPI - Python Version Hatch project code style - black types - mypy imports - isort

AWS builder plugin for Hatch ๐Ÿฅš๐Ÿ. Hatch is modern, extensible Python project manager.


Checkout my other plugin hatch-aws-publisher.

Table of Contents

Global dependency

Add hatch-aws within the build-system.requires field in your pyproject.toml file.

[build-system]
requires = ["hatchling", "hatch-aws"]
build-backend = "hatchling.build"

Builder

The builder plugin name is called aws.

To start build process, run hatch build -t aws:

โฏ hatch build -t aws
[aws]
Building lambda functions ...
MyAwsLambdaFunc ... success
Build successfull ๐Ÿš€
/path/to/build/.aws-sam/build

How to use it

  1. Put your module and lambdas inside of src folder.

    .
    โ”œโ”€โ”€ pyproject.toml
    โ”œโ”€โ”€ src
    โ”‚   โ””โ”€โ”€ my_app
    โ”‚       โ”œโ”€โ”€ __init__.py
    โ”‚       โ”œโ”€โ”€ common
    โ”‚       โ”‚   โ”œโ”€โ”€ __init__.py
    โ”‚       โ”‚   โ”œโ”€โ”€ config.py
    โ”‚       โ”‚   โ””โ”€โ”€ models.py
    โ”‚       โ””โ”€โ”€ lambdas
    โ”‚           โ”œโ”€โ”€ lambda1
    โ”‚           โ”‚   โ”œโ”€โ”€ __init__.py
    โ”‚           โ”‚   โ””โ”€โ”€ main.py
    โ”‚           โ””โ”€โ”€ lambda2
    โ”‚               โ”œโ”€โ”€ __init__.py
    โ”‚               โ””โ”€โ”€ main.py
    โ””โ”€โ”€ template.yml
    
  2. Specify common requirements for your project in pyproject.toml as dependencies.

    [project]
    dependencies = ["boto3"]
    
  3. Specify requirements for your lambda functions in pyproject.toml as optional dependencies. Use resource name from SAM template, but you have to adapt it to be compliant with PEP standard (transform to lower case and replace _ with -). For example, if you function name in SAM template is GetAll_Accounts, use getall-accounts.

    [project.optional-dependencies]
    lambda1 = ["pyaml"]
    lambda2 = ["request", "pydantic"]
    
  4. Specify additional paths(source/destination) you want to copy to the build folder. Destination is relative to a build directory (.aws-sam/build by default). You can use glob * to copy common to all lambda functions.

    [tool.hatch.build.force-include]
    "src/batman/common" = "*/batman/common" # copy to all lambda functions
    ".editorconfig" = ".editorconfig.txt"
    "CHANGELOG.md" = "../CH.txt"
    "images/" = "*/images"
    
  5. Set the CodeUri and Handler parameter pointing to your lambdas in SAM template. Only resources with Runtime: python{version} are supported. The rest is ignored.

    Resources:
     Lambda1:
       Type: AWS::Serverless::Function
       Properties:
         Runtime: python3.9
         FunctionName: lambda1-function
         CodeUri: src
         Handler: my_app.lambdas.lambda1.main.app
         ...
    
     Lambda2:
       Type: AWS::Serverless::Function
       Properties:
         Runtime: python3.9
         FunctionName: lambda2-function
         CodeUri: src
         Handler: my_app.lambdas.lambda2.main.app
         ...
    

Options

Following table contains available customization of builder behavior. You can find example of pyproject.toml in tests/assets/pyproject.toml.

Option Type Default Description
template str template.yml SAM template filename.
use-sam bool false Use only sam build command without any custom actions.
sam-exec str sam Path to sam executable. Env var: HATCH_SAM_EXEC.
sam-params array Additional sam build args.

License

Plugin hatch-aws is distributed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hatch_aws-1.1.0.tar.gz (9.4 kB view hashes)

Uploaded Source

Built Distribution

hatch_aws-1.1.0-py3-none-any.whl (8.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page