Skip to content
v2.278.0

CfnJson ​

Captures a synthesis-time JSON object a CloudFormation reference which resolves during deployment to the resolved values of the JSON object.

The main use case for this is to overcome a limitation in CloudFormation that does not allow using intrinsic functions as dictionary keys (because dictionary keys in JSON must be strings). Specifically this is common in IAM conditions such as StringEquals: { lhs: "rhs" } where you want "lhs" to be a reference.

This object is resolvable, so it can be used as a value.

This construct is backed by a custom resource.

Example ​

ts
const tagParam = new CfnParameter(this, 'TagName');

const stringEquals = new CfnJson(this, 'ConditionJson', {
  value: {
    [`aws:PrincipalTag/${tagParam.valueAsString}`]: true,
  },
});

const principal = new iam.AccountRootPrincipal().withConditions({
  StringEquals: stringEquals,
});

new iam.Role(this, 'MyRole', { assumedBy: principal });
python
tag_param = CfnParameter(self, "TagName")

string_equals = CfnJson(self, "ConditionJson",
    value={
        "f"aws:PrincipalTag/{tagParam.valueAsString}"": True
    }
)

principal = iam.AccountRootPrincipal().with_conditions({
    "StringEquals": string_equals
})

iam.Role(self, "MyRole", assumed_by=principal)
java
CfnParameter tagParam = new CfnParameter(this, "TagName");

CfnJson stringEquals = CfnJson.Builder.create(this, "ConditionJson")
        .value(Map.of(
                String.format("aws:PrincipalTag/%s", tagParam.getValueAsString()), true))
        .build();

PrincipalBase principal = new AccountRootPrincipal().withConditions(Map.of(
        "StringEquals", stringEquals));

Role.Builder.create(this, "MyRole").assumedBy(principal).build();
csharp
// Install: dotnet add package Amazon.CDK.Lib
var tagParam = new CfnParameter(this, "TagName");

var stringEquals = new CfnJson(this, "ConditionJson", new CfnJsonProps {
    Value = new Dictionary<string, boolean> {
        { $"aws:PrincipalTag/{tagParam.valueAsString}", true }
    }
});

var principal = new AccountRootPrincipal().WithConditions(new Dictionary<string, object> {
    { "StringEquals", stringEquals }
});

new Role(this, "MyRole", new RoleProps { AssumedBy = principal });
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
tagParam := awscdk.NewCfnParameter(this, jsii.String("TagName"))

stringEquals := awscdk.NewCfnJson(this, jsii.String("ConditionJson"), &CfnJsonProps{
	Value: map[string]*bool{
		fmt.Sprintf("aws:PrincipalTag/%v", tagParam.valueAsString): jsii.Boolean(true),
	},
})

principal := iam.NewAccountRootPrincipal().WithConditions(map[string]interface{}{
	"StringEquals": stringEquals,
})

iam.NewRole(this, jsii.String("MyRole"), &RoleProps{
	AssumedBy: principal,
})

Initializer ​

ts
new CfnJson(scope: Construct, id: string, props: CfnJsonProps)
python
CfnJson(scope: Construct, id: str, props: CfnJsonProps)
java
new CfnJson(Construct scope, String id, CfnJsonProps props)
csharp
new CfnJson(Construct scope, string id, CfnJsonProps props)
go
NewCfnJson(Scope Construct, Id *string, Props CfnJsonProps)

Construct Props ​

valueanyvalueAnyvalueObjectValueobjectValueinterface{} required #
The value to resolve.

Can be any JavaScript object, including tokens and references in keys or values.

Properties ​

creationStackstring[]creation_stacktyping.List[str]creationStackList<String>CreationStackstring[]CreationStack*[]*string #
The creation stack of this resolvable which will be appended to errors thrown during resolution.

This may return an array with a single informational element indicating how to get this property populated, if it was skipped for performance reasons.

nodeNodenodeNodenodeNodeNodeNodeNodeNode #
The tree node.
An Fn::GetAtt to the JSON object passed through `value` and resolved during synthesis.

Normally there is no need to use this property since CfnJson is an IResolvable, so it can be simply used as a value.

Methods ​

isConstruct(x: any): boolean@staticmethod def is_construct(x: Any) -> boolpublic static Boolean isConstruct(Object x)public static bool IsConstruct(object x)func IsConstruct(X interface{}) *bool static #
Checks if `x` is a construct.
notes · 1 parameter · returns

Use this method instead of instanceof to properly detect Construct instances, even when the construct library is symlinked.

Explanation: in JavaScript, multiple copies of the constructs library on disk are seen as independent, completely different libraries. As a consequence, the class Construct in each copy of the constructs library is seen as a different class, and an instance of one class will not test as instanceof the other class. npm install will not create installations like this, but users may manually symlink construct libraries together or use a monorepo tool: in those cases, multiple copies of the constructs library can be accidentally installed, and instanceof will behave unpredictably. It is safest to avoid using instanceof, and using this type-testing method instead.

Parameters

  • xxxxX — Any object.

Returns: true if x is an object created from a class which extends Construct.

resolve(_context: IResolveContext): anydef resolve(self, _context: IResolveContext) -> Anypublic Object resolve(IResolveContext _context)public object Resolve(IResolveContext _context)func Resolve(_context IResolveContext) interface{} #
Produce the Token's value at resolution time.
toJSON(): stringdef to_json(self, ) -> strpublic String toJSON()public string ToJSON()func ToJSON() *string #
This is required in case someone JSON.stringifys an object which references this object. Otherwise, we'll get a cyclic JSON reference.
toString(): stringdef to_string(self, ) -> strpublic String toString()public string ToString()func ToString() *string #
Returns a string representation of this construct.
with(...mixins: IMixin[]): IConstructdef with(self, mixins: IMixin) -> IConstructpublic IConstruct with(IMixin mixins)public IConstruct With(IMixin mixins)func With(Mixins IMixin) IConstruct #
Applies one or more mixins to this construct.

Mixins are applied in order. The list of constructs is captured at the start of the call, so constructs added by a mixin will not be visited. Use multiple with() calls if subsequent mixins should apply to added constructs.

1 parameter · returns

Parameters

  • mixinsmixinsmixinsmixinsMixins — The mixins to apply.

Returns: This construct for chaining