AWS CDK에서 DependsOn 동작을 복제 할 수 있습니까?

Aug 21 2020

AWS CDK (python)를 사용하여 격리 된 서브넷과 여러 인터페이스 엔드 포인트가있는 VPC를 생성하고 있습니다.

또한 연결된 Codecommit 리포지토리가 있는 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 노트북 run을 주석 처리 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]
        )

답변

2 MauricioKlein Oct 14 2020 at 16:14

CDK에 Typescript를 사용하기 때문에 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 구조에서 내부 노드에 액세스하는 방법을 파악한 후에는 매우 간단합니다.