sst / ion

❍ — a new engine for SST
https://ion.sst.dev
MIT License
1.11k stars 131 forks source link

Support for python lambda functions on AWS #366

Open kdenz opened 2 months ago

kdenz commented 2 months ago

Hi! Loved SST, thank you so much for all the work over the years! Curious when python lambda functions will be supported for AWS? I think it might help adoption too because right now AI development is huge and there's no easy to use serverless frameworks for python out there. Most people needed to resort to using FastAPI or something. I was using SST v2 but the setup has been painful as it's not well-documented, and VSCode debugger just didn't work at all unlike node.js counterpart. Pulumi supports Python lambdas already but there's no live debugging on their roadmap.

https://www.pulumi.com/docs/clouds/aws/guides/lambda/

thdxr commented 2 months ago

our challenge is no one on the team uses python day to day so it's quite hard for us to create a good python experience given how many different ways there is to do things in that ecosystem

nothing stops us from supporting it technically we just don't have much clarity on what supporting it well means

RamblinGambler commented 2 months ago

Is it possible to document a working python function pattern using the pulumi api in the ion run block? That way the dev team doesn't have to commit to building an experience they are unfamiliar with and on the flipside this would do two things:

  1. Give people who just want to run some python a pattern to follow. It doesn't have to be the best experience like the rest of the native ion behavior but its something for devs to go off of and extend.

  2. The more people use it the more interest there will be to improve it.

I am having a bear of a time getting python pulumi functions to be recognized in the ion run block. I could use some better guidance than the scattered bits of info between the sites and discord, but once it works I can help improve it.

cmanou commented 1 month ago

The way i've been working around it from a purely deployment point of view with ion, which still has problems with dev experience is via docker image lambdas. I have a docker image based of the python lambda base images (since I found its easiest way to deal with dependencies for platform specific) and then use code similar to this in the run block

const imageRepository = new aws.ecr.Repository(
      "ImageRepository",
      { name: "<name_repository>" }
);

const imageAuthToken = aws.ecr.getAuthorizationTokenOutput({
  registryId: imageRepository.registryId,
});
const image = new docker.Image("Image", {
  imageName: $interpolate`${imageRepository.repositoryUrl}:latest`,
  build: {
    context: path.join($cli.paths.root, "projects/<path_to_docker_context>"),
    platform: "linux/amd64",
  },
  registry: {
    server: imageAuthToken.proxyEndpoint,
    username: imageAuthToken.userName,
    password: $util.secret(imageAuthToken.password),
  },
});
const lambdaRole = new aws.iam.Role("LambdaRole", {
  assumeRolePolicy: aws.iam.assumeRolePolicyForPrincipal({
    Service: "lambda.amazonaws.com",
  }),
  inlinePolicies: [],
  managedPolicyArns: [
    "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
  ],
});

const lambdaHandler = new aws.lambda.Function(`HandlerFunction`, {
  name: `${$app.name}-${$app.stage}-HandlerFunction`,
  imageUri: image.repoDigest,
  timeout: 120,
  architectures: ["x86_64"],
  packageType: "Image",
  role: lambdaRole.arn,
});

But yeah better support from a dev experience side would be great. Though I agree with @thdxr that the ecosystem has many varied options for things so supporting all of them will be an up hill battle and there doesn't seem to be any leading contenders especially in the dependency management/bundling side of things.

Potentially a middle ground could be something like first class docker image support for SST functions and live mode could maybe do something with running the images in the local test mode that spins up a http server but that would only work if you use the proper base images on the runtime interface client.

kdenz commented 1 month ago

Much agreed! For now I would look forward to just feature parity with SST v2's Function, where it at least allows easy development and deployment of python lambda functions, with live debugging / breakpoint debugging / SST console logs on cloud, etc. Just wondering if there's any timeline for SST Ion's Function to support python runtimes like SST v2? @thdxr

partmor commented 1 week ago

in theory it should be possible to have live python functions as of now with provided.al2023 and custom bundler and handler (like in the Swift example), right?

(cc @thdxr )

edit: this I have just said is probably stupid because in this case you don’t have a binary and probably you’d have to stick to the system’s python (if even present)