gpt4 book ai didi

amazon-web-services - 在 cloudformation 模板中使用 env 变量

转载 作者:行者123 更新时间:2023-12-03 07:33:40 25 4
gpt4 key购买 nike

我有一个带有多个分支的放大应用程序。

我使用 amplify add custom 添加了自定义 cloudformation 模板

看起来像:

{
"AWSTemplateFormatVersion": "2010-09-09",
"Parameters": { "env": { "Type": "String" } },
"Resources": {
"db": {
"Type": "AWS::Timestream::Database",
"Properties": { "DatabaseName": "dev_db" }
},
"timestreamtable": {
"DependsOn": "db",
"Type": "AWS::Timestream::Table",
"Properties": {
"DatabaseName": "dev_db",
"TableName": "avg_16_4h",
"MagneticStoreWriteProperties": { "EnableMagneticStoreWrites": true },
"RetentionProperties": {
"MemoryStoreRetentionPeriodInHours": "8640",
"MagneticStoreRetentionPeriodInDays": "1825"
}
}
}
},
"Outputs": {},
"Description": "{\"createdOn\":\"Windows\",\"createdBy\":\"Amplify\",\"createdWith\":\"8.3.1\",\"stackType\":\"custom-customCloudformation\",\"metadata\":{}}"
}

您可以看到有一个名为DatabaseName的字段。在我的 amplify 应用程序中,我编写了一个名为 TIMESTREAM_DB 的环境变量,我想在此 cloudformation 文件中使用它。

这可能吗?还是我需要手动将其全部写入其中?

最佳答案

模板无法访问任意环境变量。相反,CloudFormation 将部署时值注入(inject)到模板中 Parameters .

Amplify 有助于将 env 变量添加为参数。啦啦Amplify docs ,使用 env 值作为 AWS::Timestream::Database 名称后缀:

"DatabaseName": "Fn::Join": [ "", [ "my-timestream-db-name-", { "Ref": "env" } ] ]

AWS::Timestream::Table 资源还需要 DatabaseName 参数。您可以重复上面的内容,但获取名称 via the Database's Ref 会更干燥。 :

"DatabaseName": { "Ref" : "db" }

关于amazon-web-services - 在 cloudformation 模板中使用 env 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72458808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com