Skip to content

hazelops/terraform-aws-tailscale

Repository files navigation

Terraform AWS Tailscale Module

This module is used to deploy a Tailscale router instance to set up access from VPC to the Tailscale Cloud.

Module logic is the following:

  1. Connect to TailScale API using the Terraform Provider and Tailscale api token.
  2. Generate TailScale Auth Key and place it into the instance.
  3. Create an Autoscale Group with a single instance using and connect it to the TailScale network.

Usage

Please refer to Tailscale Configuration first

module "tailscale" {
  source            = "registry.terraform.io/hazelops/tailscale/aws"
  version           = "~>0.2"
  allowed_cidr_blocks = ["0.0.0.0/0"] # Please lock this down to your specific CIDR
  ec2_key_pair_name = "default-key"
  env               = "prod"
  subnets           = ["subnet-0000000", "subnet-0000000"]
  vpc_id            = "vpc-0000000"
  api_token         = "00000000000000000000000000" # Please don't store secrets in plain text
}

More examples can be found in the examples directory.

Tailscale Configuration

  1. Create Tailscale API access token (More info on Access tokens can be found here
  2. Add tag to the ACL control list. ACL should look like this:
{
"acls": [
  {
    "action": "accept",
    "ports": [
      "*:*"
    ],
    "users": [
      "*"
    ]
  }
],
"tagOwners": {
  "tag:<your-environment>": []
}
}

Make sure to approve the advertised route:

  1. Go to Machines page
  2. Find the machine and click on the ... button.
  3. Select "Edit route settings", check the checkbox and then click "Save".

The tag must be added to the ACL to disable automatic key expiration!

Default parameter for tag is tag:<your-environment>.

More examples can be found in Tailscale Tag Docs.

  1. Create AWS SSM Parameter using the obtained Tailscale API access token. For example, use the following path pattern: <env-name>/global/tailscale_api_token. For more information please refer to AWS Docs.
  2. Add data source to Terraform code like in the example configuration main.tf file.
  3. In the module call parameters, set api_token variable like in the example configuration main.tf file.
  4. Alternatively Tailscale API token could be set as string, but this is very unsafe, therefore it is * highly not recommended* to do this.

Troubleshooting

The following error may occur during module removal:

Error: Provider configuration not present

To work with module.tailscale.tailscale_tailnet_key.this (orphan) its
original provider configuration at
module.tailscale.provider["registry.terraform.io/tailscale/tailscale"] is
required, but it has been removed. This occurs when a provider
configuration is removed while objects created by that provider still exist
in the state. Re-add the provider configuration to destroy
module.tailscale.tailscale_tailnet_key.this (orphan), after which you can
remove the provider configuration again.

To remove it, run the following code:

terraform state rm module.tailscale.tailscale_tailnet_key.this

Requirements

Name Version
terraform >=1.2.0
aws >=4.30.0
local ~> 1.2
tailscale 0.13.13
template >=2.2

Providers

Name Version
aws >=4.30.0
tailscale 0.13.13
template >=2.2

Modules

No modules.

Resources

Name Type
aws_autoscaling_group.this resource
aws_iam_instance_profile.this resource
aws_iam_role.this resource
aws_iam_role_policy_attachment.this resource
aws_launch_template.this resource
aws_security_group.this resource
tailscale_tailnet_key.this resource
aws_ami.this data source
aws_iam_policy_document.this data source
template_file.ec2_user_data data source

Inputs

Name Description Type Default Required
allowed_cidr_blocks List of network subnets that are allowed. According to PCI-DSS, CIS AWS and SOC2 providing a default wide-open CIDR is not secure. list(string) n/a yes
ami_id Optional AMI ID for Tailscale instance. Otherwise latest Amazon Linux will be used. string "" no
api_token Set Tailscale API access token here string n/a yes
asg Scaling settings of an Auto Scaling Group map
{
"max_size": 1,
"min_size": 1
}
no
ec2_key_pair_name n/a string n/a yes
env n/a string n/a yes
ext_security_groups External security groups to add to the Tailscale instance list(any) [] no
instance_type Set type of Tailscale instance string "t3.nano" no
key_ephemeral Indicates if the key is ephemeral bool true no
key_expiry The expiry of the key in seconds. Defaults to 7776000 (90 days) number 7776000 no
key_preauthorized Determines whether or not the machines authenticated by the key will be authorized for the tailnet by default bool true no
key_reusable Indicates if the key is reusable or single-use bool true no
monitoring_enabled Enable monitoring for the Auto Scaling Group bool true no
name Set a name for Tailscale instance string "tailscale-router" no
public_ip_enabled Enable Public IP for Tailscale instance bool false no
ssm_role_arn SSM role to attach to a Tailscale instance string "arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforSSM" no
subnets Subnets where the Taiscale instance will be placed. It is recommended to use a private subnet for better security. list(string) n/a yes
tags A device is automatically tagged when it is authenticated with this key list(string) [] no
vpc_id n/a string n/a yes

Outputs

Name Description
autoscaling_group_id n/a
name n/a
security_group_id n/a