简体   繁体   English

从 AWS 全局变量中替换字符串中的变量

[英]Replacing variables in string from AWS globals

I am writing a sample app for CDK and writing some SSM values but want to replace the current region in the string value written based on global variables (eg {{global:REGION}} in the string. when synth / deploying a project, how do I lever environment / global context values in an existing string so that it writes the value into the string when pushing the values into AWS?我正在为 CDK 编写一个示例应用程序并编写一些 SSM 值,但想替换基于全局变量编写的字符串值中的当前区域(例如字符串中的 {{global:REGION}}。在合成/部署项目时,如何我是否可以利用现有字符串中的环境/全局上下文值,以便在将值推送到 AWS 时将值写入字符串?

eg ssmParamterValue = 'I am working in region {{global:REGION}} which should be written'例如 ssmParamterValue = '我在应该写的区域 {{global:REGION}} 工作'

I would expect the value above when written to SSM would be: 'I am working in region us-east-1 which should be written'我希望在写入 SSM 时上面的值是:'我在应该写入的区域 us-east-1 工作'

I have other environment values I am setting as well, but this is one of the globals available for testing.我还设置了其他环境值,但这是可用于测试的全局变量之一。

Thanks Nick谢谢尼克

If you are following the AWS recommendation to explicitly define env at synth-time * , you can reference the env string values using the Stack construct properties (eg Stack.region and Stack.account ).如果您遵循 AWS 建议在 synth-time *明确定义 env ,则可以使用Stack 构造属性(例如Stack.regionStack.account )引用 env 字符串值。

// MyStack.ts
const ssmParamterValue = `I am working in region ${this.region} which should be written`

* If you are instead using environment-agnostic stacks, these values will be tokens at synth-time and resolved to strings at deploy-time. * 如果您改为使用与环境无关的堆栈,这些值将在合成时成为标记,并在部署时解析为字符串。 CDK is usually smart enough to convert typescript interpolated strings to the CloudFormation Join equivalent. CDK 通常足够聪明,可以将 typescript 内插字符串转换为等效的 CloudFormation Join

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM