如何在CDK中获取AppRunner服务的默认域名
获取AWS App Runner默认域名的正确方法
问题原因
你混淆了AWS CDK中L1(CfnService)和L2(AppRunnerService)构造的属性命名规则,错误使用了不存在的attrs_service_url属性。
解决方案
根据你使用的构造类型,选择对应的方法获取默认域名:
1. 使用L2构造(推荐,封装更便捷)
如果用的是aws_apprunner.AppRunnerService(L2高层构造),直接通过serviceUrl属性获取:
from aws_cdk import aws_apprunner as apprunner # 创建App Runner Service service = apprunner.AppRunnerService(self, "MyAppRunnerService", source=apprunner.Source.from_ecr_public( image_configuration=apprunner.ImageConfiguration(port=80), image_identifier="public.ecr.aws/aws-containers/hello-app-runner:latest" ) ) # 获取默认域名 default_domain = service.serviceUrl
2. 使用L1构造(CfnService)
如果直接用aws_apprunner.CfnService(L1底层构造),需要使用attr_default_domain属性,对应CloudFormation的DefaultDomain输出:
from aws_cdk import aws_apprunner as apprunner # 创建L1的App Runner Service cfn_service = apprunner.CfnService(self, "MyCfnAppRunnerService", source_configuration=apprunner.CfnService.SourceConfigurationProperty( image_repository=apprunner.CfnService.ImageRepositoryProperty( image_identifier="public.ecr.aws/aws-containers/hello-app-runner:latest", image_repository_type="ECR_PUBLIC", image_configuration=apprunner.CfnService.ImageConfigurationProperty(port=80) ) ), instance_configuration=apprunner.CfnService.InstanceConfigurationProperty( cpu="1 vCPU", memory="2 GB" ) ) # 获取默认域名 default_domain = cfn_service.attr_default_domain
额外提示
如果仍有问题,检查你的AWS CDK版本,旧版本可能存在属性命名差异,建议升级到最新的稳定版。
内容的提问来源于stack exchange,提问作者Yougeshwar Khatri
相关产品推荐
相关产品推荐

