cli53

repository·main·Indexed 24 days ago

https://github.com/barnybug/cli53

A command line tool for Amazon Route 53 that provides import/export functionality for BIND format files and simplified management of hosted zones and individual records. It supports AWS-specific extensions including Alias, Weighted, Geolocation, Failover, and Multivalue records, as well as the management of reusable delegation sets.

Tokens
6.6K
Snippets
15
Records
41
Agent score
80%

What's inside cli53

  1. Import and export BIND zone files

    main

    cli53 supports importing and exporting records using the BIND format.

    Importing

    • Standard Import: cli53 import --file <file> <domain>
    • Replace existing records: cli53 import --file <file> --replace --wait <domain>
    • Upsert (Add new, keep existing): cli53 import --file <file> --upsert <domain>
    • Dry-run: Add --dry-run to see what would happen without making changes.
    • Validate syntax: cli53 validate --file <file>

    Exporting

    • Standard Export: cli53 export <domain>
    • Full FQDN Export: Use --full to export fully-qualified domain names instead of prefixes.

    Troubleshooting CNAMEs: If importing from providers like GoDaddy, ensure CNAME/MX records have a trailing dot. If they don't, use this regex to fix them:

    perl -pe 's/((CNAME|MX\s+\d+)\s+[-a-zA-Z0-9._]+)(?!.)$/$1./i' broken.txt > fixed.txt
    cli53 import --file zonefile.txt --replace --wait --dry-run example.com
    cli53 export --full --debug example.com > example.com.txt 2> example.com.err.log
  2. Manage reusable delegation sets

    main

    cli53 can create, list, and delete Route 53 reusable delegation sets.

    • Create: cli53 dscreate
    • List: cli53 dslist
    • Delete: cli53 dsdelete <delegation-set-id>
    cli53 dscreate
    cli53 dslist
    cli53 dsdelete NA24DEGBDGB32
  3. Configure Amazon credentials for cli53

    main

    cli53 uses standard AWS credential loading. You can configure access in two ways:

    1. AWS Credentials File

    Place your credentials in ~/.aws/credentials:

    [default]
    aws_access_key_id = AKID1234567890
    aws_secret_access_key = MY-SECRET-KEY

    2. Environment Variables

    Set the following environment variables:

    • AWS_ACCESS_KEY_ID
    • AWS_SECRET_ACCESS_KEY

    Switching Profiles and Roles

    • Profiles: Use the --profile flag or set the AWS_PROFILE environment variable to switch between credential sets.
    • Roles: Use the --role-arn flag to assume a specific IAM role. You can combine --profile and --role-arn.
    • Endpoint URL: Use the --endpoint-url flag to specify a custom Route 53 endpoint (useful for testing).
    cli53 list --profile my_profile
    cli53 list --role-arn arn:aws:iam::123456789012:role/myRole
    cli53 list --endpoint-url "http://localhost:4580"
  4. Create and manage individual resource records (rrcreate/rrdelete)

    main

    Use rrcreate to add records and rrdelete to remove them.

    Basic Records

    • A Record: cli53 rrcreate <domain> '<name> <ttl> A <ip>'
    • MX Record: cli53 rrcreate <domain> '@ MX 10 mail1.' '@ MX 20 mail2.'
    • CNAME Record:
      • Relative: cli53 rrcreate example.com 'login CNAME www'
      • Absolute: cli53 rrcreate example.com 'mail CNAME ghs.googlehosted.com.' (requires trailing dot)

    Advanced AWS Record Types

    • Weighted: Use --identifier <id> --weight <weight>.
    • Alias (to ELB): cli53 rrcreate <domain> 'www AWS ALIAS A <dns-name> <zone-id> false'
    • Alias (to A/CNAME record): cli53 rrcreate <domain> 'www AWS ALIAS A <identifier> $self false'
    • Geolocation: Use -i <location> --continent-code <code|--country-code <code|--subdivision-code <code>.
    • Failover: Use -i <id> --failover <PRIMARY|SECONDARY> --health-check <id>.
    • Multivalue: Use -i <id> --multivalue --health-check <id>.

    Deleting Records

    • cli53 rrdelete <domain> <name> <type>
    cli53 rrcreate example.com 'www 60 A 192.168.0.1'
    cli53 rrcreate --replace example.com 'www 60 A 192.168.0.2'
    cli53 rrdelete example.com www A
  5. Install cli53

    main

    You can install cli53 using one of the following methods:

    Download Binaries

    Download the appropriate binary for your platform (Linux, Mac, or Windows) from the GitHub releases page, then move it to your path:

    $ sudo mv cli53-my-platform /usr/local/bin/cli53
    $ sudo chmod +x /usr/local/bin/cli53

    Homebrew (macOS)

    $ brew install cli53

    Build from source

    If you are using Alpine on Docker, pre-built binaries may not work. You should build from source using Go (version >= 1.21):

    $ go install github.com/barnybug/cli53/cmd/cli53@latest

    This will place the cli53 binary in $GOPATH/bin (typically ~/go/bin).

    $ brew install cli53
  6. Manage hosted zones with cli53

    main

    Use the following commands to manage Route 53 hosted zones:

    • Create a zone: cli53 create <domain> --comment '<comment>'
    • List zones: cli53 list (supports -format json for scripting)
    • Delete a zone: (Implicitly handled via rrdelete or specific zone management commands if available, though list and create are the primary zone-level commands shown).

    Note on Private/Public Zones: If you have both a private and a public zone with the same name, use the 13-character Zone ID instead of the domain name to avoid ambiguity.

    Example using Zone ID:

    cli53 rrcreate ZZZZZZZZZZZZZ 'name A 127.0.0.1'
    cli53 create example.com --comment 'my first zone'
    cli53 list -format json | jq .[].Name
  7. Implement AWS Routing Policies with AWSRoute

    main

    AWS Route 53 supports several routing policies, each implemented as a type satisfying the AWSRoute interface. These types can be instantiated using the RoutingTypes map.

    Supported routing types:

    • FAILOVER: Uses FailoverRoute. Requires a failover key.
    • GEOLOCATION: Uses GeoLocationRoute. Supports optional countryCode, continentCode, and subdivisionCode.
    • LATENCY: Uses LatencyRoute. Requires a region key.
    • WEIGHTED: Uses WeightedRoute. Requires a weight key (integer).
    • MULTIVALUE: Uses MultiValueAnswerRoute. No additional parameters.
  8. Configure IAM permissions for cli53

    main

    To allow cli53 to manage Route 53, attach the following IAM policy to your user or role.

    Security Warning: The Cli53ManageZones and Cli53ManageDelegationSets statements use "Resource": "*", which grants permission to modify any hosted zone or delegation set in the account. For better security, scope these to specific ARNs:

    "Resource": [
      "arn:aws:route53:::delegationset/Z123ABC",
      "arn:aws:route53:::hostedzone/Z456DEF"
    ]
    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Sid": "Cli53AccountLevelRead",
          "Effect": "Allow",
          "Action": [
            "route53:ListHostedZones",
            "route53:ListHostedZonesByName",
            "route53:ListReusableDelegationSets"
          ],
          "Resource": "*"
        },
        {
          "Sid": "Cli53Create",
          "Effect": "Allow",
          "Action": [
            "route53:CreateHostedZone",
            "route53:CreateReusableDelegationSet"
          ],
          "Resource": "*"
        },
        {
          "Sid": "Cli53ManageZones",
          "Effect": "Allow",
          "Action": [
            "route53:GetHostedZone",
            "route53:ListResourceRecordSets",
            "route53:ChangeResourceRecordSets",
            "route53:DeleteHostedZone"
          ],
          "Resource": "*"
        },
        {
          "Sid": "Cli53ManageDelegationSets",
          "Effect": "Allow",
          "Action": [
            "route53:DeleteReusableDelegationSet"
          ],
          "Resource": "*"
        }
      ]
    }
  9. Manage Hosted Zones

    main

    The cli53 package provides functions to manage Amazon Route 53 hosted zones, including creation, listing, and deletion.

    • Create a zone: Use createZone to create a new hosted zone. You can optionally specify a VPC ID and region for private zones, or a delegationSetId.
    • List zones: Use listZones to retrieve all hosted zones. This function uses a paginator and accepts a Formatter to output the results.
    • Delete a zone: Use deleteZone to remove a hosted zone. If the purge flag is set to true, all non-authoritative records (excluding SOA and NS records matching the zone name) will be deleted before the zone itself is removed.
  10. Convert grouped DNS records to a Route 53 ResourceRecordSet

    main

    Use ConvertBindToRRSet to convert a slice of dns.RR records into a single route53types.ResourceRecordSet.

    Requirements:

    • The records in the slice must have been previously grouped by matching name, type, and (if applicable) identifier.
    • This function handles the conversion of AWS-specific routing policies (Failover, GeoLocation, Latency, Weighted, and MultiValueAnswer) and AWS extensions (like HealthCheckId and SetIdentifier) if the records are wrapped in *AWSRR.
    • It also handles dns.PrivateRR for AWS ALIAS records.
  11. Export Route 53 records to BIND format

    main

    You can export the current state of a hosted zone to BIND format using exportBind or ExportBindToWriter.

    • Full export: When full is true, the output includes the full domain names for all records.
    • Shortened export: When full is false, the output uses relative names (shortened by the $ORIGIN) and simplifies the output for easier reading.
    • Alias handling: The tool can automatically unexpand $self aliases back to the $self notation in the exported file using UnexpandSelfAliases.
  12. Delete specific DNS records

    main

    Use deleteRecord to remove specific records from a hosted zone. You can filter the deletion by:

    • Name: The exact domain name.
    • Type: The DNS record type (e.g., A, CNAME, TXT).
    • Identifier: The specific record identifier (used for extended records like weighted or geolocation records).

    If no records match the criteria, the function prints a warning.