CloudFormation - Install and configure CloudWatch Agent to EC2 Instances - amazon-web-services

I have a CloudFormation template that create ec2 instances using LaunchTemplate, but I want to add cloudwatch agent to the instances, but I can't figure what's wrong.
I have 2 configSets, but the cfn-init get stuck in "02_download_app" > "Command run_app", this part I run a jar file from /.
Here is the output from /var/log/cfn-init-cmd.log, I noticed that after the command run_app the jar get stucked 17:25, and some time later 17:42 I connect to the instance kill the jar and relaunch and it continues the script successfully, I think the error is some encoding problem, but I don't know where: in the jar? in log4j config file?, etc.
2020-06-23 17:25:20,679 main ERROR Console contains an invalid element or attribute "Encoding"
configSets:
setup:
- "01_install_java"
- "02_download_app"
- "03_setupCfnHup"
- "04_config-amazon-cloudwatch-agent"
- "05_restart_amazon-cloudwatch-agent"
updateEnvironment:
- "04_config-amazon-cloudwatch-agent"
- "05_restart_amazon-cloudwatch-agent"
I followed this template, here is part of my template and the cfn-init part.
AWSTemplateFormatVersion: 2010-09-09
Description: >-
Cloudformation template.
Metadata:
Stack:
Value: 2
VersionDate:
Value: 30032020
Identifier:
Value: template-app-stack
Input:
Description: EC2 Instance Types, DB Instance Type and Engine
Output:
Description: Outputs ID of all deployed resources
AWS::CloudFormation::Interface:
Parameters:
#....... other omitted .......#
#....... other omitted .......#
Resources:
#....... other omitted .......#
#backend autoscaling#
rBackendFleet:
Type: AWS::AutoScaling::AutoScalingGroup
Condition: cIsProduction
Properties:
AutoScalingGroupName: !Join [ '-', [!Ref pApplicationName, 'asg', !Ref pApplicationEnvironment, 'backend'] ]
VPCZoneIdentifier: !Ref pBackendSubnets
MinSize: 1
MaxSize: 10
MetricsCollection:
- Granularity: 1Minute
DesiredCapacity: 1
HealthCheckGracePeriod: 300
TargetGroupARNs:
- !Ref rPrivateLoadBalancerTargetGroup
LaunchTemplate:
LaunchTemplateId: !Ref rBackendLaunchTemplate
Version: !GetAtt rBackendLaunchTemplate.LatestVersionNumber
rBackendLaunchTemplate:
Type: AWS::EC2::LaunchTemplate
Metadata:
AWS::CloudFormation::Init:
configSets:
setup:
- "01_install_java"
- "02_download_app"
- "03_setupCfnHup"
- "04_config-amazon-cloudwatch-agent"
- "05_restart_amazon-cloudwatch-agent"
updateEnvironment:
- "04_config-amazon-cloudwatch-agent"
- "05_restart_amazon-cloudwatch-agent"
01_install_java:
packages:
apt:
unzip: []
openjdk-8-jre-headless: []
02_download_app:
files:
/tmp/backend.zip:
source: !Sub
- >-
https://${bucketName}.s3-${region}.amazonaws.com/${objectKey}
- bucketName: !Ref pSourceCodeBucketName
objectKey: !Ref pBackendArtifactObjectKey
region: !Ref AWS::Region
mode: "000644"
owner: "root"
group: "root"
authentication: rolebased
commands:
configure_app:
command: "sudo unzip backend.zip"
test: "sudo ls -la"
cwd: "/tmp"
run_app:
command: "sudo java -jar /tmp/app.jar &"
test: "sudo ps -ef | grep java"
cwd: "/"
04_config-amazon-cloudwatch-agent:
files:
/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json:
content: !Sub |
{
"metrics": {
"append_dimensions": {
"AutoScalingGroupName": "${!aws:AutoScalingGroupName}",
"ImageId": "${!aws:ImageId}",
"InstanceId": "${!aws:InstanceId}",
"InstanceType": "${!aws:InstanceType}"
},
"aggregation_dimensions":
[["AutoScalingGroupName"], ["InstanceId", "InstanceType"],[]]
,
"metrics_collected": {
"mem": {
"measurement": [
"mem_used_percent"
]
},
"swap": {
"measurement": [
"swap_used_percent"
]
}
}
},
"logs": {
"logs_collected": {
"files": {
"collect_list": [
{
"file_path": "/profuturo/app/profuturo-rest-comunicaciones/logs/profuturo-rest-comunicaciones.log",
"log_group_name": "/var/log/${pApplicationName}-${pApplicationEnvironment}",
"log_stream_name": "{instance_id}",
"timestamp_format": "%Y-%m-%d %H:%M:%S,%f",
"multi_line_start_pattern": "{timestamp_format}"
}
]
}
}
}
}
05_restart_amazon-cloudwatch-agent:
commands:
01_stop_service:
command: "/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a stop"
02_start_service:
command: "/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json -s"
03_setupCfnHup:
files:
/etc/cfn/cfn-hup.conf:
content: !Sub |
[main]
stack=${AWS::StackName}
region=${AWS::Region}
verbose=true
interval=1
mode: "000400"
owner: "root"
group: "root"
/etc/cfn/hooks.d/amazon-cloudwatch-agent-auto-reloader.conf:
content: !Sub |
[cfn-auto-reloader-hook]
triggers=post.update
path=Resources.rBackendLaunchTemplate.Metadata.AWS::CloudFormation::Init.04_config-amazon-cloudwatch-agent
action=/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource rBackendLaunchTemplate --configsets updateEnvironment --region ${AWS::Region}
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
mode: "000400"
owner: "root"
group: "root"
commands:
01enable_cfn_hup:
command: "systemctl enable cfn-hup.service"
02start_cfn_hup:
command: "systemctl start cfn-hup.service"
AWS::CloudFormation::Authentication:
rolebased:
type: S3
buckets:
- !Sub ${pSourceCodeBucketName}
roleName:
Ref: pInstanceRole
Properties:
LaunchTemplateName: !Join [ '-', [!Ref pApplicationName, 'lt', !Ref pApplicationEnvironment, 'backend'] ]
LaunchTemplateData:
BlockDeviceMappings:
- Ebs:
VolumeSize: 10
VolumeType: gp2
DeviceName: /dev/sda1
SecurityGroupIds:
- !Ref rBackendSecurityGroup
EbsOptimized: false
IamInstanceProfile:
Arn: !GetAtt rRootInstanceProfile.Arn
ImageId: !Ref pBackendAMI
InstanceType: !Ref pBackendInstanceType
KeyName: !Ref pKeyName
Monitoring:
Enabled: true
UserData:
Fn::Base64: !Sub |
#!/bin/bash -xe
sudo apt-get update -y
sudo apt-get install -y python
sudo apt-get install -y python-setuptools
sudo apt-get install -y python-pip
wget https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb -O /tmp/amazon-cloudwatch-agent.deb
dpkg -i /tmp/amazon-cloudwatch-agent.deb
sudo mkdir -p /opt/aws/bin
sudo python /usr/lib/python2.7/dist-packages/easy_install.py --script-dir /opt/aws/bin https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz
sudo /opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource rBackendLaunchTemplate --configsets setup,updateEnvironment --region ${AWS::Region}
sudo /opt/aws/bin/cfn-signal -e $? -r "Backend setup done." --stack ${AWS::StackName} --resource rBackendLaunchTemplate --region ${AWS::Region}
........
#App Tier Autoscaling#
#....... other omitted .......#
I watched /var/log/cloud-init-output.log, but no clue of the error, says that is some encoding problem but in what step?
+ sudo /opt/aws/bin/cfn-init -v --stack cwp-prod --resource rBackendLaunchTemplate --configsets setup,updateEnvironment --region us-west-2
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 306: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 309: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 207: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 284: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 290: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 180: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 306: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 309: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 207: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 284: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 290: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 180: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 13224: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 306: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 309: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 207: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 284: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 290: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 180: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 306: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 309: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 207: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 284: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 290: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 180: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 306: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 309: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 207: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 284: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 290: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 180: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 306: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 309: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 207: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 284: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 290: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 180: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 306: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 309: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 207: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 306: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 309: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 207: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 284: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 290: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 180: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 13233: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 306: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 309: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 207: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 284: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 290: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 180: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 114: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 130: ordinal not in range(128)
Logged from file util.py, line 503
+ sudo /opt/aws/bin/cfn-signal -e 0 -r 'Backend setup done.' --stack cwp-prod --resource rBackendLaunchTemplate --region us-west-2
ValidationError: Stack arn:aws:cloudformation:us-west-2:496852590701:stack/cwp-prod/80743380-b533-11ea-9ea4-02e40fd26800 is in CREATE_COMPLETE state and cannot be signaled
Cloud-init v. 19.3-41-gc4735dd3-0ubuntu1~18.04.1 running 'modules:final' at Tue, 23 Jun 2020 20:31:35 +0000. Up 23.76 seconds.
2020-06-23 21:31:14,217 - util.py[WARNING]: Failed running /var/lib/cloud/instance/scripts/part-001 [1]
2020-06-23 21:31:14,223 - cc_scripts_user.py[WARNING]: Failed to run module scripts-user (scripts in /var/lib/cloud/instance/scripts)
2020-06-23 21:31:14,226 - util.py[WARNING]: Running module scripts-user (<module 'cloudinit.config.cc_scripts_user' from '/usr/lib/python3/dist-packages/cloudinit/config/cc_scripts_user.py'>) failed
Cloud-init v. 19.3-41-gc4735dd3-0ubuntu1~18.04.1 finished at Tue, 23 Jun 2020 21:31:14 +0000. Datasource DataSourceEc2Local. Up 3602.81 seconds

I guess you are referring this sudo java -jar /tmp/app.jar &? I think you should kill it in your Init after your test finishes.
One way to do this could be as follows:
run_app:
command: |
sudo java -jar /tmp/app.jar &
PROCESS_ID=$!
test: "sudo ps -ef | grep java"
cwd: "/"
run_app_kill:
command: "sudo kill -9 $PROCESS_ID"
I think also sudo is not needed in general. Since you are not numbering the steps, have to make sure run_app_kill runs after run_app.

After some research and looking other related questions, I found that the problem was the output of the command sudo java -jar /tmp/app.jar &, (up to here I didn't know if the output was too long or the ascii characters of the banner), this jar is an spring boot app and by default the logger was in DEBUG mode, so I tried to reduce output by setting INFO, but it didn't worked, then I realized that maybe the spring boot banner is causing this, so I run the app redirecting the output to /dev/null, after that, the cfn-script ran all the configSets successfully.
sudo java -jar /tmp/app.jar > /dev/null 2>&1 &
+ sudo /opt/aws/bin/cfn-init -v --stack cwp-prod --resource rBackendLaunchTemplate --configsets setup --region us-west-2
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 114: ordinal not in range(128)
Logged from file util.py, line 503
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 130: ordinal not in range(128)
Logged from file util.py, line 503
More info here:
http://www.codeandcompost.com/post/cfn,-utf8-and-two-days-i%E2%80%99ll-never-get-back
https://forums.aws.amazon.com/thread.jspa?threadID=182478

Related

UnicodeEncodeError: 'ascii' codec can't encode character u'\xbf' in position 3: ordinal not in range(128)

I encounter an error running a script I wrote on an Ubuntu box with python 2.7.
It seems there is some issue with unicode yet I cannot figure it out.
I tried to encode the variables with UTF-8 yet im not even sure which one causes the error "str(count)" or "tag[u'Value']..
Traceback (most recent call last):
File "./AWS_collection_unix.py", line 105, in <module>
main()
File "./AWS_collection_unix.py", line 91, in main
ec2_tags_per_region(region, text_file)
File "./AWS_collection_unix.py", line 65, in ec2_tags_per_region
print_ids_and_tags(instance, text_file)
File "./AWS_collection_unix.py", line 16, in print_ids_and_tags
text_file.write('%s. %s' % (str(count), tag[u'Value']))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xbf' in position 3: ordinal not in range(128)
The error doesnt specify in which parameter the error is..
UnicodeEncodeError: 'ascii' codec can't encode character u'\xbf' in position 3: ordinal not in range(128)
How can I make this work appropriately?
Thanks
The shorties solution is to warp count with -> unicode(count).encode('utf-8'),
What will convert count to be utf-8 encoded str.
But the best way is to understand what is the encoding of the count variable.

gcloud components update fails with UnicodeDecodeError in MacOS

gcloud components update fails with UnicodeDecodeError in MacOS. Its not clear what would be causing this issue in MacOS and it says Hotfix for UnicodeDecodeError issue affecting logging. How do I get past this issue?
This is my current configuration
gcloud -v
Google Cloud SDK 190.0.0
beta 2017.09.15
bq 2.0.29
core 2018.02.16
gsutil 4.28
Updates are available for some Cloud SDK components. To install them,
please run:
$ gcloud components update
This is the error I get
Container Engine
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 859, in emit
msg = self.format(record)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 732, in format
return fmt.format(record)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 474, in format
s = self._fmt % record.__dict__
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 6: ordinal not in range(128)
Logged from file log.py, line 184
▪ Added container/use_v1_api_client property as an alias of
container/use_v1_api. container/use_v1_api is still supported.
Kubernetes Engine
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 859, in emit
msg = self.format(record)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 732, in format
return fmt.format(record)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 474, in format
s = self._fmt % record.__dict__
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 6: ordinal not in range(128)
Logged from file log.py, line 184
▪ Added --enable-pod-security-policy flag to enable PodSecurityPolicy
enforcement in clusters: gcloud <alpha|beta> container clusters
<create|update> --enable-pod-security-policy
Stackdriver Debugger
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 859, in emit
msg = self.format(record)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 732, in format
return fmt.format(record)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 474, in format
s = self._fmt % record.__dict__
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 6: ordinal not in range(128)
Logged from file log.py, line 184
▪ Changed gcloud beta source upload command to use the .gcloudignore
file if present. If not present and .gitignore is, .gitignore will
still be used. See gcloud topic gcloudignore to learn more.
190.0.1 (2018-02-22)
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 859, in emit
msg = self.format(record)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 732, in format
return fmt.format(record)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 474, in format
s = self._fmt % record.__dict__
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 6: ordinal not in range(128)
Logged from file log.py, line 184
▪ Hotfix for UnicodeDecodeError issue affecting logging.
I can see your Python version is Ok (2.7).
You may need to run gcloud init before trying to update.
If the issue persists or you already ran the aforementioned command, it would be faster to reinstall gcloud. Current version was released today (198.0.0).

Django 1.9 + Python 2.7: style.css did not take effect due to UnicodeDecodeError

I am trying to follow Django tutorial (Django 1.9, Python 2.7) to implement my polls project, but style.css did not take effect (neither the color nor the backgroud image).
https://docs.djangoproject.com/en/1.9/intro/tutorial06/
When running the server, I observed the UnicodeDecodeError due to 'ascii' codec.
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb0 in position 1: ordinal not in range(128)
I followed the suggestion by set system default encoding to utf8, but when server was running it threw similar error due to 'utf8'.
How to fix: "UnicodeDecodeError: 'ascii' codec can't decode byte"
UnicodeDecodeError: 'utf8' codec can't decode byte 0xb0 in position 1: invalid start byte
So I think neither ascii nor utf8 is enough, and how can I fix it?
I saw some posts provided some suggestion like json.loads, but I have no idea how to add in my Django project.
json.loads(unicode(opener.open(...), "ISO-8859-1")),
utf8' codec can't decode byte 0x89 in position 15: invalid start byte
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 3-6: invalid data
Issue Full Trace:
[17/Mar/2016 12:26:28] "GET /polls/ HTTP/1.1" 200 161
Traceback (most recent call last):
File "C:\Python27\lib\wsgiref\handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "C:\Python27\lib\site-packages\django\contrib\staticfiles\handlers.py", line 64, in __call__
return super(StaticFilesHandler, self).__call__(environ, start_response)
File "C:\Python27\lib\site-packages\django\core\handlers\wsgi.py", line 177, in __call__
response = self.get_response(request)
File "C:\Python27\lib\site-packages\django\contrib\staticfiles\handlers.py", line 54, in get_response
return self.serve(request)
File "C:\Python27\lib\site-packages\django\contrib\staticfiles\handlers.py", line 47, in serve
return serve(request, self.file_path(request.path), insecure=True)
File "C:\Python27\lib\site-packages\django\contrib\staticfiles\views.py", line 40, in serve
return static.serve(request, path, document_root=document_root, **kwargs)
File "C:\Python27\lib\site-packages\django\views\static.py", line 66, in serve content_type, encoding = mimetypes.guess_type(fullpath)
File "C:\Python27\lib\mimetypes.py", line 297, in guess_type
init()
File "C:\Python27\lib\mimetypes.py", line 358, in init db.read_windows_registry()
File "C:\Python27\lib\mimetypes.py", line 258, in read_windows_registry
for subkeyname in enum_types(hkcr):
File "C:\Python27\lib\mimetypes.py", line 249, in enum_types
ctype = ctype.encode(default_encoding) # omit in 3.x!
UnicodeDecodeError: 'utf8' codec can't decode byte 0xb0 in position 1: invalid start byte

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position

When I try to extract some pattern from a tagged text in nltk, I have the error: UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 79: ordinal not in range(128). Firstly I had not this error, but I got it only after installing some packages.
this is the code:
# -*- coding: utf-8 -*-
import codecs
import sys
import re
import sys
import nltk
from nltk.corpus import *
k = nltk.corpus.brown.tagged_words('myfile')
for (w1,t1), (w2,t2) in nltk.bigrams(k):
if t1 == 'NN' and t2 == 'AJ':
print w1, w2
this is the entire output of the code.
Traceback (most recent call last):
File "/home/fathi/egfe.py", line 12, in <module>
for (w1,t1), (w2,t2) in nltk.bigrams(k):
File "/usr/local/lib/python2.7/dist-packages/nltk/util.py", line 442, in bigrams
for item in ngrams(sequence, 2, **kwargs):
File "/usr/local/lib/python2.7/dist-packages/nltk/util.py", line 419, in ngrams
history.append(next(sequence))
File "/usr/local/lib/python2.7/dist-packages/nltk/corpus/reader/util.py", line 291, in iterate_from
tokens = self.read_block(self._stream)
File "/usr/local/lib/python2.7/dist-packages/nltk/corpus/reader/tagged.py", line 241, in read_block
for para_str in self._para_block_reader(stream):
File "/usr/local/lib/python2.7/dist-packages/nltk/corpus/reader/util.py", line 564, in read_blankline_block
line = stream.readline()
File "/usr/local/lib/python2.7/dist-packages/nltk/data.py", line 1095, in readline
new_chars = self._read(readsize)
File "/usr/local/lib/python2.7/dist-packages/nltk/data.py", line 1322, in _read
chars, bytes_decoded = self._incr_decode(bytes)
File "/usr/local/lib/python2.7/dist-packages/nltk/data.py", line 1352, in _incr_decode
return self.decode(bytes, 'strict')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 79: ordinal not in range(128)
The problem is that the ntlk version is not compatabile with the python version, so it requires an older version of the nltk toolkit.

Trouble with pip install selenium

I have Vista(please avoid comment ;D)... I'm sure of the right install of pip because I have installed other package but when I try to install selenium by pip that's the result.:
C:\Program Files\Python2.7.6>pip install selenium
Downloading/unpacking selenium Running setup.py
(path:c:\users\gianlu~1\appdata\local\temp\pip_build_Gianluca
«\selenium\setup.py) egg_info for package selenium
Cleaning up...
The line below are in red(this is my comment)
Exception:
Traceback (most recent call last):
File "C:\Program Files\Python2.7.6\lib\site-packages\pip\basecommand.py", line
122, in main
status = self.run(options, args)
File "C:\Program Files\Python2.7.6\lib\site-packages\pip\commands\install.py",
line 278, in run
requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundl
e=self.bundle)
File "C:\Program Files\Python2.7.6\lib\site-packages\pip\req.py", line 1234, i
n prepare_files
req_to_install.assert_source_matches_version()
File "C:\Program Files\Python2.7.6\lib\site-packages\pip\req.py", line 464, in
assert_source_matches_version
% (display_path(self.source_dir), version, self))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xae in position 65: ordinal
not in range(128)
End of the red
Traceback (most recent call last):
File "C:\Program Files\Python2.7.6\lib\runpy.py", line 162, in _run_module_as_
main
"__main__", fname, loader, pkg_name)
File "C:\Program Files\Python2.7.6\lib\runpy.py", line 72, in _run_code
exec code in run_globals
File "C:\Program Files\Python2.7.6\pip.exe\__main__.py", line 9, in <module>
File "C:\Program Files\Python2.7.6\lib\site-packages\pip\__init__.py", line 18
5, in main
return command.main(cmd_args)
File "C:\Program Files\Python2.7.6\lib\site-packages\pip\basecommand.py", line
161, in main
text = '\n'.join(complete_log)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xae in position 80: ordinal
not in range(128)
I'm new of Phyton and that is over for me. The only thing that I can suppose and investigate is that the matter is about the last line "Unicode...".
Thank you for any suggestion.
Yes, really a bad question. I was very tired after a code marathon ;D. Starting from my edit of investigate the unicode error and only after a few hours of sleep I find a lot of intresting and working solution. Here the more considerable and useful link:
Python ez_install : UnicodeDecodeError: 'ascii' codec can't decode byte 0xae in position 11
UnicodeDecodeError : 'ascii' codec can't decode byte 0xe0 in position 0: ordinal not in range(128)
pip install tabulate UnicodeDecodeError