amazon-web-services - 在 Cloudformation 模板中传递安全组 ID 和子网 ID

标签 amazon-web-services yaml aws-cloudformation aws-cli

Parameters:
  ClusterName:
    Type: String
  ClusterVersion:
    Type: Number
    AllowedValues: [1.21, 1.20, 1.19, 1.18]
  RoleArnValue:
    Type: String
  ListOfSubnetIDs: 
    Description: Array of Subnet IDs
    Type: List<AWS::EC2::Subnet::Id>
  ListOfSecurityGroupIDs:
    Description: Array of security group ids
    Type: List<AWS::EC2::SecurityGroup::Id>


Resources:
  EKSCluster:
    Type: AWS::EKS::Cluster
    Properties:
      Name: !Sub ${ClusterName}
      Version: !Sub ${ClusterVersion}
      RoleArn: !Sub ${RoleArnValue}
      ResourcesVpcConfig:
        SecurityGroupIds: 
          - !Sub ${ListOfSecurityGroupIDs}
        SubnetIds:
          - !Sub ${ListOfSubnetIDs}                  

上面是我创建的 .yaml cloudformation 模板,以便我可以启动 eks 集群。然后我使用 aws cli 通过以下命令启动集群。

aws cloudformation deploy --template-file eks.yaml --stack-name cluster-test --parameter-overrides ClusterName=Dev ClusterVersion=1.21 ListOfSubnetIDs=subnet-11111d11b11b011f4,subnet-99999d237f87f11d7,subnet-222222c110c7e4be7,subnet-88888884de8d25176  ListOfSecurityGroupIDs=sg-01111111a21221 RoleArnValue=arn:aws:iam::123456546456:role/cluster-ServiceRole-WMIC72AOWSP0 --capabilities CAPABILITY_NAMED_IAM

我收到以下错误

An error occurred (ValidationError) when calling the CreateChangeSet operation: Template error: variable ListOfSecurityGroupIDs in Fn::Sub expression does not resolve to a string

我不知道为什么。我正确使用 !sub 吗?非常感谢对此的意见。

最佳答案

由于您想按原样引用您在模板中提供的参数,因此您应该使用 Ref 函数。

以下是有效模板的示例:

Parameters:
  ClusterName:
    Type: String
  RoleArnValue:
    Type: String
  ListOfSubnetIDs: 
    Description: Array of Subnet IDs
    Type: List<AWS::EC2::Subnet::Id>
  ListOfSecurityGroupIDs:
    Description: Array of security group ids
    Type: List<AWS::EC2::SecurityGroup::Id>


Resources:
  EKSCluster:
    Type: AWS::EKS::Cluster
    Properties:
      Name: !Ref ClusterName
      RoleArn: !Ref RoleArnValue
      ResourcesVpcConfig:
        SecurityGroupIds: !Ref ListOfSecurityGroupIDs
        SubnetIds: !Ref ListOfSubnetIDs

这是我的部署方式:

aws cloudformation deploy --template-file eks.yml --stack-name cluster-test --parameter-overrides ClusterName=Dev ListOfSubnetIDs=subnet-be0a99c4,subnet-c71046ae ListOfSecurityGroupIDs=sg-009690ac6b3bff6df,sg-009a3f1cb63943941 -RoleArnValue=...
当您想要执行字符串操作时,应使用

Sub。查看 documentation 中的示例.

关于amazon-web-services - 在 Cloudformation 模板中传递安全组 ID 和子网 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71680369/

相关文章:

java - 如何将 Java 枚举与 Amazon DynamoDB 和 AWS SDK v2 结合使用?

azure-devops - Azure 数据工厂 YAML 部署因 overrideParameter 键包含空格而失败

amazon-web-services - 模板资源属性无效 'Ref'

amazon-web-services - AWS CloudFormation 模板格式错误 : Unresolved resource dependencies <s3 bucket> in the Resources block of the template

amazon-web-services - 通过 cloudformation 在 s3 存储桶上启用对象日志记录

amazon-web-services - AWS KMS 签名为我的 JWT 返回无效签名

javascript - s3.getObject(...).createReadStream 不是函数

javascript - 将node.js应用程序部署到elastic beanstalk(使用express)

python - 如何为 Python 安装 yaml 包?

amazon-web-services - 如何提取AWS CloudFormation模板中的公共(public)行?