Skip to content

Commit fa583d6

Browse files
authored
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.
1 parent 6c41074 commit fa583d6

File tree

2 files changed

+92
-11
lines changed

2 files changed

+92
-11
lines changed

docs/docs/python/integrations/aws-lambda.mdx

+46-7
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ First, add your lambda function in a Python file like you would [normally do wit
3333

3434
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.
3535

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`.
3737

3838
For example:
3939

@@ -43,7 +43,6 @@ python_sources(name="lib")
4343

4444
python_aws_lambda_function(
4545
name="lambda",
46-
runtime="python3.8",
4746
# Pants will convert this to `project.lambda_example:example_handler`.
4847
handler="lambda_example.py:example_handler",
4948
)
@@ -66,6 +65,49 @@ Use [layout](../../../reference/targets/python_aws_lambda_function.mdx#layout) t
6665
`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.
6766
:::
6867

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.
73+
```python title="BUILD"
74+
file(name="lambda-platform", source="lambda-platform.json")
75+
76+
python_aws_lambda_function(
77+
name="lambda",
78+
handler="lambda_example.py:example_handler",
79+
# Explicit complete platforms:
80+
complete_platforms=[":lambda-platform"],
81+
)
82+
```
83+
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+
69111
## Step 3: Run `package`
70112

71113
Now run `pants package` on your `python_aws_lambda_function` target to create a zipped file.
@@ -118,7 +160,6 @@ python_sources()
118160

119161
python_aws_lambda_function(
120162
name="lambda",
121-
runtime="python3.8",
122163
handler="main.py:lambda_handler"
123164
)
124165

@@ -148,15 +189,13 @@ python_sources(name="lib")
148189

149190
python_aws_lambda_function(
150191
name="function",
151-
runtime="python3.8",
152192
handler="lambda_example.py:example_handler",
153193
# only include the sources, the boto3 requirement is packaged in `:layer`
154194
include_requirements=False,
155195
)
156196

157197
python_aws_lambda_layer(
158198
name="layer",
159-
runtime="python3.8"
160199
# specify the handler file, and pants will automatically find its transitive dependencies
161200
dependencies=["./lambda_example.py"],
162201
# only include the boto3 requirement, any sources are packaged in `:function`
@@ -198,8 +237,8 @@ python_sources()
198237
pex_binary(
199238
name="lambda",
200239
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:
241+
complete_platforms=["path/to:platform-json-target"],
203242
)
204243
```
205244

docs/docs/python/integrations/google-cloud-functions.mdx

+46-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ First, add your Cloud function in a Python file like you would [normally do with
3333

3434
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.
3535

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`.
3737

3838
For example:
3939

@@ -43,7 +43,6 @@ python_sources(name="lib")
4343

4444
python_google_cloud_function(
4545
name="cloud_function",
46-
runtime="python38",
4746
# Pants will convert this to `project.google_cloud_function_example:example_handler`.
4847
handler="google_cloud_function_example.py:example_handler",
4948
type="event",
@@ -67,6 +66,49 @@ Use [layout](../../../reference/targets/python_google_cloud_function.mdx#layout)
6766
`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.
6867
:::
6968

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.
74+
```python title="BUILD"
75+
file(name="gcf-platform", source="gcf-platform.json")
76+
77+
python_google_cloud_function(
78+
name="cloud_function",
79+
handler="google_cloud_function_example.py:example_handler",
80+
type="event",
81+
# Explicit complete platforms:
82+
complete_platforms=[":gcf-platform"],
83+
)
84+
```
85+
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.)
86+
```python title="BUILD"
87+
python_google_cloud_function(
88+
name="cloud_function",
89+
handler="google_cloud_function_example.py:example_handler",
90+
type="event",
91+
# 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).
96+
97+
```toml tab={"label":"pants.toml"}
98+
[python]
99+
interpreter_constraints = ["==3.12.*"]
100+
```
101+
```python tab={"label":"project/BUILD"}
102+
python_google_cloud_function(
103+
name="cloud_function",
104+
handler="google_cloud_function_example.py:example_handler",
105+
type="event",
106+
# `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+
70112
## Step 3: Run `package`
71113

72114
Now run `pants package` on your `python_google_cloud_function` target to create a zipped file.
@@ -119,8 +161,8 @@ python_sources()
119161
pex_binary(
120162
name="gcf",
121163
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:
165+
complete_platforms=["path/to:platform-json-target"],
124166
)
125167
```
126168

0 commit comments

Comments
 (0)