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

SAM local lambda prevent rebuilding image on every invocation #6277

Closed
tobyloki opened this issue Nov 16, 2023 · 3 comments
Closed

SAM local lambda prevent rebuilding image on every invocation #6277

tobyloki opened this issue Nov 16, 2023 · 3 comments
Labels
blocked/close-if-inactive Blocked for >14 days with no response, will be closed if still inactive after 7 days type/question

Comments

@tobyloki
Copy link

I am trying to test my Lambda locally using sam local start-lambda. It is attached to a local Lambda Layer called MyLambdaLayer. However, whenever I try to invoke the Lambda, it always rebuilds the image. Is there a way to cache the build or prevent it from rebuilding on every invocation?

Here is my setup procedure:

  1. Start the local Lambda server
sam local start-lambda --template Lambda.yaml --skip-pull-image
  1. Invoke the Lambda via JS test script
const { LambdaClient, InvokeCommand } = require('@aws-sdk/client-lambda');
const lambda = new LambdaClient({
    endpoint: 'http://127.0.0.1:3001',
    tls: false,
    region: 'us-east-1',
    credentials: {
        accessKeyId: 'any',
        secretAccessKey: 'any'
    }
});
const invoke = async (funcName, payload) => {
    const command = new InvokeCommand({
        FunctionName: funcName,
        Payload: JSON.stringify(payload)
    });
    const ret = await lambda.send(command);
    const { Payload, StatusCode } = ret;
    const result = Buffer.from(Payload).toString();
    return {
        payload: result,
        statusCode: StatusCode
    };
};

test('test', async () => {
    const event = {
        data: 'some data'
    };
    const result = await invoke('MyLambda', event);
    console.log(result);
}, 25000);
npm test lambdaInvoke.test.js

Here are the logs from the local Lambda server every time I invoke the Lambda:

2023-11-15 20:11:46 127.0.0.1 - - [15/Nov/2023 20:11:46] "POST /2015-03-31/functions/MyLambda/invocations HTTP/1.1" 200 -
Invoking index.handler (nodejs16.x)                                                                                                                         
MyLambdaLayer is a local Layer in the template                                                                                                
Building image.....................
Requested to skip pulling images ...                                                                                                                        
                                                                                                                                                            
Mounting /path/MyLambda as /var/task:ro,delegated, inside runtime container                                    
START RequestId: 76f26199-64cf-4def-959a-97afd4a10951 Version: $LATEST
END RequestId: 76f26199-64cf-4def-959a-97afd4a10951
REPORT RequestId: 76f26199-64cf-4def-959a-97afd4a10951  Init Duration: 0.13 ms  Duration: 1290.95 ms    Billed Duration: 1291 ms        Memory Size: 256 MBMax Memory Used: 256 MB

2023-11-15 20:12:14 127.0.0.1 - - [15/Nov/2023 20:12:14] "POST /2015-03-31/functions/MyLambda/invocations HTTP/1.1" 200 -
Invoking index.handler (nodejs16.x)                                                                                                                         
MyLambdaLayer is a local Layer in the template                                                                                                
Building image.....................
Requested to skip pulling images ...                                                                                                                        
                                                                                                                                                            
Mounting /path/MyLambda as /var/task:ro,delegated, inside runtime container                                    
START RequestId: b5b2e638-1d45-48a8-8d13-e3118c50f4bc Version: $LATEST
END RequestId: b5b2e638-1d45-48a8-8d13-e3118c50f4bc
REPORT RequestId: b5b2e638-1d45-48a8-8d13-e3118c50f4bc  Init Duration: 0.07 ms  Duration: 1348.59 ms    Billed Duration: 1349 ms        Memory Size: 256 MBMax Memory Used: 256 MB
@tobyloki tobyloki added the stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. label Nov 16, 2023
@mildaniel
Copy link
Contributor

Hi @tobyloki, can you try running the command using the --warm-containers EAGER flag and see if this works for you?

@mildaniel mildaniel added type/question and removed stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. labels Nov 21, 2023
@hnnasit hnnasit added the blocked/close-if-inactive Blocked for >14 days with no response, will be closed if still inactive after 7 days label Mar 6, 2024
@hnnasit
Copy link
Contributor

hnnasit commented Apr 17, 2024

Closing as this issue has been inactive for a while. Feel free to try @mildaniel's suggestion and open a new issue if you still have questions or concerns.

@hnnasit hnnasit closed this as completed Apr 17, 2024
Copy link
Contributor

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked/close-if-inactive Blocked for >14 days with no response, will be closed if still inactive after 7 days type/question
Projects
None yet
Development

No branches or pull requests

3 participants