2323import static software .amazon .awscdk .services .s3objectlambda .CfnAccessPoint .*;
2424
2525public class S3ObjectLambdaStack extends Stack {
26-
2726 private static final String S3_ACCESS_POINT_NAME = "s3-access-point" ;
2827 private static final String OBJECT_LAMBDA_ACCESS_POINT_NAME = "object-lambda-access-point" ;
2928
29+ /**
30+ * Constructs a new S3ObjectLambdaStack.
31+ */
3032 public S3ObjectLambdaStack (final Construct scope , final String id , final StackProps props ) {
31- super (scope , id , props );
33+ super (scope , id , props );
34+
35+ // Construct the access point ARN using the region, account ID and access point name
3236 var accessPoint = "arn:aws:s3:" + Aws .REGION + ":" + Aws .ACCOUNT_ID + ":accesspoint/" + S3_ACCESS_POINT_NAME ;
37+
38+ // Create a new S3 bucket with secure configuration including:
3339 var s3ObjectLambdaBucket = Bucket .Builder .create (this , "S3ObjectLambdaBucket" )
3440 .removalPolicy (RemovalPolicy .RETAIN )
3541 .autoDeleteObjects (false )
3642 .accessControl (BucketAccessControl .BUCKET_OWNER_FULL_CONTROL )
3743 .encryption (BucketEncryption .S3_MANAGED )
3844 .blockPublicAccess (BlockPublicAccess .BLOCK_ALL )
3945 .build ();
46+
47+ // Create bucket policy statement allowing access through access points
4048 var s3ObjectLambdaBucketPolicyStatement = PolicyStatement .Builder .create ()
4149 .actions (List .of ("*" ))
4250 .principals (List .of (new AnyPrincipal ()))
@@ -52,20 +60,30 @@ public S3ObjectLambdaStack(final Construct scope, final String id, final StackPr
5260 )
5361 )
5462 .build ();
63+
64+ // Attach the policy to the bucket
5565 s3ObjectLambdaBucket .addToResourcePolicy (s3ObjectLambdaBucketPolicyStatement );
66+
67+ // Create the Lambda function that will transform objects
5668 var s3ObjectLambdaFunction = createS3ObjectLambdaFunction ();
69+
70+ // Add permission for Lambda to write GetObject responses for s3 object
5771 var s3ObjectLambdaFunctionPolicyStatement = PolicyStatement .Builder .create ()
5872 .effect (Effect .ALLOW )
5973 .resources (List .of ("*" ))
6074 .actions (List .of ("s3-object-lambda:WriteGetObjectResponse" ))
6175 .build ();
6276 s3ObjectLambdaFunction .addToRolePolicy (s3ObjectLambdaFunctionPolicyStatement );
77+
78+ // Add permission for the account root to invoke the Lambda function
6379 var s3ObjectLambdaFunctionPermission = Permission .builder ()
6480 .action ("lambda:InvokeFunction" )
6581 .principal (new AccountRootPrincipal ())
6682 .sourceAccount (Aws .ACCOUNT_ID )
6783 .build ();
6884 s3ObjectLambdaFunction .addPermission ("S3ObjectLambdaPermission" , s3ObjectLambdaFunctionPermission );
85+
86+ // Create policy allowing Lambda function to get objects through the access point
6987 var s3ObjectLambdaAccessPointPolicyStatement = PolicyStatement .Builder .create ()
7088 .sid ("S3ObjectLambdaAccessPointPolicyStatement" )
7189 .effect (Effect .ALLOW )
@@ -76,16 +94,22 @@ public S3ObjectLambdaStack(final Construct scope, final String id, final StackPr
7694 )
7795 .resources (List .of (accessPoint + "/object/*" ))
7896 .build ();
97+
98+ // Create policy document containing the access point policy
7999 var s3ObjectLambdaAccessPointPolicyDocument = PolicyDocument .Builder .create ()
80100 .statements (List .of (
81101 s3ObjectLambdaAccessPointPolicyStatement
82102 ))
83103 .build ();
104+
105+ // Create the S3 access point for direct bucket access
84106 software .amazon .awscdk .services .s3 .CfnAccessPoint .Builder .create (this , "S3ObjectLambdaS3AccessPoint" )
85107 .bucket (s3ObjectLambdaBucket .getBucketName ())
86108 .name (S3_ACCESS_POINT_NAME )
87109 .policy (s3ObjectLambdaAccessPointPolicyDocument )
88110 .build ();
111+
112+ // Create the Object Lambda access point that will transform objects
89113 var s3ObjectLambdaAccessPoint = CfnAccessPoint .Builder .create (this , "S3ObjectLambdaAccessPoint" )
90114 .name (OBJECT_LAMBDA_ACCESS_POINT_NAME )
91115 .objectLambdaConfiguration (ObjectLambdaConfigurationProperty .builder ()
@@ -107,28 +131,39 @@ public S3ObjectLambdaStack(final Construct scope, final String id, final StackPr
107131 )
108132 .build ();
109133 CfnOutput .Builder .create (this , "s3ObjectLambdaBucketArn" )
110- .value (s3ObjectLambdaBucket .getBucketArn ())
134+ .value (s3ObjectLambdaBucket .getBucketArn ()) // Export bucket ARN
111135 .build ();
112136 CfnOutput .Builder .create (this , "s3ObjectLambdaFunctionArn" )
113- .value (s3ObjectLambdaFunction .getFunctionArn ())
137+ .value (s3ObjectLambdaFunction .getFunctionArn ()) // Export Lambda function ARN
114138 .build ();
115139 CfnOutput .Builder .create (this , "s3ObjectLambdaAccessPointArn" )
116- .value (s3ObjectLambdaAccessPoint .getAttrArn ())
140+ .value (s3ObjectLambdaAccessPoint .getAttrArn ()) // Export access point ARN
117141 .build ();
142+
143+ // Create output with Console URL for easy access to the Lambda access point
118144 CfnOutput .Builder .create (this , "s3ObjectLambdaAccessPointUrl" )
119145 .value ("https://console.aws.amazon.com/s3/olap/" + Aws .ACCOUNT_ID + "/" + OBJECT_LAMBDA_ACCESS_POINT_NAME + "?region=" + Aws .REGION )
120146 .build ();
121147 }
122148
149+ /**
150+ * Creates the Lambda function that will process S3 Object Lambda requests.
151+ * This method configures the function's runtime, code, and build process.
152+ *
153+ * @return A Lambda Function construct configured for S3 Object Lambda processing
154+ */
123155 private Function createS3ObjectLambdaFunction () {
156+ // Define Maven packaging commands to build the Lambda function
124157 List <String > packagingInstructions = List .of (
125158 "/bin/sh" ,
126159 "-c" ,
160+ // Build the project and copy the JAR to the asset output directory
127161 "mvn -e -q clean package && cp /asset-input/target/lambda-1.0-SNAPSHOT.jar /asset-output/"
128162 );
163+ // Configure the bundling options for packaging the Lambda function
129164 var builderOptions = BundlingOptions .builder ()
130- .command (packagingInstructions )
131- .image (Runtime .JAVA_17 .getBundlingImage ())
165+ .command (packagingInstructions ) // Set the Maven build commands
166+ .image (Runtime .JAVA_17 .getBundlingImage ()) // Use Java 17 runtime image
132167 .volumes (
133168 singletonList (
134169 DockerVolume .builder ()
@@ -139,10 +174,12 @@ private Function createS3ObjectLambdaFunction() {
139174 .user ("root" )
140175 .outputType (ARCHIVED )
141176 .build ();
177+
178+ // Create the Lambda function with specified configuration
142179 return Function .Builder .create (this , "S3ObjectLambdaFunction" )
143- .runtime (Runtime .JAVA_17 )
144- .functionName ("S3ObjectLambdaFunction" )
145- .memorySize (2048 )
180+ .runtime (Runtime .JAVA_17 ) // Set Java 17 runtime
181+ .functionName ("S3ObjectLambdaFunction" ) // Set function name
182+ .memorySize (2048 ) // Allocate 2GB memory
146183 .code (
147184 Code .fromAsset (
148185 "../lambda/" ,
@@ -152,4 +189,4 @@ private Function createS3ObjectLambdaFunction() {
152189 .handler ("com.myorg.S3ObjectLambdaTransformer::handleRequest" )
153190 .build ();
154191 }
155- }
192+ }
0 commit comments