Search code examples
sftpterraformterraform-provider-aws

Is it possible to setup a custom hostname for AWS Transfer SFTP via Terraform


I'm trying to set up an SFTP server with a custom hostname using AWS Transfer. I'm managing the resource using Terraform. I've currently got the resource up and running, and I've used Terraform to create a Route53 record to point to the SFTP server, but the custom hostname entry on the SFTP dashboard is reading as blank.

And of course, when I create the server manually throught the AWS console and associate a route53 record with it, it looks like what I would expect:

I've looked through the terraform resource documentation and I've tried to see how it might be done via aws cli or cloudformation, but I haven't had any luck.

My server resource looks like:

resource "aws_transfer_server" "sftp" {
  identity_provider_type = "SERVICE_MANAGED"
  logging_role           = "${aws_iam_role.logging.arn}"
  force_destroy          = "false"

  tags {
    Name = ${local.product}-${terraform.workspace}"
  }
}

and my Route53 record looks like:

resource "aws_route53_record" "dns_record_cname" {
  zone_id = "${data.aws_route53_zone.sftp.zone_id}"
  name    = "${local.product}-${terraform.workspace}"
  type    = "CNAME"
  records = ["${aws_transfer_server.sftp.endpoint}"]
  ttl = "300"
}

Functionally, I can move forward with what I have, I can connect to the server with my DNS, but I'm trying to understand the complete picture.


Solution

  • In AWS,

    When you create a server using AWS Cloud Development Kit (AWS CDK) or through the CLI, you must add a tag if you want that server to have a custom hostname. When you create a Transfer Family server by using the console, the tagging is done automatically.

    So, you will need to be able to add those tags using Terraform. In v4.35.0 they added support for a new resource: aws_transfer_tag.

    An example supplied in the GitHub Issue:

    resource "aws_transfer_server" "with_custom_domain" {
      # config here
    }
    
    resource "aws_transfer_tag" "with_custom_domain_route53_zone_id" {
      resource_arn = aws_transfer_server.with_custom_domain.arn
      key          = "aws:transfer:route53HostedZoneId"
      value        = "/hostedzone/ABCDE1111222233334444"
    }
    
    resource "aws_transfer_tag" "with_custom_domain_name" {
      resource_arn = aws_transfer_server.with_custom_domain.arn
      key          = "aws:transfer:customHostname"
      value        = "abc.example.com"
    }