AWS CDK Route53 Targets (CDK v1)

1.204.0 · active · verified Fri Apr 17

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

Warnings

Install

Imports

Quickstart

This example demonstrates how to create an AWS CDK application (for CDK v1) that provisions an S3 static website and then creates an AWS Route53 A-alias record pointing to that S3 bucket. It utilizes the `S3BucketWebsiteTarget` from the `aws-cdk-aws-route53-targets` library to simplify the alias configuration. Remember to replace `myexampledomain.com` with your own registered domain name for actual deployment.

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()

view raw JSON →