@@ -77,7 +77,9 @@ The best way to browse API documentation is on [Constructs Hub][13]. It is avail
77
77
dotnet add package CloudSnorkel.Cdk.TurboLayers
78
78
```
79
79
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.
81
83
82
84
```typescript
83
85
const packager = new PythonDependencyPackager(this, 'Packager', {
@@ -91,15 +93,77 @@ new Function(this, 'Function with inline requirements', {
91
93
// this will create a layer from with requests and Scrapy in a Lambda function instead of locally
92
94
layers: [packager.layerFromInline('inline requirements', ['requests', 'Scrapy'])],
93
95
});
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
+ });
94
105
new Function (this , ' Function with external source and requirements' , {
95
106
handler: ' index.handler' ,
96
107
code: lambda .Code .fromAsset (' lambda-src' ),
97
108
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 ,
98
125
// 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' )],
100
127
});
101
128
```
102
129
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
+
103
167
## Older Implementations
104
168
105
169
* [ lovage] ( https://github.com/CloudSnorkel/lovage ) : standalone Python framework that uses the same trick to deploy decorated functions to AWS
0 commit comments