amazon-web-services - 如何在 AWS CloudFormation 中使用 sub 进行参数化?

标签 amazon-web-services aws-cloudformation aws-cloudformation-custom-resource

{
   "AWSTemplateFormatVersion":"2010-09-09",
   "Parameters":{
      "Prefix":{
         "Type":"String",
         "Description":"Prefix to add to the names of the IAM resources",
         "Default":"my-prefix",
         "MinLength": "3",
         "MaxLength": "9",
         "ConstraintDescription": "Prefix must be between 3 and 9 characters in length and must be a String."
      }
   },
   "Resources":{
      "RkVpcPolicyGroup":{
         "Type":"AWS::IAM::Group",
         "Properties":{
            "GroupName":{
               "!Sub":"${Prefix}-evpc-policy-group"
            }
         }
      },
      "RkVpcEksctlPolicyGroup":{
         "Type":"AWS::IAM::Group",
         "Properties":{
            "GroupName":{
               "!Sub":"${Prefix}-evpc-eksctl-policy-group"
            }
         }
      },
      "RkExistingVpcServiceAccountUser":{
         "Type":"AWS::IAM::User",
         "Properties":{
            "UserName":{
               "!Sub":"${Prefix}-existing-vpc-sa"
            },
            "Groups":[
               {
                  "Ref":"RkVpcPolicyGroup"
               },
               {
                  "Ref":"RkVpcEksctlPolicyGroup"
               }
            ]
         }
      }
   }
}

我尝试使用 sub 函数向 IAM 资源名称添加前缀,但上面的代码对于逻辑 ID 的 RkVpcEksctlPolicyGroup 和 RkVpcPolicyGroup 都抛出“属性 GroupName 的值必须为字符串类型”错误。我在这里缺少什么?

最佳答案

您应该使用Sub完整形式。因此,对于 RkVpcPolicyGroup 来说,它应该是:

      "RkVpcPolicyGroup":{
         "Type":"AWS::IAM::Group",
         "Properties":{
            "GroupName":{
               "Fn::Sub":["${Prefix}-evpc-policy-group", {"Prefix": {"Ref": "Prefix"}}]
            }
         }
      }

必须对另一组进行类似的更改。

关于amazon-web-services - 如何在 AWS CloudFormation 中使用 sub 进行参数化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75989780/

相关文章:

amazon-web-services - 无法使用 aws cli 通过 CloudFormation 创建堆栈,但可以在门户中

amazon-web-services - 集成响应无法映射项目数组

amazon-web-services - 如何保护实例角色的 S3 存储桶?

amazon-web-services - Cloudformation 是否支持 API Gateway 中的“使用代理集成”选项?

amazon-web-services - 将现有的 AWS secret 引入 Cloudformation 堆栈

amazon-web-services - 如何显示来自自定义 Lambda 的资源

amazon-web-services - 如何在不同的 cloudformation 堆栈中分离 dynamodb 表及其 GSI?

java - 我想设置 s3 对象特定保留

aws-cloudformation - 使用 CloudFormation 读取 secret

amazon-web-services - AWS Cloudformation 快速创建链接不提供其他选项