aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.71k stars 3.93k forks source link

aws_s3_deployment.BucketDeployment: default_child not useable as dependency #32253

Open gratinierer opened 5 days ago

gratinierer commented 5 days ago

Describe the bug

Using a aws_s3_deployment.BucketDeployment and trying do add this deployment as dependency does not work.

Regression Issue

Last Known Working CDK Version

No response

Expected Behavior

i would expect, that I can add a BucketDeployment as dependency to another L1-Constructs, as can do e.g. with aws_s3.Bucket

Current Behavior

TypeError: type of argument target must be aws_cdk.CfnResource; got NoneType instead

is thrown

Reproduction Steps

 deployment = aws_s3_deployment.BucketDeployment(self,
                                                                        'DeploymentBucketDeployment',
                                                                        sources=[aws_s3_deployment.Source.asset(
                                                                            zip_file)],
                                                                        extract=True,
                                                                        destination_bucket=deployment_bucket
                                                                        )
  cfn_application_version = aws_cdk.aws_elasticbeanstalk.CfnApplicationVersion(
            self, "MyCfnApplicationVersion",
            application_name="application",
            source_bundle=aws_cdk.aws_elasticbeanstalk.CfnApplicationVersion.SourceBundleProperty(
                s3_bucket=deplomyent_bucket_name,
                s3_key=deployment_artifact
            ),
        )
        cfn_application_version.add_dependency(deployment.node.default_child)

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.148.0 (build e5740c0)

Framework Version

No response

Node.js Version

20.15

OS

Win10

Language

Python

Language Version

No response

Other information

No response

ashishdhingra commented 4 days ago

@gratinierer Good morning. Thanks for reporting the issue. Using the below CDK code (in TypeScript):

import * as cdk from 'aws-cdk-lib';
import * as s3 from 'aws-cdk-lib/aws-s3';
import path = require('path');
import * as s3Deployment from 'aws-cdk-lib/aws-s3-deployment';

export class CdktestStack extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const s3TestDeployment = new s3Deployment.BucketDeployment(this, 'TestBucketDeployment', {
      sources:[s3Deployment.Source.asset(path.join(__dirname, 'source-files'))],
      destinationBucket: s3.Bucket.fromBucketName(this, 'MyBucket', 'testbucket-issue32253')
    });

    console.log("s3TestDeployment.node.defaultChild instanceof cdk.Resource: " + (s3TestDeployment.node.defaultChild instanceof cdk.Resource));
    console.log("s3TestDeployment.node.defaultChild === undefined: " + (s3TestDeployment.node.defaultChild === undefined));

    const newBucket = new s3.Bucket(this, 'NewBucket');
    console.log("newBucket.node.defaultChild instanceof cdk.Resource: "+  (newBucket.node.defaultChild instanceof cdk.Resource));
    console.log("newBucket.node.defaultChild === undefined: "+  (newBucket.node.defaultChild === undefined));
  }
}

And running cdk synth produces the below output:

s3TestDeployment.node.defaultChild instanceof cdk.Resource: false
s3TestDeployment.node.defaultChild === undefined: true
newBucket.node.defaultChild instanceof cdk.Resource: false
newBucket.node.defaultChild === undefined: false
...
<<CFN Template YAML>>

If you check the code documentation for node.defaultChild, it @returns — a construct or undefined if there is no default child.

Also in above output, the defaultChild for a BucketDeployment is not defined, hence it cannot be used as a dependency (in Python CDK, undefined is represented as NoneType).

The most likely reason for defaultChild not defined is that BucketDeployment doesn't represents a resource where per code, BucketDeployment extends Construct (compare it to Bucket class, which extends BucketBase which further extends Resource). And this makes sense since BucketDeployment construct represents a deployment operation. And per CloudFormation DependsOn attribute documentation, it specifies that the creation of a specific resource follows another resource.

Thanks, Ashish

github-actions[bot] commented 2 days ago

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.