Skip to content

AWS SAM local start-api cannot resolve Fn::ImportValue

0

Once in a while SAM’s “local-api” command will give an error (see below). The best I can make of the root cause is that the cdk output template for the Lambda Integrations stack contains “Fn:ImportValue” commands with variable names that aren’t resolved in the same template file. That being said, the cdk.out template usually works just fine. (I cannot verify if the FN:ImportValue are different when it’s working fine – as I usually don’t look when things are working). I’m not sure how we’ve resolved this in the past every time, but the last time it happened, I uninstalled/reinstalled CDK globally, recreated the node_modules folder for the CDK project, and deleted the cdk.out folder. “cdk synth” produced working outputs after that.

My issue is identical to the one posted on https://stackoverflow.com/questions/59928691/aws-sam-local-start-api-cannot-resolve-fnimportvalue

What can I do to resolve this error? I am aware I can pass these values as environment variables, but I am looking for a long term fix instead of a workaround. Thanks in advance.

1 Answer
0

The issue you're experiencing with AWS SAM local start-api not being able to resolve Fn::ImportValue is a known limitation when testing CDK applications locally. This occurs because the Fn::ImportValue function is designed to work with deployed CloudFormation stacks, and local testing environments don't have access to these deployed resources.

To address this issue, there are a few approaches you can take:

  1. Ensure you're running the latest versions of AWS CDK and AWS SAM CLI. Sometimes, updates to these tools can resolve compatibility issues.

  2. Before running any SAM local commands with a CDK application, always run 'cdk synth' first. This ensures that your CloudFormation template is up-to-date with your latest CDK code changes.

  3. When using SAM local commands with CDK applications, make sure to specify the path to your synthesized CloudFormation template. For example:

    sam local start-api -t ./cdk.out/YourStackName.template.json

  4. If your application uses nested stacks, you may need to provide the stack name where the function is defined to resolve naming conflicts.

  5. Consider using the 'sam local start-lambda' command instead of 'sam local start-api' if you're primarily testing Lambda functions. This command starts a local endpoint that emulates AWS Lambda and may be less prone to issues with resolving CloudFormation intrinsic functions.

  6. As a last resort, if the issue persists, you can try the approach you mentioned: uninstalling and reinstalling CDK globally, recreating the node_modules folder, and deleting the cdk.out folder before running 'cdk synth' again.

While these steps can help mitigate the issue, it's important to note that local testing of CDK applications with SAM has limitations, particularly when it comes to resolving certain CloudFormation functions. For the most accurate testing, consider deploying your application to a test environment in the cloud.
Sources
Locally testing AWS CDK applications with AWS SAM - AWS Serverless Application Model
Introduction to testing with sam local start-lambda - AWS Serverless Application Model

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.