AWSTemplateFormatVersion: "2010-09-09" Description: >- Observer Agent on a small EC2 instance in your own VPC. It checks the services it can reach from the subnet you choose and only dials out to Observer. No inbound ports. https://docs.use.observer # Long-form intrinsic functions (Fn::Sub, Ref) on purpose: the file stays # plain YAML, so tests and the smoke run can parse it without CloudFormation # tag support. Metadata: AWS::CloudFormation::Interface: ParameterGroups: - Label: { default: Observer } Parameters: [AgentKey, CloudServerUrl] - Label: { default: Network } Parameters: [VpcId, SubnetId, AssignPublicIp] - Label: { default: Instance } Parameters: [InstanceType, LatestAmiId] ParameterLabels: AgentKey: { default: Agent key } CloudServerUrl: { default: Observer address } VpcId: { default: VPC } SubnetId: { default: Subnet } AssignPublicIp: { default: Public IP for outbound traffic } InstanceType: { default: Instance type } LatestAmiId: { default: Image } Parameters: AgentKey: Type: String NoEcho: true AllowedPattern: "^obs_live_[A-Za-z0-9_-]{43}$" ConstraintDescription: "the key starts with obs_live_ (Observer: Agents, Add agent)" Description: >- From Observer: Agents, Add agent. Stored in AWS Secrets Manager and read by the instance at boot; it never appears in the instance user data. CloudServerUrl: Type: String Default: https://use.observer AllowedPattern: "^https?://[A-Za-z0-9._~:/?#@!&+,;=%-]+$" Description: Leave as is unless Observer told you otherwise. VpcId: Type: AWS::EC2::VPC::Id Description: The VPC of the services the agent should check. SubnetId: Type: AWS::EC2::Subnet::Id Description: >- A subnet in that VPC with outbound internet access: a public subnet (keep the public IP on) or a private subnet behind a NAT gateway (turn it off). AssignPublicIp: Type: String Default: "true" AllowedValues: ["true", "false"] Description: Needed in a public subnet for outbound traffic. Nothing listens on it. InstanceType: Type: String Default: t4g.nano AllowedValues: [t4g.nano, t4g.micro, t4g.small] Description: The agent is small; t4g.nano is enough for most setups. LatestAmiId: Type: AWS::SSM::Parameter::Value Default: /aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-arm64 Description: Amazon Linux 2023 (arm64). Leave as is. Resources: AgentKeySecret: Type: AWS::SecretsManager::Secret Properties: Description: Observer Agent key SecretString: { Ref: AgentKey } InstanceRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: "2012-10-17" Statement: - Effect: Allow Principal: { Service: ec2.amazonaws.com } Action: sts:AssumeRole # Session Manager instead of SSH: read the agent's logs without opening a port. ManagedPolicyArns: - { "Fn::Sub": "arn:${AWS::Partition}:iam::aws:policy/AmazonSSMManagedInstanceCore" } Policies: - PolicyName: read-agent-key PolicyDocument: Version: "2012-10-17" Statement: - Effect: Allow Action: secretsmanager:GetSecretValue Resource: { Ref: AgentKeySecret } InstanceProfile: Type: AWS::IAM::InstanceProfile Properties: Roles: [{ Ref: InstanceRole }] SecurityGroup: Type: AWS::EC2::SecurityGroup Properties: GroupDescription: Observer Agent (outbound only) VpcId: { Ref: VpcId } SecurityGroupEgress: - IpProtocol: "-1" CidrIp: 0.0.0.0/0 Description: The agent dials out to Observer and to the services it checks - IpProtocol: "-1" CidrIpv6: ::/0 Description: The agent dials out to Observer and to the services it checks LaunchTemplate: Type: AWS::EC2::LaunchTemplate Properties: LaunchTemplateData: MetadataOptions: HttpTokens: required HttpPutResponseHopLimit: 1 BlockDeviceMappings: - DeviceName: /dev/xvda Ebs: VolumeType: gp3 VolumeSize: 8 Encrypted: true DeleteOnTermination: true Instance: Type: AWS::EC2::Instance Properties: ImageId: { Ref: LatestAmiId } InstanceType: { Ref: InstanceType } IamInstanceProfile: { Ref: InstanceProfile } LaunchTemplate: LaunchTemplateId: { Ref: LaunchTemplate } Version: { "Fn::GetAtt": [LaunchTemplate, LatestVersionNumber] } NetworkInterfaces: - DeviceIndex: "0" SubnetId: { Ref: SubnetId } AssociatePublicIpAddress: { Ref: AssignPublicIp } GroupSet: [{ Ref: SecurityGroup }] Tags: - Key: Name Value: observer-agent # Runs once, at first boot. The key comes from Secrets Manager, so it is # never in the user data. The install script (from the latest agent # release on GitHub, else use.observer) installs the agent binary as the # observer-agent systemd service. UserData: "Fn::Base64": "Fn::Sub": | #!/bin/bash set -euo pipefail KEY="$(aws secretsmanager get-secret-value --region ${AWS::Region} --secret-id ${AgentKeySecret} --query SecretString --output text)" curl -fsSL --retry 5 https://github.com/useobserver/agent/releases/latest/download/install.sh -o /tmp/observer-install.sh || curl -fsSL --retry 5 https://use.observer/install.sh -o /tmp/observer-install.sh AGENT_KEY="$KEY" sh /tmp/observer-install.sh --cloud-url "${CloudServerUrl}" rm -f /tmp/observer-install.sh Outputs: InstanceId: Description: The agent's instance Value: { Ref: Instance } Logs: Description: Read the agent's logs from Session Manager Value: "Fn::Sub": "aws ssm start-session --region ${AWS::Region} --target ${Instance} (then: sudo journalctl -u observer-agent -f)"