我可以在AWS CDK中复制DependsOn行为吗?

问题描述

使用AWS CDK(python),我正在创建具有隔离子网和多个Interface端点的VPC。

我还将推出带有associated Codecommit repo

的Sagemaker笔记本

我为Codecommit和Git Codecommit创建了接口端点,但是当我的Sagemaker笔记本开始部署时,接口端点仍在创建,因此Cloudformation堆栈因错误而失败

fatal: unable to access 'https://git-codecommit.us-east-1.amazonaws.com/v1/repos/SomeRepo/': Failed to connect to git-codecommit.us-east-1.amazonaws.com port 443: Connection timed out

如果我注释掉我的Sagemaker笔记本,运行cdk deploy myStack,则创建端点,然后我可以取消注释我的笔记本,由于Codecommit接口端点已经存在,因此它可以正常启动。

CDK中是否可以将DependsOn添加到使用CDK创建的资源中?

下面的相关代码

from aws_cdk import (
    core,aws_sagemaker as sagemaker,aws_iam as iam,aws_ec2 as ec2
)

class SagemakerStack(core.Stack):

    def __init__(self,scope: core.Construct,id: str,bucket,repo,**kwargs) -> None:  # repo is passed with the codecommit repo I created in a prevIoUs stack
        super().__init__(scope,id,**kwargs)

        self.sagemaker_vpc = ec2.Vpc(
            # Leaving out some details
            self,"SagemakerVPC",subnet_configuration=[
                ec2.subnetConfiguration(
                    subnet_type=ec2.subnetType.ISOLATED,# <-- Isolated,therefore need interface endpoints
                    name="IsolatedA",cidr_mask=24
                )
            ]
        )

        self.sagemaker_vpc.add_interface_endpoint(
            "GitCodecommitInterface",service=ec2.InterfaceVpcEndpointAwsService.CODECOMMIT_GIT  # Needed to be created before I can create notebook
        )


        sagemaker_role = iam.Role()  # Hiding details
        sagemaker_repo = sagemaker.CfnCodeRepository()  # hiding details
        sagemaker_sg = ec2.SecurityGroup()  # Hiding details

        # Need for this to wait until GitCodecommitInterface has been created
        notebook = sagemaker.CfnNotebookInstance(
            self,"MyNotebook",instance_type="ml.t3.medium",role_arn=sagemaker_role.role_arn,default_code_repository=repo.repository_clone_url_http,subnet_id=self.sagemaker_vpc.isolated_subnets[0].subnet_id,direct_internet_access="disabled",security_group_ids=[sagemaker_sg.security_group_id]
        )

解决方法

由于我将Typescript用于CDK,所以不确定Python的语法如何,但是CDK构造最终会在CloudFormation资源中解析,因此实际上是可能的。

下面是使用Typescript的示例:

const x = new apigateway.LambdaRestApi(this,"apigw",{...})
const y = new lambda.Function(this,"lambda",{...})

// Y will be deployed before X
x.node.addDependency(y)

因此,在您了解了如何从CDK构造访问内部节点之后,这很简单