From dee7698135cef8b619afd73915d515ad5ecb6219 Mon Sep 17 00:00:00 2001 From: Agam More Date: Mon, 21 Oct 2024 00:02:52 -0500 Subject: [PATCH 1/3] Add lambda quickstart --- .../content/docs/docs/start/aws/lambda.mdx | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 www/src/content/docs/docs/start/aws/lambda.mdx diff --git a/www/src/content/docs/docs/start/aws/lambda.mdx b/www/src/content/docs/docs/start/aws/lambda.mdx new file mode 100644 index 000000000..251dc1446 --- /dev/null +++ b/www/src/content/docs/docs/start/aws/lambda.mdx @@ -0,0 +1,125 @@ +--- +title: Python lambda on AWS with SST +description: Create and deploy a python lambda to AWS with SST. +--- + +We'll deploy a python lambda that responds to a JSON payload. + + +--- + +## Preparation + +To create your first lambda, [install uv](https://docs.astral.sh/uv/getting-started/installation/). + +--- + +### 1. Create a python lambda project + +Let's start by creating our function. + +```bash +uv init hello-world +cd hello-world +``` + +This is going to create several files. + +--- + +##### Editing the files + +First, let's rename `hello.py` to `handler.py` (naming convention). + +If you open the file the contents will look something like: +```py +def main(): + print("Hello from hello-world!") + + +if __name__ == "__main__": + main() + +``` +Let's change it to the lambda format: + +```py +def main(event, context): + try: + body = json.loads(event['body']) + param = body.get('param') + return { + "statusCode": 200, + "body": json.dumps(f"Got: {param}"), + } + except Exception as e: + return { + "statusCode": 500, + "body": json.dumps(f"An error occurred: {str(e)}"), + } +``` + +--- + +##### Adding dependencies + +To add a dependency like `boto3`: + +```bash +uv add boto3 +``` + +This will add the dependency to pyproject.toml, it will automatically be installed on the lambda when deployed. + +--- + +##### Adding to SST + +First let's add the lambda to SST: +``` +const pdfWorker = new sst.aws.Function("MyLambda", { + handler: "/handler.main", + url: true, + runtime: "python3.10", +}); +``` + +--- + +##### Running + +`sst dev` + +You will notice it will output a url for the lambda, something like: +`https://.lambda-url..on.aws/` + +Now you can use Postman or anything you desire to invoke your lambda function + +--- + +##### Connecting to your project + +You can use the SST SDK to connect to your project like so: + +```js +import { Resource } from "sst"; + +const response = await fetch(Resource.MyLambda.url, { + method: "POST", + body: JSON.stringify({ param: "123" }), +}); + +const data = await response.json(); +console.log(data) +``` + +--- + +## Connect the console + +As a next step, you can setup the [SST Console](/docs/console/) to _**git push to deploy**_ your app and monitor it for any issues. + +![SST Console Autodeploy](../../../../../assets/docs/start/sst-console-autodeploy.png) + +You can [create a free account](https://console.sst.dev) and connect it to your AWS account. + From a4890a7ae9ba9bb89e394a9592b43bc5b66d76a9 Mon Sep 17 00:00:00 2001 From: Agam More Date: Mon, 21 Oct 2024 00:05:15 -0500 Subject: [PATCH 2/3] Add lambda quickstart --- www/src/content/docs/docs/start/aws/lambda.mdx | 1 - 1 file changed, 1 deletion(-) diff --git a/www/src/content/docs/docs/start/aws/lambda.mdx b/www/src/content/docs/docs/start/aws/lambda.mdx index 251dc1446..b5d0dfeca 100644 --- a/www/src/content/docs/docs/start/aws/lambda.mdx +++ b/www/src/content/docs/docs/start/aws/lambda.mdx @@ -5,7 +5,6 @@ description: Create and deploy a python lambda to AWS with SST. We'll deploy a python lambda that responds to a JSON payload. - --- ## Preparation From 7efd0f3979dc9fb57f2c68ce20feaa4883e4d437 Mon Sep 17 00:00:00 2001 From: Agam More Date: Mon, 21 Oct 2024 00:08:16 -0500 Subject: [PATCH 3/3] Update lambda.mdx --- www/src/content/docs/docs/start/aws/lambda.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/www/src/content/docs/docs/start/aws/lambda.mdx b/www/src/content/docs/docs/start/aws/lambda.mdx index b5d0dfeca..19e33238e 100644 --- a/www/src/content/docs/docs/start/aws/lambda.mdx +++ b/www/src/content/docs/docs/start/aws/lambda.mdx @@ -68,7 +68,7 @@ To add a dependency like `boto3`: uv add boto3 ``` -This will add the dependency to pyproject.toml, it will automatically be installed on the lambda when deployed. +This will add the dependency to `pyproject.toml`, it will automatically be installed on the lambda when deployed. --- @@ -98,7 +98,7 @@ Now you can use Postman or anything you desire to invoke your lambda function ##### Connecting to your project -You can use the SST SDK to connect to your project like so: +You can use the SST SDK to connect to your project like so (assuming you [linked](https://sst.dev/docs/component/linkable) it): ```js import { Resource } from "sst";