Skip to content

Commit edce0fb

Browse files
authored
chore: Improve readme with more examples (#112)
1 parent 2799046 commit edce0fb

File tree

2 files changed

+132
-4
lines changed

2 files changed

+132
-4
lines changed

API.md

Lines changed: 66 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ The best way to browse API documentation is on [Constructs Hub][13]. It is avail
7777
dotnet add package CloudSnorkel.Cdk.TurboLayers
7878
```
7979
80-
## Example
80+
## Examples
81+
82+
The very basic example below will create a layer with dependencies specified as parameters and attach it to a Lambda function.
8183
8284
```typescript
8385
const packager = new PythonDependencyPackager(this, 'Packager', {
@@ -91,15 +93,77 @@ new Function(this, 'Function with inline requirements', {
9193
// this will create a layer from with requests and Scrapy in a Lambda function instead of locally
9294
layers: [packager.layerFromInline('inline requirements', ['requests', 'Scrapy'])],
9395
});
96+
```
97+
98+
The next example will create a layer with dependencies specified in a `requirements.txt` file and attach it to a Lambda function.
99+
100+
```typescript
101+
const packager = new PythonDependencyPackager(this, 'Packager', {
102+
runtime: lambda.Runtime.PYTHON_3_9,
103+
type: DependencyPackagerType.LAMBDA,
104+
});
94105
new Function(this, 'Function with external source and requirements', {
95106
handler: 'index.handler',
96107
code: lambda.Code.fromAsset('lambda-src'),
97108
runtime: lambda.Runtime.PYTHON_3_9,
109+
// this will read requirements.txt and create a layer from the requirements in a Lambda function instead of locally
110+
layers: [packager.layerFromRequirementsTxt('requirements.txt', 'lambda-src')],
111+
});
112+
```
113+
114+
Custom package managers like Pipenv or Poetry are also supported.
115+
116+
```typescript
117+
const packager = new PythonDependencyPackager(this, 'Packager', {
118+
runtime: lambda.Runtime.PYTHON_3_9,
119+
type: DependencyPackagerType.LAMBDA,
120+
});
121+
new Function(this, 'Function with external source and requirements', {
122+
handler: 'index.handler',
123+
code: lambda.Code.fromAsset('lambda-poetry-src'),
124+
runtime: lambda.Runtime.PYTHON_3_9,
98125
// this will read pyproject.toml and poetry.lock and create a layer from the requirements in a Lambda function instead of locally
99-
layers: [packager.layerFromPoetry('poetry requirements', 'lambda-src')],
126+
layers: [packager.layerFromPoetry('poetry dependencies', 'lambda-poetry-src')],
100127
});
101128
```
102129

130+
If your dependencies have some C library dependencies, you may need to use the more capable but slower CodeBuild packager.
131+
132+
```typescript
133+
const packager = new PythonDependencyPackager(this, 'Packager', {
134+
runtime: lambda.Runtime.PYTHON_3_9,
135+
type: DependencyPackagerType.CODEBUILD,
136+
preinstallCommands: [
137+
'apt install -y libxml2-dev libxslt-dev libffi-dev libssl-dev',
138+
],
139+
});
140+
new Function(this, 'Function with external source and requirements', {
141+
handler: 'index.handler',
142+
code: lambda.Code.fromAsset('lambda-pipenv-src'),
143+
runtime: lambda.Runtime.PYTHON_3_9,
144+
layers: [packager.layerFromPipenv('pipenv dependencies', 'lambda-pipenv-src')],
145+
});
146+
```
147+
148+
Building layers for ARM64 functions is also supported.
149+
150+
```typescript
151+
const packager = new PythonDependencyPackager(this, 'Packager', {
152+
runtime: lambda.Runtime.PYTHON_3_9,
153+
type: DependencyPackagerType.LAMBDA,
154+
architecture: Architecture.ARM_64,
155+
});
156+
new Function(this, 'Function with external source and requirements', {
157+
handler: 'index.handler',
158+
code: lambda.Code.fromAsset('lambda-poetry-src'),
159+
runtime: lambda.Runtime.PYTHON_3_9,
160+
architecture: Architecture.ARM_64,
161+
layers: [packager.layerFromPoetry('poetry dependencies', 'lambda-poetry-src')],
162+
});
163+
```
164+
165+
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.
166+
103167
## Older Implementations
104168

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

0 commit comments

Comments
 (0)