amazon-web-services - AWS Serverless,CloudFormation : Error, 尝试将非字符串值填充到变量的字符串中

标签 amazon-web-services aws-cloudformation serverless-framework aws-serverless

我正在使用无服务器框架在 AWS 云上部署我的应用程序。

https://serverless.com/

我想要在 serverless.yml 文件中使用 AWS 账户 ID 的值,并且想要将账户 ID 导出为环境变量,以便可以从 Lambda 函数访问它。

根据此 lambda 函数的值,我想创建一些引用此 accountId 变量的资源(如 IAM 角色等)。

但是当我尝试部署堆栈时,出现以下错误,

Trying to populate non string value into a string for variable ${self:custom.accountId}. Please make sure the value of the property is a string.

我的Serverless.yml文件如下

custom:
  accountId : !Ref "AWS::AccountId"


provider:
  name: aws
  runtime: go1.x
  stage: dev

  region: us-east-1

  environment:
     ACCOUNT_ID : ${self:custom.accountId}       
     myRoleArn: arn:aws:iam::${self:custom.accountId}:role/xxxxxxxx

有什么方法可以引用serverless.yml文件中Account Id的值吗?

最佳答案

您无法在 serverless.yml 中引用 AWS::AccountId,因为在创建 CloudFormation 模板时它无法完全转换。

解决方法是使用无服务器插件 Pseudo Parameters .

您可以使用 npm 安装该插件。

npm install serverless-pseudo-parameters

您还需要将该插件添加到 serverless.yml 文件中。

plugins:
  - serverless-pseudo-parameters

然后您可以使用 #{AWS::AccountId} 引用您的 AccountId

functions:
  helloWorld:
    handler: index.handler
    events:
      - http:
          path: /
          method: get
    environment:
      ACCOUNT_ID : #{AWS::AccountId}

请注意,引用以哈希而不是美元符号开头。

关于amazon-web-services - AWS Serverless,CloudFormation : Error, 尝试将非字符串值填充到变量的字符串中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58933941/

相关文章:

amazon-web-services - 如何使用 webpack 导入 aws-sdk

amazon-s3 - 无服务器框架从 CloudFormation 模板为 S3 存储桶创建额外字符

aws-lambda - AWS Lambda 完成的 HTTP 请求中的 "X-Forwarded-*" header 问题

aws-cloudformation - Cloudformation 创建加密的 athena 粘合表

amazon-web-services - 如何获取 Cloud Formation 特定资源属性的 AWS Lambda 函数的 ARN?

amazon-web-services - 监视 S3 存储桶在过去 x 小时内丢失新文件的最佳策略是什么?

node.js - 使用 AWS Cognito 和 AD FS 作为 Web 应用程序的身份验证

java - Android aws CognitoUserSession.getIdToken() 给出空指针异常

amazon-web-services - AWS 云形成 : The security group 'sg-' does not exist in default VPC 'vpc'

amazon-web-services - AWS : monitoring if service is running