Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions posts/1733138598695.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ The DigitalOcean provider lets Terraform interact with the DigitalOcean API to b

- **digitalocean_droplet**: Droplets (servers)
- **digitalocean_loadbalancer**: Load Balancers
- **digitalocean_domain**: DNS domain entries
- **digitalocean_record**: DNS records

`www-1.tf` file

Expand Down Expand Up @@ -276,6 +278,69 @@ ip_addresses = {
load_balancer_ip = "x.x.x.x"
```

## Creating DNS Domains and Records with Terraform

Terraform can manage DNS domains and records in addition to resources like Droplets and Load Balancers. For instance, to point a domain to a Load Balancer, you can define a configuration to establish this relationship.

### Expl: Pointing `multividas.com` to a Load Balancer

Create a file named `domain_root.tf` with the following configuration:

```yaml
# domain_root.tf
resource "digitalocean_domain" "default" {
name = "multividas.com"
ip_address = digitalocean_loadbalancer.www-lb.ip
}
```

### Adding a CNAME Record for www.multividas.com

To create a `CNAME` record that points `www.multividas.com` to `multividas.com`, define the following in a new file called `domain_cname.tf`:

```
# domain_cname.tf

resource "digitalocean_record" "CNAME-www" {
domain = digitalocean_domain.default.name
type = "CNAME"
name = "www"
value = "@"
}
```

### Applying the Configuration

After adding these DNS entries, run the following commands to plan and apply the changes:

```sh
terraform plan
terraform apply
```

## Destroying Your Infrastructure with Terraform

In development environments where infrastructure is frequently created and destroyed, Terraform provides a way to tear down resources it manages. While this is less common in production, it can be useful for testing and development purposes.

### Step 1: Create a Destruction Plan

```sh
terraform plan -destroy -out=terraform.tfplan \
-var "pvt_key=$(cat $HOME/.ssh/terraform_ssh)"
```

Terraform will display a plan with resources marked in red and prefixed with a minus sign (-), indicating they will be removed.

### Step 2: Apply the Destruction Plan

Execute the destruction plan with the following command:

```sh
terraform apply terraform.tfplan
```

Terraform will then destroy the resources as outlined in the generated plan.

### Additional Resources

- [ansible-digitalocean](https://github.com/soulaimaneyahya/ansible-digitalocean)
Expand Down