【问题标题】:Using a Parameter value as a key in Cloudformation在 Cloudformation 中使用参数值作为键
【发布时间】:2020-03-23 12:54:41
【问题描述】:

AWS 在线文档和参考资料似乎并未表明可以允许 CloudFormation 模板密钥(在我的例子中为 YAML)由参数填充。

我需要创建 IAM 策略,将 AssumeRoleWithWebIdentity 锁定为仅对 EKS OIDC 提供商的 EKS 服务帐户。

Effect: "Allow"
Principal:
  Federated:
    - !Ref MasterARN
Condition:
  StringLike:
    !Ref MasterOIDC: !Ref ServiceAccount
Action:
 - "sts:AssumeRoleWithWebIdentity"

我可以理解为什么 CloudFormation 不允许这样做,因为您可以滥用“模板”,但我认为这是一个极端情况。

标签(键:值)通过让您在“名称”键(例如Name: key; Value: value)下传递键来解决此问题。

【问题讨论】:

  • 我之前也有同样的想法,找不到解决办法。我认为达​​到类似结果的最佳方式是通过custom resourcesmacros
  • 是的,我是这么认为的@Marcin - 我用 Terraform 配置了前几个,但我可能会查看 SDK 来生成模板,因为我们需要一些可重用的东西

标签: amazon-web-services amazon-iam amazon-eks


【解决方案1】:

如果我正确理解了您的问题(您希望条件名称取决于参数),您可以为此使用 PyPlate macro

AWSTemplateFormatVersion: "2010-09-09"
Transform: [PyPlate]
Description: A stack that provisions a bunch of s3 buckets based on param names
Parameters:
  CreateOne:
    Type: String
    Default: "a"
  CreateTwo:
    Type: String
    Default: "a"

Conditions: 
  |
    #!PyPlate
    output = {}
    for k in params:
      output[k] = {"Fn::Equals" : ["prod", "prod"]}

Resources:
  myBucket:
    Type: AWS::S3::Bucket
    Condition: CreateOne

处理后的模板将是:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "A stack that provisions a bunch of s3 buckets based on param names",
  "Parameters": {
    "CreateOne": {
      "Type": "String",
      "Default": "a"
    },
    "CreateTwo": {
      "Type": "String",
      "Default": "a"
    }
  },
  "Resources": {
    "myBucket": {
      "Type": "AWS::S3::Bucket",
      "Condition": "CreateOne"
    }
  },
  "Conditions": {
    "CreateOne": {
      "Fn::Equals": [
        "prod",
        "prod"
      ]
    },
    "CreateTwo": {
      "Fn::Equals": [
        "prod",
        "prod"
      ]
    }
  }
}

【讨论】:

猜你喜欢
  • 2020-11-17
  • 1970-01-01
  • 1970-01-01
  • 2016-05-14
  • 1970-01-01
  • 2019-06-01
  • 2018-05-29
  • 1970-01-01
  • 2022-01-22
相关资源
最近更新 更多