diff --git a/src/content/docs/aws/services/route53.mdx b/src/content/docs/aws/services/route53.mdx index 88851b6a..4cdff466 100644 --- a/src/content/docs/aws/services/route53.mdx +++ b/src/content/docs/aws/services/route53.mdx @@ -12,10 +12,11 @@ import FeatureCoverage from "../../../../components/feature-coverage/FeatureCove Route 53 is a highly scalable and reliable domain name system (DNS) web service provided by Amazon Web Services. Route 53 allows you to register domain names, and associate them with IP addresses or other resources. In addition to basic DNS functionality, Route 53 offers advanced features like health checks and DNS failover. -Route 53 integrates seamlessly with other AWS services, such as route traffic to CloudFront distributions, S3 buckets configured for static website hosting, EC2 instances, and more. +Route 53 integrates seamlessly with other AWS services, such as route traffic to CloudFront distributions, S3 buckets configured for static website hosting, Elastic Load Balancers, EC2 instances, and more. LocalStack allows you to use the Route53 APIs in your local environment to create hosted zones and to manage DNS entries. The supported APIs are available on our [API Coverage section](#api-coverage), which provides information on the extent of Route53's integration with LocalStack. +LocalStack supports routing traffic to various AWS resources including [S3 static websites](#routing-to-s3-static-websites) and [Elastic Load Balancers](#routing-to-elastic-load-balancers) using alias records. LocalStack also integrates with its DNS server to respond to DNS queries with these domains. :::note @@ -68,6 +69,139 @@ awslocal route53 change-resource-record-sets \ } ``` +## Routing traffic to AWS resources + +LocalStack Route53 supports routing traffic to various AWS resources using alias records. This allows you to point your domain names directly to AWS services without managing IP addresses. + +### Routing to S3 static websites + +You can route traffic from a Route53 domain to an S3 bucket configured for static website hosting using alias records. This is useful when you want to serve a static website with a custom domain name. + +#### Create an S3 bucket with website hosting + +First, create an S3 bucket and configure it for static website hosting. Run the following commands: + +```bash +DOMAIN="example.com" +BUCKET_NAME="$DOMAIN" + +# Create the bucket +awslocal s3api create-bucket --bucket "$BUCKET_NAME" + +# Upload your website files +awslocal s3 cp index.html s3://$BUCKET_NAME/ +awslocal s3 cp error.html s3://$BUCKET_NAME/ + +# Configure the bucket for website hosting +awslocal s3 website s3://"$BUCKET_NAME"/ \ + --index-document index.html \ + --error-document error.html + +# Set bucket policy to allow public read access +awslocal s3api put-bucket-policy \ + --bucket $BUCKET_NAME \ + --policy '{ + "Version": "2012-10-17", + "Statement": [{ + "Sid": "PublicReadGetObject", + "Effect": "Allow", + "Principal": "*", + "Action": "s3:GetObject", + "Resource": "arn:aws:s3:::'$BUCKET_NAME'/*" + }] + }' +``` + +#### Create a Route53 alias record + +Now create a hosted zone and an alias record that points to the S3 website endpoint: + +```bash +# Create the hosted zone +HOSTED_ZONE_ID=$(awslocal route53 create-hosted-zone \ + --name "$DOMAIN" \ + --caller-reference "$(date +%s)" \ + --output text \ + --query 'HostedZone.Id' | cut -d'/' -f3) + +echo "Hosted Zone created with ID: $HOSTED_ZONE_ID" + +# Create an alias record pointing to the S3 website endpoint +awslocal route53 change-resource-record-sets \ + --hosted-zone-id "$HOSTED_ZONE_ID" \ + --change-batch '{ + "Comment": "Create alias record for S3 static website", + "Changes": [{ + "Action": "CREATE", + "ResourceRecordSet": { + "Name": "'$DOMAIN'", + "Type": "A", + "AliasTarget": { + "HostedZoneId": "'$HOSTED_ZONE_ID'", + "DNSName": "'$BUCKET_NAME'.s3-website.localhost.localstack.cloud", + "EvaluateTargetHealth": false + } + } + }] + }' +``` + +The key points for S3 website alias records are: + +- The `DNSName` follows the format: `.s3-website.localhost.localstack.cloud` +- The `Type` must be `A` (for IPv4) or `AAAA` (for IPv6) +- Set `EvaluateTargetHealth` to `false` for S3 website endpoints + +#### Verify DNS resolution + +You can verify that your domain resolves to the S3 website using [DNS resolution](#dns-resolution) or by making HTTP requests: + +```bash +# Using dig to verify DNS resolution +dig @localhost $DOMAIN + +# Using curl to access the website +curl http://$DOMAIN:4566/ +``` + +### Routing to Elastic Load Balancers + +You can also route traffic to Elastic Load Balancers (ELB) using alias records. This is commonly used for distributing traffic across multiple instances. + +#### Create an alias record for ELB + +After creating your load balancer, you can create an alias record that points to it: + +```bash +# Assuming you have an ELB with DNS name +ELB_DNS_NAME="my-load-balancer-123456.elb.localhost.localstack.cloud" + +# Create an alias record pointing to the ELB +awslocal route53 change-resource-record-sets \ + --hosted-zone-id "$HOSTED_ZONE_ID" \ + --change-batch '{ + "Comment": "Create alias record for ELB", + "Changes": [{ + "Action": "CREATE", + "ResourceRecordSet": { + "Name": "app.example.com", + "Type": "A", + "AliasTarget": { + "HostedZoneId": "'$HOSTED_ZONE_ID'", + "DNSName": "'$ELB_DNS_NAME'", + "EvaluateTargetHealth": true + } + } + }] + }' +``` + +For ELB alias records: + +- Use the load balancer's DNS name as the `DNSName` value +- You can set `EvaluateTargetHealth` to `true` to enable health checks +- The `Type` should be `A` for IPv4 addresses + ## DNS resolution LocalStack Pro supports the ability to respond to DNS queries for your Route53 domain names, with our [integrated DNS server](/aws/tooling/dns-server).