如何用 Terraform 在 AWS EC2 上部署 Infisical Relay Server 并完成认证配置?
【免费下载链接】infisicalInfisical is the open-source platform for secrets, certificates, and privileged access management.项目地址: https://gitcode.com/GitHub_Trending/in/infisical
如果你的 Infisical 需要访问 VPC 内的私有网络资源(数据库、内部 API),又不想把这些资源暴露到公网,就需要部署 Infisical Relay:它是一个加密流量的路由层,负责在 Infisical 平台与你部署的 Gateway 之间转发流量。本文覆盖的任务是:用一份 Terraform 配置在 AWS EC2 上一键拉起 Relay 服务器,并完成 Relay 与 Infisical 实例之间的认证配置,最后验证 Relay 处于健康状态。
这条路径来自 Infisical 官方文档 Terraform 部署指南 与 Relay 部署总览,配置中的认证方式与 CLI 参数可在 infisical relay 命令参考 中核对。
部署前先确认的前提条件
根据 Terraform 指南的 Prerequisites 一节,开始前需要满足:
- 一个有权限创建 EC2 实例、Security Group 和 Elastic IP 的 AWS 账号;
- 目标 AWS 区域中已有的 VPC 和 Subnet ID;
- 所选操作系统的 AMI ID(官方示例使用 Ubuntu 22.04 LTS 的 AMI);
- 让 Relay 认证到 Infisical 实例的凭据。官方 Terraform 示例使用 token 认证,其他方法可在 relay 命令参考中查看。
另外,Relay 部署总览文档给出一份判断标准:Infisical Cloud 用户在 US/EU 区域已有托管 Relay,无需自部署;只有自托管 Infisical、使用企业专属实例、需要更低延迟或完全掌控 Relay 基础设施时才需要部署自己的 Relay。确认你需要自部署后再继续。
在 Infisical UI 中创建 Relay 并选择认证方式
Terraform 只负责拉起机器和安装服务,Relay 本身要先在 Infisical 控制台创建。按总览文档的 UI 步骤:
- 进入Organization Settings > Networking > Relays;
- 点击Create Relay;
- 输入 Relay 名称和 host address(即服务器的静态 IP 或 DNS 名——本方案中这个静态 IP 由 Terraform 分配的 Elastic IP 提供);
- (可选)打开新 Relay 的详情页面,点击Authentication旁边的编辑图标切换认证方式。文档列出两种:
- Token(默认):一次性注册 token,1 小时过期,用于引导 Relay 注册;
- AWS:Relay 用宿主机可解析到的 AWS 凭据(实例角色、环境变量或 shared profile)签名
sts:GetCallerIdentity请求完成认证,需要配置允许的主账号 ARN 和/或账号 ID;
- 点击Show deploy command,复制生成的 CLI 命令,其中包含你要填进 Terraform 脚本的 enrollment token。
Terraform 配置:EC2 实例、安全组与认证脚本
把下面这份完整配置保存为main.tf(来自官方文档原文,未做删改)。它完成三件事:创建放行必要端口的安全组、创建带 Elastic IP 的 EC2 实例、通过user_data启动脚本安装 Infisical CLI 并以 systemd 服务形式注册 Relay。
terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } } } provider "aws" { region = "us-west-2" # Change to your desired AWS region } # Security Group for the Infisical Relay instance resource "aws_security_group" "infisical_relay_sg" { name = "infisical-relay-sg" description = "Allows inbound traffic for Infisical Relay and SSH" vpc_id = "vpc-0c71f9c5709d88d18" # Change to your VPC ID # Inbound: Allows the Infisical platform to securely communicate with the Relay server. ingress { from_port = 8443 to_port = 8443 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } # Inbound: Allows Infisical Gateway to securely communicate via the Relay. ingress { from_port = 2222 to_port = 2222 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } # Inbound: Allows secure shell (SSH) access for administration. ingress { from_port = 22 to_port = 22 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] # Restrict this to your IP in production } # Outbound: Allows the Relay server to make necessary outbound connections to the Infisical platform. egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } tags = { Name = "infisical-relay-sg" } } # Elastic IP for a static public IP address resource "aws_eip" "infisical_relay_eip" { tags = { Name = "infisical-relay-eip" } } # EC2 instance to run Infisical Relay module "infisical_relay_instance" { source = "terraform-aws-modules/ec2-instance/aws" version = "~> 5.6" name = "infisical-relay-example" ami = "ami-065778886ef8ec7c8" # Change to your desired AMI ID instance_type = "t3.micro" subnet_id = "subnet-0fd2337a1c604a494" # Change to your Subnet ID vpc_security_group_ids = [aws_security_group.infisical_relay_sg.id] associate_public_ip_address = false # We are using an Elastic IP instead user_data = <<-EOT #!/bin/bash set -e # Install Infisical CLI curl -1sLf 'https://artifacts-cli.infisical.com/setup.deb.sh' | bash apt-get update && apt-get install -y infisical # Install the relay as a systemd service. # Create the relay in the Infisical UI first, then use the enrollment token here. # # Note: For production environments, you might consider fetching the token from AWS Parameter Store or AWS Secrets Manager. sudo infisical relay systemd install my-relay-example \ --enroll-method=token \ --token "your-enrollment-token" \ --domain "https://app.infisical.com" # Start and enable the service to run on boot. # The systemd service is named after the relay. sudo systemctl start my-relay-example EOT } # Associate the Elastic IP with the EC2 instance resource "aws_eip_association" "eip_assoc" { instance_id = module.infisical_relay_instance.id allocation_id = aws_eip.infisical_relay_eip.id }配置中必须替换的值(对应文档 "Customize values" 一节):
provider块中的region:改成你的 AWS 区域;aws_security_group中的vpc_id:改成你的 VPC ID;- 模块中的
ami和subnet_id:改成你的 AMI 和子网; user_data中的--token:填入 Relay 详情页获取的 enrollment token;user_data中的--domain:自托管 Infisical 时改成你的实例域名,使用 Infisical Cloud 则保持https://app.infisical.com。
认证相关的两个要点:
my-relay-example是 systemd 位置参数,也就是 relay 名称,必须与你在 UI 中创建 Relay 时使用的名称一致(relay 命令参考中--name的定义是"必须匹配在 dashboard 中创建 relay 时使用的名称");user_data里的服务名、sudo systemctl start my-relay-example中的名称要与上面保持一致,因为 systemd 服务以 relay 名称命名。
官方文档同时提醒:生产环境可以考虑把 token 从 AWS Parameter Store 或 AWS Secrets Manager 中读取,而不是硬编码在main.tf里;注册 token 是一次性的且 1 小时过期,过期前未完成部署就回 Relay 详情页重新点Show deploy command生成新的。
端口规则的作用可以对照总览文档的入站/出站规则表理解:入站 8443(TCP)供 Infisical 平台与 Relay 通信,入站 2222(TCP)供 Gateway 建立 SSH 反向隧道,出站 443(TCP)到 Infisical 实例用于 API 通信和证书请求。
执行 Terraform 部署
在main.tf所在目录依次执行:
terraform init terraform plan terraform applyterraform plan输出预期资源后再apply。apply 成功后,EC2 实例启动时会自动运行user_data脚本:安装 Infisical CLI,执行infisical relay systemd install完成注册,并用sudo systemctl start启动以 relay 命名的 systemd 服务(服务开机自启)。
验证 Relay 部署结果
总览文档给出的验证分三步:
检查日志中是否有 "Relay server started successfully" 消息。SSH 到实例后用 relay 命令参考中给出的命令查看服务状态和日志:
sudo systemctl status my-relay-example sudo journalctl -u my-relay-example -fsystemd 服务文件位于
/etc/systemd/system/<name>.service,配置目录为/etc/infisical/relays/。在 UI 中确认注册状态:进入Networking > Relays,选择你的 relay,确认状态显示为 "Healthy"。
测试连通性:部署一个经此 relay 路由的 Gateway,确认流量能走通。
连接不通时的排查命令
总览文档的 FAQ 给出了两条针对性检查命令:
# 从 relay 上测试出站 API 访问;自托管时把 URL 换成你的 Infisical 实例 curl -I https://app.infisical.com # 从平台一侧测试到 relay 的 TLS 端口 openssl s_client -connect <relay-ip>:8443其中<relay-ip>替换为你的 Elastic IP。如果平台连不上 relay,文档指出的检查方向是防火墙规则是否放行了入站 TCP 8443(带 TLS)。
生产环境限制与注意事项
官方文档对这份配置本身给出了两条明确的边界说明:
- 示例安全组的入站规则为了简化对
0.0.0.0/0开放,文档警告生产环境应把cidr_blocks限制为已知 IP 地址,尤其是 SSH 22 端口; - 如果 relay 服务器宕机,经过它的 Gateway 会断连:配置了自动 relay 选择的 Gateway 会切换到其他健康 relay,绑定特定 relay 的 Gateway 会在其恢复后自动重连;恢复前经该 relay 访问的 secrets 和资源暂时不可用。文档建议生产环境部署多个 relay 避免单点,并用 systemd 或容器编排实现故障自动重启。
另外,relay 服务器无法解密经过它的流量:客户端与 Gateway 之间的 mTLS 流量再经 SSH 隧道二次加密,relay 只负责路由这两层加密的流量,这是文档 FAQ 中明确说明的端到端加密设计。
Relay 安装后,后续管理命令(如infisical relay systemd uninstall移除服务)可继续查阅 infisical relay 命令参考,完整部署流程见 Relay 部署总览。
【免费下载链接】infisicalInfisical is the open-source platform for secrets, certificates, and privileged access management.项目地址: https://gitcode.com/GitHub_Trending/in/infisical
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考