Skip to content

Commit

Permalink
chore: Improve readme with more examples (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
kichik authored Jan 5, 2024
1 parent 2799046 commit edce0fb
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 4 deletions.
68 changes: 66 additions & 2 deletions API.md

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

68 changes: 66 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ The best way to browse API documentation is on [Constructs Hub][13]. It is avail
dotnet add package CloudSnorkel.Cdk.TurboLayers
```
## Example
## Examples
The very basic example below will create a layer with dependencies specified as parameters and attach it to a Lambda function.
```typescript
const packager = new PythonDependencyPackager(this, 'Packager', {
Expand All @@ -91,15 +93,77 @@ new Function(this, 'Function with inline requirements', {
// this will create a layer from with requests and Scrapy in a Lambda function instead of locally
layers: [packager.layerFromInline('inline requirements', ['requests', 'Scrapy'])],
});
```

The next example will create a layer with dependencies specified in a `requirements.txt` file and attach it to a Lambda function.

```typescript
const packager = new PythonDependencyPackager(this, 'Packager', {
runtime: lambda.Runtime.PYTHON_3_9,
type: DependencyPackagerType.LAMBDA,
});
new Function(this, 'Function with external source and requirements', {
handler: 'index.handler',
code: lambda.Code.fromAsset('lambda-src'),
runtime: lambda.Runtime.PYTHON_3_9,
// this will read requirements.txt and create a layer from the requirements in a Lambda function instead of locally
layers: [packager.layerFromRequirementsTxt('requirements.txt', 'lambda-src')],
});
```

Custom package managers like Pipenv or Poetry are also supported.

```typescript
const packager = new PythonDependencyPackager(this, 'Packager', {
runtime: lambda.Runtime.PYTHON_3_9,
type: DependencyPackagerType.LAMBDA,
});
new Function(this, 'Function with external source and requirements', {
handler: 'index.handler',
code: lambda.Code.fromAsset('lambda-poetry-src'),
runtime: lambda.Runtime.PYTHON_3_9,
// this will read pyproject.toml and poetry.lock and create a layer from the requirements in a Lambda function instead of locally
layers: [packager.layerFromPoetry('poetry requirements', 'lambda-src')],
layers: [packager.layerFromPoetry('poetry dependencies', 'lambda-poetry-src')],
});
```

If your dependencies have some C library dependencies, you may need to use the more capable but slower CodeBuild packager.

```typescript
const packager = new PythonDependencyPackager(this, 'Packager', {
runtime: lambda.Runtime.PYTHON_3_9,
type: DependencyPackagerType.CODEBUILD,
preinstallCommands: [
'apt install -y libxml2-dev libxslt-dev libffi-dev libssl-dev',
],
});
new Function(this, 'Function with external source and requirements', {
handler: 'index.handler',
code: lambda.Code.fromAsset('lambda-pipenv-src'),
runtime: lambda.Runtime.PYTHON_3_9,
layers: [packager.layerFromPipenv('pipenv dependencies', 'lambda-pipenv-src')],
});
```

Building layers for ARM64 functions is also supported.

```typescript
const packager = new PythonDependencyPackager(this, 'Packager', {
runtime: lambda.Runtime.PYTHON_3_9,
type: DependencyPackagerType.LAMBDA,
architecture: Architecture.ARM_64,
});
new Function(this, 'Function with external source and requirements', {
handler: 'index.handler',
code: lambda.Code.fromAsset('lambda-poetry-src'),
runtime: lambda.Runtime.PYTHON_3_9,
architecture: Architecture.ARM_64,
layers: [packager.layerFromPoetry('poetry dependencies', 'lambda-poetry-src')],
});
```

All these examples are for Python, but the same API is available for Node.js, Ruby, and Java. The same build options are available. Multiple different package managers are supported. See [Constructs Hub][13] for more details.

## Older Implementations

* [lovage](https://github.com/CloudSnorkel/lovage): standalone Python framework that uses the same trick to deploy decorated functions to AWS
Expand Down

0 comments on commit edce0fb

Please sign in to comment.