【问题标题】:AWS SAM local start-api cannot resolve Fn::ImportValueAWS SAM 本地启动 API 无法解析 Fn::ImportValue
【发布时间】:2022-02-18 03:34:53
【问题描述】:

我有 SAM 模板(部分张贴在这里):

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"

命令给我一个通知

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.

我试图删除 Sub(没有运气):

SOURCE_TABLE_NAME:
  Fn::ImportValue: "storage-dev-SourceTableName"

模板在服务器上工作,因此支持 Fn::ImportValue。 所以我的问题是本地调用是否支持 Fn::ImportValue?

我确保我对本地 SAM 使用的凭据(配置文件)与我拥有 storage-dev 堆栈的凭据(配置文件)相同。有什么办法可以再次检查以确保更多?

【问题讨论】:

    标签: amazon-web-services aws-sam serverless-application-model


    【解决方案1】:

    从这篇文章开始,它似乎不受支持。我能找到的最后一个 PR 只包含 !Sub 和 !If。看起来它们必须在 sam cli 成熟时作为参数或环境变量传入。

    【讨论】:

      【解决方案2】:

      这是一个 SAM 错误,我希望也能尽快修复。现在,我创建了一个解决方法。它是一个 bash 文件,它从另一个堆栈中获取导出的输出值,并在调用期间将其作为 env 变量传递,因此它将被覆盖。

      只需将 EXPORTED_VARIABLE_NAME 和 S3_BUCKET_NAME 替换为您的实际姓名即可。

      像这样运行它:./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
      

      【讨论】:

        猜你喜欢
        • 2018-05-09
        • 2022-10-05
        • 2021-08-10
        • 1970-01-01
        • 2020-09-05
        • 1970-01-01
        • 1970-01-01
        • 2021-08-08
        • 1970-01-01
        相关资源
        最近更新 更多