Skip to content

Commit bdcbd19

Browse files
author
Leon Michalski
committed
Add documentation for passing references to L1s
1 parent 207e58f commit bdcbd19

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed

v2/guide/constructs.adoc

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,168 @@ Some of our language-specific API references currently have errors in the paths
660660
661661
====
662662
663+
[#constructs-l1-l2-references]
664+
=== Passing construct references to L1 properties
665+
666+
L1 constructs support passing construct objects directly to properties that expect resource references, such as ARNs or names. The CDK automatically resolves the appropriate attribute (ARN, name, or other identifier) based on the property type.
667+
668+
The following example creates a Lambda function using an L1 construct (`CfnFunction`) with a role defined using an L2 construct (`Role`). The role object is passed directly to the `role` property:
669+
670+
====
671+
[role="tablist"]
672+
TypeScript::
673+
+
674+
[source,javascript,subs="verbatim,attributes"]
675+
----
676+
const l2Role = new iam.Role(this, 'L2Role', {
677+
assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'),
678+
managedPolicies: [
679+
iam.ManagedPolicy.fromAwsManagedPolicyName(
680+
'service-role/AWSLambdaBasicExecutionRole'
681+
),
682+
],
683+
});
684+
685+
new lambda.CfnFunction(this, "HelloWorldFunction", {
686+
runtime: 'nodejs24.x',
687+
role: l2Role, // Pass L2 construct directly
688+
handler: 'index.handler',
689+
code: {
690+
zipFile: `
691+
exports.handler = async function(event) {
692+
return {
693+
statusCode: 200,
694+
body: JSON.stringify('Hello World!'),
695+
};
696+
};
697+
`}
698+
});
699+
----
700+
701+
JavaScript::
702+
+
703+
[source,javascript,subs="verbatim,attributes"]
704+
----
705+
const l2Role = new iam.Role(this, 'L2Role', {
706+
assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'),
707+
managedPolicies: [
708+
iam.ManagedPolicy.fromAwsManagedPolicyName(
709+
'service-role/AWSLambdaBasicExecutionRole'
710+
)
711+
]
712+
});
713+
714+
new lambda.CfnFunction(this, "HelloWorldFunction", {
715+
runtime: 'nodejs24.x',
716+
role: l2Role, // Pass L2 construct directly
717+
handler: 'index.handler',
718+
code: {
719+
zipFile: `
720+
exports.handler = async function(event) {
721+
return {
722+
statusCode: 200,
723+
body: JSON.stringify('Hello World!'),
724+
};
725+
};
726+
`}
727+
});
728+
----
729+
730+
Python::
731+
+
732+
[source,python,subs="verbatim,attributes"]
733+
----
734+
l2_role = iam.Role(self, "L2Role",
735+
assumed_by=iam.ServicePrincipal("lambda.amazonaws.com"),
736+
managed_policies=[
737+
iam.ManagedPolicy.from_aws_managed_policy_name(
738+
"service-role/AWSLambdaBasicExecutionRole"
739+
)
740+
]
741+
)
742+
743+
lambda_.CfnFunction(self, "HelloWorldFunction",
744+
runtime="nodejs24.x",
745+
role=l2_role, # Pass L2 construct directly
746+
handler="index.handler",
747+
code=lambda_.CfnFunction.CodeProperty(
748+
zip_file=
749+
"""
750+
exports.handler = async function(event) {
751+
return {
752+
statusCode: 200,
753+
body: JSON.stringify('Hello World!'),
754+
};
755+
};
756+
"""
757+
)
758+
)
759+
----
760+
761+
Java::
762+
+
763+
[source,java,subs="verbatim,attributes"]
764+
----
765+
Role l2Role = Role.Builder.create(this, "L2Role")
766+
.assumedBy(new ServicePrincipal("lambda.amazonaws.com"))
767+
.managedPolicies(Arrays.asList(
768+
ManagedPolicy.fromAwsManagedPolicyName(
769+
"service-role/AWSLambdaBasicExecutionRole"
770+
)
771+
))
772+
.build();
773+
774+
CfnFunction.Builder.create(this, "HelloWorldFunction")
775+
.runtime("nodejs24.x")
776+
.role(l2Role) // Pass L2 construct directly
777+
.handler("index.handler")
778+
.code(CfnFunction.CodeProperty.builder()
779+
.zipFile(
780+
"exports.handler = async function(event) {" +
781+
" return {" +
782+
" statusCode: 200," +
783+
" body: JSON.stringify('Hello World!')," +
784+
" };" +
785+
"};")
786+
.build())
787+
.build();
788+
----
789+
790+
C#::
791+
+
792+
[source,csharp,subs="verbatim,attributes"]
793+
----
794+
var l2Role = new Role(this, "L2Role", new RoleProps
795+
{
796+
AssumedBy = new ServicePrincipal("lambda.amazonaws.com"),
797+
ManagedPolicies = new[]
798+
{
799+
ManagedPolicy.FromAwsManagedPolicyName(
800+
"service-role/AWSLambdaBasicExecutionRole"
801+
)
802+
}
803+
});
804+
805+
new CfnFunction(this, "HelloWorldFunction", new CfnFunctionProps
806+
{
807+
Runtime = "nodejs24.x",
808+
Role = l2Role, // Pass L2 construct directly
809+
Handler = "index.handler",
810+
Code = new CfnFunction.CodeProperty
811+
{
812+
ZipFile = @"
813+
exports.handler = async function(event) {
814+
return {
815+
statusCode: 200,
816+
body: JSON.stringify('Hello World!'),
817+
};
818+
};
819+
"
820+
}
821+
});
822+
----
823+
====
824+
663825
[#constructs-using]
664826
=== Working with L2 constructs
665827

0 commit comments

Comments
 (0)