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

npm URLs are re-downloaded despite caching in dockerfile #214

Open
GJZwiers opened this issue Nov 27, 2022 · 0 comments
Open

npm URLs are re-downloaded despite caching in dockerfile #214

GJZwiers opened this issue Nov 27, 2022 · 0 comments

Comments

@GJZwiers
Copy link

GJZwiers commented Nov 27, 2022

I am having an issue when using Deno Lambda together with npm: URLs. It seems the npm dependencies are being re-downloaded on executing the function even though they have already been cached during the Docker build.

src/listFunctions.ts:

import {
  APIGatewayProxyEventV2,
  APIGatewayProxyResultV2,
  Context,
} from "https://deno.land/x/lambda@1.28.1/mod.ts";
import { LambdaClient, ListFunctionsCommand } from "npm:@aws-sdk/client-lambda";
 
export async function handler(
  _event: APIGatewayProxyEventV2,
  _context: Context,
): Promise<APIGatewayProxyResultV2> {

  const client = new LambdaClient({ region: "eu-west-3" });

  const funs = await client.send(new ListFunctionsCommand({
    MaxItems: 10,
  }));
  console.log(funs);

  return {
    statusCode: 200,
    headers: { "content-type": "text/html;charset=utf8" },
    body: JSON.stringify(funs.Functions),
  };
}

src/Dockerfile:

FROM hayd/deno-lambda:1.28.2

COPY listFunctions.ts .
RUN deno cache listFunctions.ts

CMD ["listFunctions.handler"]

The lambda is deployed from an AWS CDK project inside an infra folder using this stack:

import * as cdk from "aws-cdk-lib";
import * as iam from "aws-cdk-lib/aws-iam";
import * as lambda from "aws-cdk-lib/aws-lambda";
import { Construct } from "constructs";

export class LambdaStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const fn = new lambda.DockerImageFunction(this, "DockerHandler", {
      code: lambda.DockerImageCode.fromImageAsset("../src"),
    });

    fn.role?.attachInlinePolicy(
      new iam.Policy(this, "allow-lambda-list-functions", {
        statements: [
          new iam.PolicyStatement({
            actions: ["lambda:ListFunctions"],
            resources: ["*"],
          }),
        ],
      }),
    );
  }
}

During the docker build in my pipeline I can see the npm dependencies are downloaded:

 Status: Downloaded newer image for hayd/deno-lambda:1.28.2
 ---> 0306bd607a84
Step 2/4 : COPY listFunctions.ts .
 ---> 855fc54db93f
Step 3/4 : RUN deno cache listFunctions.ts
 ---> Running in cdeb25b45f93
Download https://deno.land/x/lambda@1.28.1/mod.ts
Download https://deno.land/x/lambda@1.28.1/types.d.ts
Download https://registry.npmjs.org/@aws-sdk/client-lambda
Download https://registry.npmjs.org/@aws-crypto/sha256-browser
Download https://registry.npmjs.org/@aws-crypto/sha256-js
Download https://registry.npmjs.org/@aws-sdk/client-sts
Download https://registry.npmjs.org/@aws-sdk/config-resolver
Download https://registry.npmjs.org/@aws-sdk/credential-provider-node
Download https://registry.npmjs.org/@aws-sdk/fetch-http-handler
Download https://registry.npmjs.org/@aws-sdk/hash-node
Download https://registry.npmjs.org/@aws-sdk/invalid-dependency
Download https://registry.npmjs.org/@aws-sdk/middleware-content-length
Download https://registry.npmjs.org/@aws-sdk/middleware-endpoint
Download https://registry.npmjs.org/@aws-sdk/middleware-host-header
Download https://registry.npmjs.org/@aws-sdk/middleware-logger
Download https://registry.npmjs.org/@aws-sdk/middleware-recursion-detection
...

But when invoking the lambda it times out and I can see in the logs that the npm dependencies are being downloaded again:

warn: unable to import '.deno_dir/' as DENO_DIR
Download https://registry.npmjs.org/@aws-sdk/client-lambda
Download https://registry.npmjs.org/@aws-crypto/sha256-browser
Download https://registry.npmjs.org/@aws-crypto/sha256-js
Download https://registry.npmjs.org/@aws-sdk/client-sts
Download https://registry.npmjs.org/@aws-sdk/config-resolver
Download https://registry.npmjs.org/@aws-sdk/credential-provider-node
Download https://registry.npmjs.org/@aws-sdk/fetch-http-handler
Download https://registry.npmjs.org/@aws-sdk/hash-node
Download https://registry.npmjs.org/@aws-sdk/invalid-dependency
Download https://registry.npmjs.org/@aws-sdk/middleware-content-length
Download https://registry.npmjs.org/@aws-sdk/middleware-endpoint
Download https://registry.npmjs.org/@aws-sdk/middleware-host-header
Download https://registry.npmjs.org/@aws-sdk/middleware-logger
Download https://registry.npmjs.org/@aws-sdk/middleware-recursion-detection

If I change dependencies to use the aws_api module from deno.land/x I don't get this issue at all (although I do get the same warning about being unable to import .deno_dir)

import {
  APIGatewayProxyEventV2,
  APIGatewayProxyResultV2,
  Context,
} from "https://deno.land/x/lambda@1.28.1/mod.ts";
import { ApiFactory } from "https://deno.land/x/aws_api@v0.7.0/client/mod.ts";
import { Lambda } from "https://deno.land/x/aws_api@v0.7.0/services/lambda/mod.ts";
 
export async function handler(
  _event: APIGatewayProxyEventV2,
  _context: Context,
): Promise<APIGatewayProxyResultV2> {
  const lambda = new ApiFactory({
    region: "eu-west-3",
  }).makeNew(Lambda);

  const funs = await lambda.listFunctions({
    MaxItems: 10,
  });
  console.log(funs);

  return {
    statusCode: 200,
    headers: { "content-type": "text/html;charset=utf8" },
    body: JSON.stringify(funs.Functions),
  };
}
warn: unable to import '.deno_dir/' as DENO_DIR
--
START RequestId: 4c3c8794-3800-4708-8fb2-98d757bb4f56 Version: $LATEST
INFO	RequestId: 4c3c8794-3800-4708-8fb2-98d757bb4f56{  Functions: [    {      Description: "",      TracingConfig: { Mode: "PassThrough" },      VpcConfig: null,      SigningJobArn: null,      RevisionId: "3f9fbe00-d293-4df9-a8c9-4055e2b5cf49",      LastModified: "2022-11-27T11:55:16.000+0000",      FileSystemConfigs: null,      FunctionName: "LambdaStack-DockerHandlerE16963D5-RJq8TsfCxSsm",      Runtime: null,      Version: "$LATEST",      PackageType: "Image",      LastUpdateStatus: null,      Layers: null,      FunctionArn: "arn:aws:lambda:eu-west-3:305747223196:function:LambdaStack-DockerHandlerE16963D5-RJq8TsfCxSsm",      KMSKeyArn: null,      MemorySize: 128,      ImageConfigResponse: null,      LastUpdateStatusReason: null,      DeadLetterConfig: null,      Timeout: 30,      Handler: null,      CodeSha256: "0b8ada40dfe0610a0720f995ca8a7b271213678e616111451c952a226c8557ca",      Role: "arn:aws:iam::305747223196:role/LambdaStack-DockerHandlerServiceRole5A884A94-1WWHCIXLKC63E",      SigningProfileVersionArn: null,      MasterArn: null,      CodeSize: 0,      State: null,      StateReason: null,      Environment: null,      StateReasonCode: null,      LastUpdateStatusReasonCode: null,      Architectures: [ "x86_64" ]    }  ],  NextMarker: null}
END RequestId: 4c3c8794-3800-4708-8fb2-98d757bb4f56
REPORT RequestId: 4c3c8794-3800-4708-8fb2-98d757bb4f56	Duration: 363.21 ms	Billed Duration: 928 ms	Memory Size: 128 MB	Max Memory Used: 71 MB	Init Duration: 563.89 ms
@GJZwiers GJZwiers changed the title Deno Lambda re-downloading npm URLs despite caching in dockerfile npm URLs are re-downloaded despite caching in dockerfile Dec 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant