我正在使用无服务器框架在 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/