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

The resource AWS::Lambda::Function 'xxx4FF3C67E' has specified S3 location for Code. It will not be built and SAM CLI does not support invoking it locally. #2802

Closed
lidong-jimi opened this issue Apr 12, 2021 · 12 comments

Comments

@lidong-jimi
Copy link

Description:

The resource AWS::Lambda::Function 'xxx4FF3C67E' has specified S3 location for Code. It will not be built and SAM CLI does not support invoking it locally.

Steps to reproduce:

When run **sam local start-api**.
output:

The resource AWS::Lambda::Function 'xxx4FF3C67E' has specified S3 location for Code. It will not be built and SAM CLI does not support invoking it locally.
The resource AWS::Lambda::Function 'xxxF73A144E' has specified S3 location for Code. It will not be built and SAM CLI does not support invoking it locally.
The resource AWS::Lambda::Function 'xxx78EA90C0' has specified S3 location for Code. It will not be built and SAM CLI does not support invoking it locally.
The resource AWS::Lambda::Function 'xxx2BE3C117' has specified S3 location for Code. It will not be built and SAM CLI does not support invoking it locally.
The resource AWS::Lambda::Function 'xxx7CB472B4' has specified S3 location for Code. It will not be built and SAM CLI does not support invoking it locally.
Mounting None at http://127.0.0.1:3000/xxx [OPTIONS]
Mounting xxx4FF3C67E at http://127.0.0.1:3000/xxx [POST]
Mounting None at http://127.0.0.1:3000/xxx [OPTIONS]
Mounting xxxF73A144E at http://127.0.0.1:3000/xxx [POST]
Mounting None at http://127.0.0.1:3000/xxx [OPTIONS]
Mounting xxx78EA90C0 at http://127.0.0.1:3000/xxx [POST]
Mounting None at http://127.0.0.1:3000/xxx [OPTIONS]
Mounting xxx2BE3C117 at http://127.0.0.1:3000/xxx [POST]
Mounting None at http://127.0.0.1:3000/xxx[OPTIONS]
Mounting xxx7CB472B4 at http://127.0.0.1:3000/xxx[POST]
You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. You only need to restart SAM CLI if you update your AWS SAM template

Observed result:

Expected result:

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: MACOS Catalina 10.15.6
  2. sam --version: SAM CLI, version 1.22.0
  3. AWS region:us-west-2

Add --debug flag to command you are running

@aahung
Copy link
Contributor

aahung commented Apr 12, 2021

This is the expected behavior, if a lambda function's Code point to a S3 location, you cannot invoke the function using sam local start-api because sam-cli currently does not support it.

@lidong-jimi
Copy link
Author

@aahung
thanks a lot, Can you tell me the solution!
I don't know why point to a S3 location

@jfuss
Copy link
Contributor

jfuss commented Apr 13, 2021

@lidong-jimi Are you by chance using CDK to generate a template to test locally with?

@aahung
Copy link
Contributor

aahung commented Apr 13, 2021

@lidong-jimi or is the template the output of sam package? The packaged template also have the S3 URLs instead of local paths.

@lidong-jimi
Copy link
Author

lidong-jimi commented Apr 14, 2021

@jfuss @aahung
Thanks for you reply...

My workflow:

my-application$ cd cdk
   cdk$ export S3_BUCKET= S3ArtifactBucketPolicy
   cdk$ cdk synth --no-staging > ../template.yaml

Generate template.yaml:

calculation4FF3C67E:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        S3Bucket: S3ArtifactBucketPolicy
        S3Key: undefined/function-code.zip
      Handler: src/handlers/calculation.handler
      Role:
        Fn::GetAtt:
          - calculationServiceRole297FB9CA
          - Arn
      Runtime: nodejs10.x
      Description: ""
    DependsOn:
      - calculationServiceRole297FB9CA
    Metadata:
      aws:cdk:path: CdkStack/calculation/Resource

In this case, sam local start-api cannot be used

if delete:

Code:
     S3Bucket: S3ArtifactBucketPolicy
     S3Key: undefined/function-code.zip

In this case, sam local start-api can be used

@jfuss jfuss reopened this Apr 14, 2021
@jfuss
Copy link
Contributor

jfuss commented Apr 14, 2021

@lidong-jimi Thanks for reporting. This is unexpected behavior. I think this was introduced in #2756. Let me dig a little further but what I think is happening is we are validating S3 uri/objects before resolving the Metadata. It is true that we cannot run locally if the CodeUri is S3 but with CDK, the generated output places some keys in the Metadata section that SAM CLI uses to resolve where the local assets are.

I will report back here once I confirm what exactly is happening.

@jfuss
Copy link
Contributor

jfuss commented Apr 14, 2021

@lidong-jimi So looking at the CDK output, there are pieces of information that CDK is not generating at all. There should be aws:asset:path and aws:asset:property in the Metadata section. How are you wiring up the local code in CDK?

CDK has a walk through of this here: https://docs.aws.amazon.com/cdk/latest/guide/sam.html Have you followed this?

From what I can tell, we are solving things correctly (just parsing the code). I am trying to setup cdk locally myself but currently node on my machine is all messed up.

@jfuss
Copy link
Contributor

jfuss commented Apr 14, 2021

Following the docs I linked above, I get the following resources generated by CDK:

MyFunction3BAA72D1:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        S3Bucket:
          Ref: AssetParametersf0b7aef3bb5af4ef18214c7c998e5c9cbd12402c869e73380c09825ef13b0d97S3Bucket02E91E48
        S3Key:
          Fn::Join:
            - ""
            - - Fn::Select:
                  - 0
                  - Fn::Split:
                      - "||"
                      - Ref: AssetParametersf0b7aef3bb5af4ef18214c7c998e5c9cbd12402c869e73380c09825ef13b0d97S3VersionKey1CEAABD3
              - Fn::Select:
                  - 1
                  - Fn::Split:
                      - "||"
                      - Ref: AssetParametersf0b7aef3bb5af4ef18214c7c998e5c9cbd12402c869e73380c09825ef13b0d97S3VersionKey1CEAABD3
      Role:
        Fn::GetAtt:
          - MyFunctionServiceRole3C357FF2
          - Arn
      Handler: app.lambda_handler
      Runtime: python3.7
    DependsOn:
      - MyFunctionServiceRole3C357FF2
    Metadata:
      aws:cdk:path: CdkSamExampleStack/MyFunction/Resource
      aws:asset:path: /Users/jfuss/cdk-sam-example/my_function
      aws:asset:property: Code

This template does local invoke correctly by doing sam local invoke. So it seems like your cdk code @lidong-jimi is incorrect. You will need to use the lambda.Code.asset('./my_function'), to point the function to a local asset. CDK will then generate the correct aws:asset:path and aws:asset:property in the template that SAM CLI can read and use.

@jfuss jfuss closed this as completed Apr 14, 2021
@lidong-jimi
Copy link
Author

@jfuss
Thank you!
Let me have a try!!!

@lidong-jimi
Copy link
Author

lidong-jimi commented Apr 15, 2021

@jfuss
I tried it your way.

CDK:

const valcheckFunction=  new lambda.Function(this, 'valcheck', {
            description: '',
            handler: 'valcheck.handler',
            runtime: lambda.Runtime.NODEJS_10_X,
            code: lambda.Code.fromAsset(
                '../src/handlers'
            ),
            timeout: Duration.seconds(60)
        });

Generate template.yaml:

valcheckF73A144E:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        S3Bucket:
          Ref: AssetParametersec584f09027af8ada830a4fe1d49ad6b4a0ba2f7c6391504c209f76b2b572a36S3BucketE98D9FB7
        S3Key:
          Fn::Join:
            - ""
            - - Fn::Select:
                  - 0
                  - Fn::Split:
                      - "||"
                      - Ref: AssetParametersec584f09027af8ada830a4fe1d49ad6b4a0ba2f7c6391504c209f76b2b572a36S3VersionKeyC2BA0C2F
              - Fn::Select:
                  - 1
                  - Fn::Split:
                      - "||"
                      - Ref: AssetParametersec584f09027af8ada830a4fe1d49ad6b4a0ba2f7c6391504c209f76b2b572a36S3VersionKeyC2BA0C2F
      Handler: valcheck.handler
      Role:
        Fn::GetAtt:
          - valcheckServiceRole44A03F8B
          - Arn
      Runtime: nodejs10.x
      Description: ""
      Timeout: 60
    DependsOn:
      - valcheckServiceRole44A03F8B
    Metadata:
      aws:cdk:path: CdkStack/valcheck/Resource
      aws:asset:path: /Users/xx/Desktop/project_name/src/handlers
      aws:asset:property: Code

valcheck file:

const { RangeVal } = require('../constants');
const { validationCheck, validationCheckDate } = require('../utils');

exports.handler = async (event) => {}

When sam local invoke valcheckF73A144E:

{"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module '../constants'","stack":["Runtime.ImportModuleError: Error: Cannot find module '../constants'","    at _loadUserApp (/var/runtime/UserFunction.js:100:13)","    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)","    at Object.<anonymous> (/var/runtime/index.js:45:30)","    at Module._compile (internal/modules/cjs/loader.js:778:30)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)","    at Module.load (internal/modules/cjs/loader.js:653:32)","    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)","    at Function.Module._load (internal/modules/cjs/loader.js:585:3)","    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)","    at startup (internal/bootstrap/node.js:283:19)"]}

Do you need to configure other options?

@jfuss
Copy link
Contributor

jfuss commented Apr 15, 2021

@lidong-jimi Something is wrong with how you have setup the code. The asset is the root of the files for the lambda function. So ../src/handlers gets mounted into the container and I would assume ../constants isn't there because the root is src/handlers not handlers.

@jankammerath
Copy link

jankammerath commented Apr 22, 2021

This is the expected behavior, if a lambda function's Code point to a S3 location, you cannot invoke the function using sam local start-api because sam-cli currently does not support it.

I would like to point out that this worked in 1.15.0 and it was a vital part of my workflow, hence SAM CLI is broken for me now and I need to create a dedicated SAM template in addition to my CF templates or what is the workaround since the functionality is removed?

Any recommendations are highly appreciated. See my issue #2825

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants