我可以在 AWS CDK 中复制 DependsOn 行为吗?
Can I replicate the DependsOn behavior in AWS CDK?
使用 AWS CDK (python),我正在创建一个具有隔离子网和多个接口端点的 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]
)
不确定 Python 的语法如何,因为我将 Typescript 用于 CDK,但 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 构造访问内部节点之后,它就非常简单了
使用 AWS CDK (python),我正在创建一个具有隔离子网和多个接口端点的 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]
)
不确定 Python 的语法如何,因为我将 Typescript 用于 CDK,但 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 构造访问内部节点之后,它就非常简单了