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.
| Source | core/lib/cfn-json.ts |
|---|---|
| Type last changed |
Example
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 });
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)
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();
// 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 });
// 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
new CfnJson(scope: Construct, id: string, props: CfnJsonProps)
CfnJson(scope: Construct, id: str, props: CfnJsonProps)
new CfnJson(Construct scope, String id, CfnJsonProps props)
new CfnJson(Construct scope, string id, CfnJsonProps props)
NewCfnJson(Scope Construct, Id *string, Props CfnJsonProps)
Construct Props
Can be any JavaScript object, including tokens and references in keys or values.
Properties
creationStackstring[]creation_stacktyping.List[str]creationStackList<String>CreationStackstring[]CreationStack*[]*string #This may return an array with a single informational element indicating how to get this property populated, if it was skipped for performance reasons.
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 #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{} #toJSON(): stringdef to_json(self, ) -> strpublic String toJSON()public string ToJSON()func ToJSON() *string #toString(): stringdef to_string(self, ) -> strpublic String toString()public string ToString()func ToString() *string #with(...mixins: IMixin[]): IConstructdef with(self, mixins: IMixin) -> IConstructpublic IConstruct with(IMixin mixins)public IConstruct With(IMixin mixins)func With(Mixins IMixin) IConstruct #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