aws-sam-cli icon indicating copy to clipboard operation
aws-sam-cli copied to clipboard

sam-beta-cdk local start-api unable to find bucket created by CDK

Open seandkendall opened this issue 5 years ago • 3 comments

Description:

Scenario: Using CDK to create an API Gateway integration to lambda which adds/removes a file from an S3 bucket; where all resources, including the s3 bucket is created via CDK.

When running sam-beta-cdk local start-api, and executing a CURL to execute the lambda function which interacts with the s3 bucket, the lambda function is attempting to interact with a bucket that does not exist. Instead of using the CDK generated s3 bucket name, its trying to use the ID that was set in the CDK construct

Steps to reproduce:

Setup the example project using CDK from: https://docs.aws.amazon.com/cdk/latest/guide/serverless_example.html once set-up is complete, execute the command sam-beta-cdk local start-api execute an HTTP GET against the endpoint: curl -X GET 'http://127.0.0.1:3000/'

Observed result:

CURL result: NoSuchBucket: The specified bucket does not exist

Debug logs: 2021-05-20 10:54:52,108 | Constructed String representation of Event to invoke Lambda. Event: {"body": null, "headers": {"Accept": "/", "Host": "127.0.0.1:3000", "User-Agent": "curl/7.64.1", "X-Forwarded-Port": "3000", "X-Forwarded-Proto": "http"}, "httpMethod": "GET", "isBase64Encoded": false, "multiValueHeaders": {"Accept": ["/"], "Host": ["127.0.0.1:3000"], "User-Agent": ["curl/7.64.1"], "X-Forwarded-Port": ["3000"], "X-Forwarded-Proto": ["http"]}, "multiValueQueryStringParameters": null, "path": "/", "pathParameters": null, "queryStringParameters": null, "requestContext": {"accountId": "123456789012", "apiId": "1234567890", "domainName": "127.0.0.1:3000", "extendedRequestId": null, "httpMethod": "GET", "identity": {"accountId": null, "apiKey": null, "caller": null, "cognitoAuthenticationProvider": null, "cognitoAuthenticationType": null, "cognitoIdentityPoolId": null, "sourceIp": "127.0.0.1", "user": null, "userAgent": "Custom User Agent String", "userArn": null}, "path": "/", "protocol": "HTTP/1.1", "requestId": "a08ebbf2-f150-4cfe-939c-9676b3b1075f", "requestTime": "20/May/2021:16:54:03 +0000", "requestTimeEpoch": 1621529643, "resourceId": "123456", "resourcePath": "/", "stage": "prod"}, "resource": "/", "stageVariables": null, "version": "1.0"} 2021-05-20 10:54:52,109 | Found one Lambda function with name 'WidgetsWidgetHandler1BC9DB34' 2021-05-20 10:54:52,109 | Invoking widgets.main (nodejs10.x) 2021-05-20 10:54:52,109 | Environment variables overrides data is standard format 2021-05-20 10:54:52,109 | Resolving code path. Cwd=/Users//Documents/git/samcdkdemo, CodeUri=/Users//Documents/git/samcdkdemo/resources 2021-05-20 10:54:52,109 | Resolved absolute path to code is /Users//Documents/git/samcdkdemo/resources 2021-05-20 10:54:52,109 | Code /Users//Documents/git/samcdkdemo/resources is not a zip/jar file 2021-05-20 10:54:53,416 | Failed to download image with name amazon/aws-sam-cli-emulation-image-nodejs10.x:rapid-1.22.0.dev202104291816 2021-05-20 10:54:53,416 | Failed to download a new amazon/aws-sam-cli-emulation-image-nodejs10.x:rapid-1.22.0.dev202104291816 image. Invoking with the already downloaded image. 2021-05-20 10:54:53,416 | Mounting /Users//Documents/git/samcdkdemo/resources as /var/task:ro,delegated inside runtime container 2021-05-20 10:54:53,905 | Starting a timer for 3 seconds for function 'WidgetsWidgetHandler1BC9DB34' START RequestId: 3b30b9e9-564d-477c-875a-1b9c7b353a72 Version: $LATEST 2021-05-20T16:54:54.449Z 3b30b9e9-564d-477c-875a-1b9c7b353a72 INFO BucketName: WidgetsWidgetStore0ED7FDB7 END RequestId: 3b30b9e9-564d-477c-875a-1b9c7b353a72 REPORT RequestId: 3b30b9e9-564d-477c-875a-1b9c7b353a72 Init Duration: 0.28 ms Duration: 914.15 ms Billed Duration: 1000 ms Memory Size: 128 MB Max Memory Used: 128 MB 2021-05-20 10:54:55,010 | Cleaning all decompressed code dirs 2021-05-20 10:54:55,010 | No Content-Type given. Defaulting to 'application/json'. 2021-05-20 10:54:55 127.0.0.1 - - [20/May/2021 10:54:55] "GET / HTTP/1.1" 400 -

Expected result:

The expected result would be the local start-api interacting with the actual S3 bucket with the bucketName: 'samcdkdemostack-widgetswidgetstore0ed7fdb7-2dn1ldpt33u0h' instead of 'WidgetsWidgetHandler1BC9DB34'

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

  1. OS: MaxOS Catalina - 10.15.7 (19H1030)
  2. sam --version: SAM CLI, version 1.23.0
  3. CDK Version: 1.105.0 (build 4813992)
  4. sam-beta-cdk version: SAM CLI, version 1.22.0.dev202104291816
  5. AWS region: us-east-1

Add --debug flag to command you are running

seandkendall avatar May 20 '21 17:05 seandkendall

Hi @seandkendall, thanks for reporting the issue. From the example you provided, it seems like the bucket name is passed into the Lambda Function via the Env Var BUCKET:

    const handler = new lambda.Function(this, "WidgetHandler", {
      runtime: lambda.Runtime.NODEJS_10_X, // So we can use async in widget.js
      code: lambda.Code.fromAsset("resources"),
      handler: "widgets.main",
      environment: {
        BUCKET: bucket.bucketName
      }
    });

When running sam-beta-cdk local start-api, it won't create any resource that you've defined in the CDK definitions. So the bucket you've defined will not be available until you deploy it. However, you can create another S3 bucket out of the CDK definitions, and then override the BUCKET Env Var:

sam-beta-cdk local start-api --env-vars env.json

Note that you need to create a json file to contains the values to override the Env Vars. For example, the json file should look like this:

{
  "WidgetHandler": {
    "BUCKET": "an-existing-bucket-used-for-testing"
  }
}

See here to learn more about the env var json file.

Hope this helps. Feel free to let me know if you still have questions :)

hawflau avatar May 20 '21 20:05 hawflau

Closing due to inactivity

hawflau avatar Oct 15 '21 00:10 hawflau

⚠️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.

github-actions[bot] avatar Oct 15 '21 00:10 github-actions[bot]