How do I route traffic to an EC2 instance with Route 53?
In this guide, we will create an EC2 instance and set up a Route 53 DNS record to route traffic to the EC2 instance. We will use Pulumi to define and deploy our infrastructure.
Key Points
- Create an EC2 instance.
- Create a Route 53 hosted zone.
- Create an A record in Route 53 to point to the EC2 instance.
import * as aws from "@pulumi/aws";
import * as pulumi from "@pulumi/pulumi";
// Create an EC2 instance
const ec2Instance = new aws.ec2.Instance("web-server", {
ami: "ami-0c55b159cbfafe1f0", // Amazon Linux 2 AMI
instanceType: "t2.micro",
tags: {
Name: "web-server",
},
});
// Create a Route 53 hosted zone
const hostedZone = new aws.route53.Zone("exampleZone", {
name: "example.com",
});
// Create a Route 53 A record to point to the EC2 instance
const aRecord = new aws.route53.Record("exampleRecord", {
zoneId: hostedZone.zoneId,
name: "www.example.com",
type: "A",
ttl: 300,
records: [ec2Instance.publicIp],
});
// Export the name of the DNS record
export const dnsName = aRecord.fqdn;
Summary
In this guide, we created an EC2 instance and set up a Route 53 hosted zone. We then created an A record in Route 53 to point to the EC2 instance, enabling traffic routing to the instance.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.