Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need example how to deploy a custom image #2457

Open
cookiejest opened this issue Jan 29, 2025 · 4 comments
Open

Need example how to deploy a custom image #2457

cookiejest opened this issue Jan 29, 2025 · 4 comments
Labels
function Issue pertaining to Amplify Function pending-response Issue is pending response from author pending-triage Incoming issues that need categorization question Question or confusion about some aspect of the product

Comments

@cookiejest
Copy link

Environment information

Hi,
is there an example of how to deploy a python custom image? I have tried but receiving an error.

export const promptChange = defineFunction(
  (scope) =>
    new Function(scope, "prompt-change", {
      handler: "promptchange.lambda_handler",
      runtime: Runtime.PYTHON_3_12, // or any other python version
      timeout: Duration.seconds(90), //  default is 3 seconds
      code: Code.fromAsset(functionDir, {
        bundling: {
          image: DockerImage.fromRegistry("975376537333.dkr.ecr.eu-west-2.amazonaws.com/myimgplc/imgname:latest")
        },
      }),
    }),
    {
      resourceGroupName: "data"
    }
);

Describe the bug

When bundling an image for a custom python error I get this:

Caused By: Failed to bundle asset amplify-xx-sandbox-8f5ab678dd/data/run-change/Code/Stage, bundle output is located at /Users/xxx/github/amplify2/.amplify/artifacts/cdk.out/asset.9a70ed8aa3b44559383b3b2324056169c53f30299385b2066ebfc7b947005880-error: Error: docker exited with status 125
--> Command: docker run --rm -u "501:20" -v "/Users/xxx/github/amplify2/amplify/functions/hello-python:/asset-input:delegated" -v "/Users/xxx/github/amplify2/.amplify/artifacts/cdk.out/asset.9a70ed8aa3b44559383b3b2324056169c53f30299385b2066ebfc7b947005880:/asset-output:delegated" -w "/asset-input" "975376537682.dkr.ecr.eu-west-2.amazonaws.com/myimgplc/imgname:latest"

Reproduction steps

Create a ecr image using python3.12. Then try and use it a lambda function inside amplify gen2.

@cookiejest cookiejest added the pending-triage Incoming issues that need categorization label Jan 29, 2025
@ykethan
Copy link
Member

ykethan commented Jan 29, 2025

Hey @cookiejest, from the CDK documentation the fromRegistry property currently pulls public hosted images such as DockerHub or public ECR repository.
If you are looking to use a bundling image with python3.12, you could use the following public images.
Docker Hub
ECR Gallery

if you are looking to use a private repository, you could use methods such as fromECRImage to use the image as the Lambda code.
For additional guidance on the CDK Lambda function construct, open a issue on AWS CDK repository: https://github.com/aws/aws-cdk

@ykethan ykethan added question Question or confusion about some aspect of the product pending-response Issue is pending response from author labels Jan 29, 2025
@cookiejest
Copy link
Author

cookiejest commented Jan 29, 2025

Code.fromEcrImage seems to work. Now I am struggling to figure out how to assign environment variables to it, specifically the graphql API url as I see environment variables are not supported. Is there another way to do this? I also need to assign a specific handler within the image but it says I must assign Handler.FROM_IMAGE,

@github-actions github-actions bot removed the pending-response Issue is pending response from author label Jan 29, 2025
@Jay2113 Jay2113 added the function Issue pertaining to Amplify Function label Jan 30, 2025
@cookiejest
Copy link
Author

Hi I have got this function, it seems environment variables might actually be passed? If so I think the docs are not correct.

Can you confirm?

export const runChange = defineFunction(
  (scope) =>
    new Function(scope, "run-change", {
      handler: Handler.FROM_IMAGE,
      environment: {
        MY_KEY: 'ASDASD' 
      },
      runtime: Runtime.FROM_IMAGE,
      timeout: Duration.seconds(90),
      code: Code.fromEcrImage(Repository.fromRepositoryName(scope, "airunner-runchange", "cloudslurp/airunner"),{
        entrypoint: ["python",  "-m", "runchange", "runchange.lambda_handler"],
        tagOrDigest: 'latest',
      })
    }),
  {
    resourceGroupName: "data"
  }
);

@ykethan
Copy link
Member

ykethan commented Jan 31, 2025

Hey @cookiejest, the provided code should be correct. As this is a custom function the AWS Lambda CDK construct provides the environment property to add your environment variables.
https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.Function.html#environment

And as the custom function is create on defineFunction the backend object on the backend.ts should also provide access to the properties.
for example: backend.myCustomFunction.resources.cfnResources.cfnFunction or backend.myCustomFunction.resources.lambda
you should be able to add you environment variables such as the API URL from the backend.ts, for example:

// function/resource.ts
import { defineFunction } from "@aws-amplify/backend";
import { Duration } from "aws-cdk-lib";
import { Repository } from "aws-cdk-lib/aws-ecr";
import { Code, Function, Handler, Runtime } from "aws-cdk-lib/aws-lambda";

export const dockerFunctionecr = defineFunction(
  (scope) =>
    new Function(scope, "docker-function-ecr2", {
      handler: Handler.FROM_IMAGE,
      runtime: Runtime.FROM_IMAGE,
      timeout: Duration.seconds(20),
      environment: {
        Hello: "there",
      },

      code: Code.fromEcrImage(
        Repository.fromRepositoryName(
          scope,
          "PythonRepo",
          "amplify/custom-function"
        ),
        {
          tagOrDigest: "latest",
        }
      ),
    }),
  {
    resourceGroupName: "data",
  }
);

// backend.ts
const cfnFn = backend.dockerFunctionecr.resources.cfnResources.cfnFunction;

cfnFn.addPropertyOverride("Environment.Variables", {
  // @ts-expect-error
  ...((cfnFn.environment?.variables || {}) as Record<string, string>),
  KEY3: "value3", // Add new vars
  KEY4: "value4",
});


@ykethan ykethan added the pending-response Issue is pending response from author label Jan 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
function Issue pertaining to Amplify Function pending-response Issue is pending response from author pending-triage Incoming issues that need categorization question Question or confusion about some aspect of the product
Projects
None yet
Development

No branches or pull requests

3 participants