sam-beta-cdk local start-api unable to find bucket created by CDK
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/
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)
- OS: MaxOS Catalina - 10.15.7 (19H1030)
sam --version: SAM CLI, version 1.23.0- CDK Version: 1.105.0 (build 4813992)
- sam-beta-cdk version: SAM CLI, version 1.22.0.dev202104291816
- AWS region: us-east-1
Add --debug flag to command you are running
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 :)
Closing due to inactivity
⚠️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.