'AWS SAM local start-api cannot resolve Fn::ImportValue
I have SAM template (post here partially):
AWSTemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"
Parameters:
StorageStackName:
Type: String
Description: Name of the stack which provisions DynamoDB table and S3 bucket.
Globals:
Function:
Runtime: nodejs12.x
MemorySize: 128
Timeout: 8
CodeUri: .
AutoPublishAlias: latest
Environment:
Variables:
SOURCE_TABLE_NAME:
Fn::ImportValue:
Fn::Sub: "${StorageStackName}-SourceTableName"
Command gives me a notification
sam local start-api --debug --parameter-overrides='StorageStackName=storage-dev'
Unable to resolve property SOURCE_TABLE_NAME: OrderedDict([('Fn::ImportValue', OrderedDict([('Fn::Sub', '${StorageStackName}-SourceTableName')]))]). Leaving as is.
I tried to remove Sub (no luck):
SOURCE_TABLE_NAME:
Fn::ImportValue: "storage-dev-SourceTableName"
The template works on the server, so Fn::ImportValue supported. So my question is Fn::ImportValue supported in local invocation at all?
I am made sure I use same credentials (profile) for local SAM as the one where I have storage-dev stack. Any way I can recheck it again to make sure even more?
Solution 1:[1]
It looks like as of this post it is not supported. The last PR I could find only included !Sub and !If. Looks like they will have to be passed in as parameters or env variables while the sam cli matures.
Solution 2:[2]
It is a SAM bug that I hope gets fixed soon also. For now, I created a workaround. It is a bash file that gets the exported output value from another stack and passes it as an env variable during invocation, so it will be overwritten.
Just substitute the EXPORTED_VARIABLE_NAME and S3_BUCKET_NAME for your actual names and you are good to go.
Run it like so: ./sam_local_invoke.sh
#!/bin/bash
# The exported variable name from another stack (change it with your variable name)
EXPORTED_VARIABLE_NAME=BotsDataBucketSAM
# Get it as the name of the parameter that we want to overwrite
S3_BUCKET_NAME=$(aws cloudformation list-exports --query "Exports[?Name=='$EXPORTED_VARIABLE_NAME'].Value" --output text | cat)
ENV_OVERWRITES=$(cat <<EOT
{
"Parameters": {
"S3_BUCKET_NAME": "$S3_BUCKET_NAME"
}
}
EOT
)
echo ${ENV_OVERWRITES} > env.json
sam local invoke --env-vars env.json
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | wattry |
| Solution 2 | Josep Alsina |
