You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Update Lambda and GCF docs for runtime inference (#21266)
This updates the Lambda and GCF docs (very belatedly) for the "new"
behaviours related to choosing platform specific code, like inference of
the `runtime` field (#19314), and associated complete platforms
(#19253).
This includes stripping out the `runtime` field from the "main" flow,
and adding an separate subsection for how to specify the runtime
explicitly.
Copy file name to clipboardexpand all lines: docs/docs/python/integrations/aws-lambda.mdx
+46-7
Original file line number
Diff line number
Diff line change
@@ -33,7 +33,7 @@ First, add your lambda function in a Python file like you would [normally do wit
33
33
34
34
Then, in your BUILD file, make sure that you have a `python_source` or `python_sources` target with the handler file included in the `sources` field. You can use [`pants tailor ::`](../../getting-started/initial-configuration.mdx#5-generate-build-files) to automate this.
35
35
36
-
Add a `python_aws_lambda_function` target and define the `runtime` and `handler` fields. The `runtime` should be one of the values from [https://docs.aws.amazon.com/lambda/latest/dg/lambda-python.html](https://docs.aws.amazon.com/lambda/latest/dg/lambda-python.html). The `handler` has the form `handler_file.py:handler_func`, which Pants will convert into a well-formed entry point. Alternatively, you can set `handler` to the format `path.to.module:handler_func`.
36
+
Add a `python_aws_lambda_function` target and define the `handler` fields. The `handler` has the form `handler_file.py:handler_func`, which Pants will convert into a well-formed entry point. Alternatively, you can set `handler` to the format `path.to.module:handler_func`.
37
37
38
38
For example:
39
39
@@ -43,7 +43,6 @@ python_sources(name="lib")
43
43
44
44
python_aws_lambda_function(
45
45
name="lambda",
46
-
runtime="python3.8",
47
46
# Pants will convert this to `project.lambda_example:example_handler`.
48
47
handler="lambda_example.py:example_handler",
49
48
)
@@ -66,6 +65,49 @@ Use [layout](../../../reference/targets/python_aws_lambda_function.mdx#layout) t
66
65
`file` / `files` targets will not be included in the built AWS Lambda artifacts because filesystem APIs like `open()` would not load them as expected. Instead, use the `resource` and `resources` target. See [Assets and archives](../../using-pants/assets-and-archives.mdx) for further explanation.
67
66
:::
68
67
68
+
### Specifying a runtime explicitly
69
+
70
+
When building an Lambda artifact, Pants and the underlying Pex tool need to know details about target runtime to be able to choose appropriate artifacts for third-party dependencies that have native code. These details can be inferred or provided in three ways, from highest precedence to lowest precedence:
71
+
72
+
1. An explicit value for [the `complete_platforms` field](../../../reference/targets/python_aws_lambda_function.mdx#complete_platforms). The "complete platforms" are the underlying source of truth.
2. An explicit value for [the `runtime` field](../../../reference/targets/python_aws_lambda_function.mdx#runtime) and, optionally, [the `architecture` field](../../../reference/targets/python_aws_lambda_function.mdx#architecture): Pants uses these to pick an appropriate "complete platforms" value, from options that Pants has pre-packaged. These are static exports from docker images provided by AWS, relying on the environment being relatively stable. (If Pants doesn't have an appropriate "complete platforms" default built-in, you will be prompted to use option 1 above.)
84
+
```python title="BUILD"
85
+
python_aws_lambda_function(
86
+
name="lambda",
87
+
handler="lambda_example.py:example_handler",
88
+
# Explicit runtime, `complete_platforms` taken from Pants' built-in defaults:
89
+
runtime="python3.12",
90
+
# Override the default x86_64 architecture:
91
+
architecture="arm64",
92
+
)
93
+
```
94
+
3. Inferred from [the relevant interpreter constraints](../overview/interpreter-compatibility.mdx): the interpreter constraints may unambiguously imply a value for the `runtime` and thus `complete_platforms` fields. For example, `interpreter_constaints = ["==3.12.*"]` implies `runtime="python3.12"`. This only works with interpreter constraints that cover all patch versions of a given minor release series: `>=3.11,<3.13` is too wide (it covers both 3.11 and 3.12), while `==3.12.0` is too specific (AWS's `python3.12` runtime may not use that exact patch version). As with option 2, the architecture is `x86_64` by default, but can changed using the `architecture` field.
95
+
96
+
```toml tab={"label":"pants.toml"}
97
+
[python]
98
+
interpreter_constraints = ["==3.12.*"]
99
+
```
100
+
```python tab={"label":"project/BUILD"}
101
+
python_aws_lambda_function(
102
+
name="lambda",
103
+
handler="lambda_example.py:example_handler",
104
+
# `runtime` inferred and `complete_platforms` from built-in defaults,
105
+
# `architecture` defaults to x86_64, but can be overridden
106
+
)
107
+
```
108
+
109
+
This guide is written using the last option, with the default `x86_64` architecture, but you can add `runtime`, `complete_platforms` and/or `architecture` to any examples using the `python_aws_lambda_function` or `python_aws_lambda_layer` targets.
110
+
69
111
## Step 3: Run `package`
70
112
71
113
Now run `pants package` on your `python_aws_lambda_function` target to create a zipped file.
@@ -118,7 +160,6 @@ python_sources()
118
160
119
161
python_aws_lambda_function(
120
162
name="lambda",
121
-
runtime="python3.8",
122
163
handler="main.py:lambda_handler"
123
164
)
124
165
@@ -148,15 +189,13 @@ python_sources(name="lib")
148
189
149
190
python_aws_lambda_function(
150
191
name="function",
151
-
runtime="python3.8",
152
192
handler="lambda_example.py:example_handler",
153
193
# only include the sources, the boto3 requirement is packaged in `:layer`
154
194
include_requirements=False,
155
195
)
156
196
157
197
python_aws_lambda_layer(
158
198
name="layer",
159
-
runtime="python3.8"
160
199
# specify the handler file, and pants will automatically find its transitive dependencies
161
200
dependencies=["./lambda_example.py"],
162
201
# only include the boto3 requirement, any sources are packaged in `:function`
@@ -198,8 +237,8 @@ python_sources()
198
237
pex_binary(
199
238
name="lambda",
200
239
entry_point="lambda_example.py",
201
-
# specify an appropriate platform(s) for the targeted Lambda runtime (complete_platforms works too)
202
-
platforms=["linux_x86_64-cp39-cp39"],
240
+
# specify an appropriate platform for the targeted Lambda runtime:
Copy file name to clipboardexpand all lines: docs/docs/python/integrations/google-cloud-functions.mdx
+46-4
Original file line number
Diff line number
Diff line change
@@ -33,7 +33,7 @@ First, add your Cloud function in a Python file like you would [normally do with
33
33
34
34
Then, in your BUILD file, make sure that you have a `python_source` or `python_sources` target with the handler file included in the `sources` field. You can use [`pants tailor ::`](../../getting-started/initial-configuration.mdx#5-generate-build-files) to automate this.
35
35
36
-
Add a `python_google_cloud_function` target and define the `runtime`, `handler`, and `type` fields. The `type` should be either `"event"` or `"http"`. The `runtime` should be one of the values from [https://cloud.google.com/functions/docs/concepts/python-runtime](https://cloud.google.com/functions/docs/concepts/python-runtime). The `handler` has the form `handler_file.py:handler_func`, which Pants will convert into a well-formed entry point. Alternatively, you can set `handler` to the format `path.to.module:handler_func`.
36
+
Add a `python_google_cloud_function` target and define `handler` and `type` fields. The `type` should be either `"event"` or `"http"`. The `handler` has the form `handler_file.py:handler_func`, which Pants will convert into a well-formed entry point. Alternatively, you can set `handler` to the format `path.to.module:handler_func`.
37
37
38
38
For example:
39
39
@@ -43,7 +43,6 @@ python_sources(name="lib")
43
43
44
44
python_google_cloud_function(
45
45
name="cloud_function",
46
-
runtime="python38",
47
46
# Pants will convert this to `project.google_cloud_function_example:example_handler`.
@@ -67,6 +66,49 @@ Use [layout](../../../reference/targets/python_google_cloud_function.mdx#layout)
67
66
`file` / `files` targets will not be included in the built Cloud Function because filesystem APIs like `open()` would not load them as expected. Instead, use the `resource` / `resources` target. See [Assets and archives](../../using-pants/assets-and-archives.mdx) for further explanation.
68
67
:::
69
68
69
+
### Specifying a runtime explicitly
70
+
71
+
When building an Cloud function artifact, Pants and the underlying Pex tool need to know details about target runtime to be able to choose appropriate artifacts for third-party dependencies that have native code. These details can be inferred or provided in three ways, from highest precedence to lowest precedence:
72
+
73
+
1. An explicit value for [the `complete_platforms` field](../../../reference/targets/python_google_cloud_function.mdx#complete_platforms). The "complete platforms" are the underlying source of truth.
2. An explicit value for [the `runtime` field](../../../reference/targets/python_google_cloud_function.mdx#runtime): Pants uses this to pick an appropriate "complete platforms" value, from options that Pants has pre-packaged. These are static exports from docker images provided by GCP, relying on the environment being relatively stable. (If Pants doesn't have an appropriate "complete platforms" default built-in, you will be prompted to use option 1 above.)
# Explicit runtime, `complete_platforms` taken from Pants' built-in defaults:
92
+
runtime="python312",
93
+
)
94
+
```
95
+
3. Inferred from [the relevant interpreter constraints](../overview/interpreter-compatibility.mdx): the interpreter constraints may unambiguously imply a value for the `runtime` and thus `complete_platforms` fields. For example, `interpreter_constaints = ["==3.12.*"]` implies `runtime="python312"`. This only works with interpreter constraints that cover all patch versions of a given minor release series: `>=3.11,<3.13` is too wide (it covers both 3.11 and 3.12), while `==3.12.0` is too specific (GCF's `python312` runtime may not use that exact patch version).
# `runtime` inferred and `complete_platforms` from built-in defaults
107
+
)
108
+
```
109
+
110
+
This guide is written using the last option, but you can add `runtime` or `complete_platforms` to any examples using the `python_google_cloud_function` target.
111
+
70
112
## Step 3: Run `package`
71
113
72
114
Now run `pants package` on your `python_google_cloud_function` target to create a zipped file.
@@ -119,8 +161,8 @@ python_sources()
119
161
pex_binary(
120
162
name="gcf",
121
163
entry_point="gcf_example.py",
122
-
# specify an appropriate platform(s) for the targeted GCF runtime (complete_platforms works too)
123
-
platforms=["linux_x86_64-cp39-cp39"],
164
+
# specify an appropriate platform for the targeted GCF runtime:
0 commit comments