AWS CDK Route53 Targets (CDK v1)
The `aws-cdk-aws-route53-targets` library provides AWS CDK constructs for creating Route53 Alias records that point to various AWS resources like S3 static websites, CloudFront distributions, API Gateway custom domains, and Elastic Load Balancers. It extends `aws-cdk.aws-route53` by offering convenient alias targets. This specific package (version 1.x.x) is designed for AWS CDK v1 applications. AWS CDK follows a monthly release cadence, aligning with new AWS service features and updates.
Common errors
-
ModuleNotFoundError: No module named 'aws_cdk.aws_route53_targets'
cause The `aws-cdk-aws-route53-targets` package is not installed in your Python environment, or you are running CDK v2 and mistakenly expecting this package to be present.fixFor CDK v1: `pip install aws-cdk-aws-route53-targets`. For CDK v2: Do not install this package; targets are directly in `aws_cdk.aws_route53` (e.g., `from aws_cdk.aws_route53 import S3BucketWebsiteTarget`). -
AttributeError: module 'aws_cdk.aws_route53' has no attribute 'S3BucketWebsiteTarget'
cause You are trying to import a target construct directly from `aws_cdk.aws_route53` while using AWS CDK v1, or you are using CDK v2 but have not installed `aws-cdk-lib` correctly.fixFor CDK v1: Correct the import to `from aws_cdk.aws_route53_targets import S3BucketWebsiteTarget`. For CDK v2: Ensure `aws-cdk-lib` is installed and the import is `from aws_cdk.aws_route53 import S3BucketWebsiteTarget`. -
JSII error: Type 'aws_cdk.aws_route53_targets.S3BucketWebsiteTarget' is not assignable to type 'aws_cdk.aws_route53.IRoute53RecordTarget'
cause This usually indicates a version mismatch between your `aws-cdk.core`, `aws-cdk.aws-route53`, and `aws-cdk-aws-route53-targets` packages, or attempting to mix CDK v1 constructs with a CDK v2 application context.fixEnsure all `aws-cdk.*` packages are on the *exact same* major.minor.patch version (e.g., all `1.204.0`). If using CDK v2, uninstall all `aws-cdk.*` and install only `aws-cdk-lib`.
Warnings
- breaking This `aws-cdk-aws-route53-targets` package is for AWS CDK v1 ONLY. For AWS CDK v2, all alias targets are integrated directly into the `aws-cdk-lib.aws_route53` module. Do NOT install this package if you are using CDK v2 (`aws-cdk-lib`).
- gotcha The target resource (e.g., S3 bucket) must be configured correctly for the alias target to work. For `S3BucketWebsiteTarget`, the S3 bucket must have static website hosting enabled and be publicly accessible. For `CloudFrontTarget`, the distribution must be deployed.
- gotcha When creating records, always use `route53.RecordTarget.from_alias()` with a target from this library. Directly assigning a raw construct will likely result in an error or an incorrect record type.
Install
-
pip install aws-cdk-aws-route53-targets
Imports
- S3BucketWebsiteTarget
from aws_cdk.aws_route53 import S3BucketWebsiteTarget
from aws_cdk.aws_route53_targets import S3BucketWebsiteTarget
- CloudFrontTarget
from aws_cdk.lib.aws_route53 import CloudFrontTarget
from aws_cdk.aws_route53_targets import CloudFrontTarget
- ApiGatewayDomain
from aws_cdk.aws_route53_targets import ApiGatewayDomain
Quickstart
import os
from aws_cdk import App, Stack, Duration
from aws_cdk import aws_s3 as s3
from aws_cdk import aws_route53 as route53
from aws_cdk.aws_route53_targets import S3BucketWebsiteTarget # This is the key import for this library
class MyRoute53Stack(Stack):
def __init__(self, scope, id, **kwargs):
super().__init__(scope, id, **kwargs)
# In a real scenario, you'd import an existing Hosted Zone:
# hosted_zone = route53.HostedZone.from_lookup(self, "MyZone", domain_name="example.com")
# For a runnable example, we'll create a dummy one (will create a Route 53 zone).
# Replace 'myexampledomain.com' with your actual domain name.
hosted_zone = route53.HostedZone(self, "MyZone",
zone_name="myexampledomain.com"
)
# Create an S3 bucket configured for static website hosting
website_bucket = s3.Bucket(self, "WebsiteBucket",
bucket_name="www.myexampledomain.com", # Must match the domain for S3 website hosting
website_index_document="index.html",
public_read_access=True # Required for public website hosting
)
# Create an A record pointing to the S3 bucket website using S3BucketWebsiteTarget
route53.ARecord(self, "WebsiteAliasRecord",
zone=hosted_zone,
target=route53.RecordTarget.from_alias(S3BucketWebsiteTarget(website_bucket)),
record_name="www" # This will create 'www.myexampledomain.com'
)
app = App()
MyRoute53Stack(app, "MyRoute53Stack")
app.synth()