拓冰建站拓冰建站
首页 / 资讯中心 / 正文

如何用 Terraform 在 AWS EC2 上部署 Infisical Relay Server 并完成认证配置?

如何用 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默认一次性注册 token1 小时过期用于引导 Relay 注册AWSRelay 用宿主机可解析到的 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-methodtoken \ --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 tokenuser_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生成新的。端口规则的作用可以对照总览文档的入站/出站规则表理解入站 8443TCP供 Infisical 平台与 Relay 通信入站 2222TCP供 Gateway 建立 SSH 反向隧道出站 443TCP到 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),仅供参考
分享:

看完干货,该让你的企业上线了

免费需求沟通 · 48 小时内出具建站方案 · 河南本地可上门