如何在CloudFormation模板中创建一些随机或唯一值? [英] How to create some random or unique value in a CloudFormation template?

查看:79
本文介绍了如何在CloudFormation模板中创建一些随机或唯一值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以在CloudFormation模板中创建某种随机或唯一值?

Is there a way to create some kind of random or unique value in a CloudFormation template?

为什么我需要这个?在我们的模板中,我们有许多自定义名称的资源,例如 AWS :: AutoScaling :: LaunchConfiguration 具有指定的 LaunchConfigurationName AWS :: AutoScaling :: AutoScalingGroup 与指定的 AutoScalingGroupName

更新堆栈时,我们经常会遇到以下错误:

Why I need this. In our templates we have a number of custom-named resources, for instance AWS::AutoScaling::LaunchConfiguration with specified LaunchConfigurationName or AWS::AutoScaling::AutoScalingGroup with specified AutoScalingGroupName.
When updating stacks, we often get the following error:


当需要替换自定义名称的资源时,CloudFormation无法更新堆栈。重命名一些堆栈启动配置并再次更新堆栈。

CloudFormation cannot update a stack when a custom-named resource requires replacing. Rename some-stack-launch-configuration and update the stack again.

我们不想重命名资源,只是因为我们需要更新它们。

我们也不想在资源中删除自定义名称。但是,我们不介意在自定义名称中添加一些随机后缀。

We don't want to rename resources just because we need to update them.
We also don't want to drop custom names in our resources. We won't mind however having some random suffix in our custom names.

使用随机数生成器,解决方案可能类似于:

With a "random generator" the solution might look something like:

  MyAutoScalingGroup:
    Type: 'AWS::AutoScaling::AutoScalingGroup'
    Properties:
      AutoScalingGroupName: !Sub 'my-auto-scaling-group-${AWS::Random}'


推荐答案

我认为您需要创建一个 Lambda 函数来执行此操作。

I think you need to create a Lambda function to do this.

这是一个GitHub项目<一个href = https://github.com/sophos/cloudformation-random-string rel = nofollow noreferrer> cloudformation-random-string ,它具有Lambda函数和一个简单的教程。

Here's a GitHub project cloudformation-random-string, which has a Lambda function and a simple tutorial.

这是另一个教程在AWS CloudFormation模板中生成密码

您可以引用上面的Lambda函数并将其用于你。

You can refer to the Lambda function above and make it work for you.

这篇关于如何在CloudFormation模板中创建一些随机或唯一值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆