Using dynamic references in AWS CloudFormation

Viewed 106

I am trying to create an IAM Role using CloudFormation. The following snippet shows the trust policy :

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Federated": "arn:aws:iam::1234567890123:oidc-provider/oidc.eks.us-east-1.amazonaws.com/id/89ABCDWEFGHRRUUUR4DDF70C"
      },
      "Action": "sts:AssumeRoleWithWebIdentity",
      "Condition": {
        "StringEquals": {
          "oidc.eks.us-east-1.amazonaws.com/id/89ABCDWEFGHRRUUUR4DDF70C:sub": "system:serviceaccount:kube-system:aws-load-balancer-controller"
        }
      }
    }
  ]
}

I want to make it dynamic, as in it should pick the value for OIDC provider from either mappings or parameters.

Here is what I have done so far:

"AssumeRolePolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Principal": {
                "Federated": {
                  "Fn::Join": [
                    "",
                    [
                      {
                        "Fn::Sub": "arn:aws:iam::${AWS::AccountId}:oidc-provider/"
                      },
                      {
                        "Fn::FindInMap": [
                          "Environments",
                          {
                            "Ref": "EnvironUsed"
                          },
                          "OIDCProvider"
                        ]
                      }
                    ]
                  ]
                }
              },
              "Action": "sts:AssumeRoleWithWebIdentity",
              "Condition": {
                "StringEquals": {
                  "oidc.eks.us-east-1.amazonaws.com/id/89738A59036B9EB2704B9ABC44DDF70C:sub" : "system:serviceaccount:kube-system:aws-load-balancer-controller"
                }
              }
            }
          ]
        }

I am unable to make the last part (Condition section where OIDC provider is mentioned) dynamic. The JSON invalidates when I try to include a Fn::Join within curly braces in place of "oidc.eks.us-east-1.amazonaws.com/id/89ABCDWEFGHRRUUUR4DDF70C:sub"

0 Answers
Related