--- AWSTemplateFormatVersion: '2010-09-09' Description: 'Template to install aws-otel-collector on amazon linux. It was validated on amazon linux 2' Parameters: KeyName: Description: Name of an existing EC2 KeyPair to enable SSH access to the instance Type: AWS::EC2::KeyPair::KeyName ConstraintDescription: must be the name of an existing EC2 KeyPair. InstanceType: Description: EC2 instance type Type: String Default: m4.2xlarge ConstraintDescription: must be a valid EC2 instance type. InstanceAMI: Description: Managed AMI ID for EC2 Instance Type : String Default: ami-7707a10f IAMRole: Description: EC2 attached IAM role Type: String Default: AWSOTelRole ConstraintDescription: must be an existing IAM role which will be attached to EC2 instance. SSHLocation: Description: The IP address range that can be used to SSH to the EC2 instances Type: String MinLength: '9' MaxLength: '18' Default: 0.0.0.0/0 AllowedPattern: (\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/(\d{1,2}) ConstraintDescription: must be a valid IP CIDR range of the form x.x.x.x/x. Resources: EC2Instance: Type: AWS::EC2::Instance Metadata: AWS::CloudFormation::Init: configSets: default: - 01_setupCfnHup - 02_config-aws-otel-collector - 03_restart-aws-otel-collector UpdateEnvironment: - 02_config-aws-otel-collector - 03_restart-aws-otel-collector # Definition of YAML configuration of aws-otel-collector, you can change the configuration below. 02_config-aws-otel-collector: files: '/opt/aws/aws-otel-collector/etc/config.yaml': content: !Sub | extensions: health_check: receivers: otlp: protocols: grpc: endpoint: 0.0.0.0:4317 exporters: awsxray: awsemf: service: pipelines: traces: receivers: [otlp] exporters: [awsxray] metrics: receivers: [otlp] exporters: [awsemf] # Invoke aws-otel-collector-ctl to restart aws-otel-collector. 03_restart-aws-otel-collector: commands: 01_stop_service: command: sudo /opt/aws/aws-otel-collector/bin/aws-otel-collector-ctl -a stop 02_start_service: command: sudo /opt/aws/aws-otel-collector/bin/aws-otel-collector-ctl -a start # Cfn-hup setting, it is to monitor the change of metadata. # When there is change in the contents of json file in the metadata section, cfn-hup will call cfn-init to restart aws-otel-collector. 01_setupCfnHup: files: '/etc/cfn/cfn-hup.conf': content: !Sub | [main] stack=${AWS::StackId} region=${AWS::Region} interval=1 mode: '000400' owner: root group: root '/etc/cfn/hooks.d/aws-otel-collector-auto-reloader.conf': content: !Sub | [cfn-auto-reloader-hook] triggers=post.update path=Resources.EC2Instance.Metadata.AWS::CloudFormation::Init.02_config-aws-otel-collector action=/opt/aws/bin/cfn-init -v --stack ${AWS::StackId} --resource EC2Instance --region ${AWS::Region} --configsets UpdateEnvironment runas=root mode: '000400' owner: root group: root "/lib/systemd/system/cfn-hup.service": content: !Sub | [Unit] Description=cfn-hup daemon [Service] Type=simple ExecStart=/opt/aws/bin/cfn-hup Restart=always [Install] WantedBy=multi-user.target commands: 01enable_cfn_hup: command: !Sub | systemctl enable cfn-hup.service 02start_cfn_hup: command: !Sub | systemctl start cfn-hup.service Properties: InstanceType: Ref: InstanceType IamInstanceProfile: Ref: IAMRole KeyName: Ref: KeyName ImageId: Ref: InstanceAMI SecurityGroups: - Ref: InstanceSecurityGroup UserData: # This script below is to install aws-otel-collector, restart aws-otel-collector and tell the result to cloudformation. Fn::Base64: !Sub | #!/bin/bash sudo rpm -Uvh https://aws-otel-collector.s3.amazonaws.com/amazon_linux/amd64/latest/aws-otel-collector.rpm /opt/aws/bin/cfn-init -v --stack ${AWS::StackId} --resource EC2Instance --region ${AWS::Region} --configsets default /opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackId} --resource EC2Instance --region ${AWS::Region} CreationPolicy: ResourceSignal: Count: 1 Timeout: "PT15M" InstanceSecurityGroup: Type: AWS::EC2::SecurityGroup Properties: GroupDescription: Enable SSH access via port 22 SecurityGroupIngress: - IpProtocol: tcp FromPort: '22' ToPort: '22' CidrIp: Ref: SSHLocation