简体   繁体   中英

Error while executing the userData in ECS optimized instance

I put this userData in the userData property of the LaunchConfiguration resource, I followed the steps in this AWS Doc , here is the script:

      "UserData":{  "Fn::Base64" : {
        "Fn::Join" : ["", [
          "#!/bin/bash -xv\n",
          "yum -y install aws-cfn-bootstrap\n",
          "/opt/aws/bin/cfn-init -v",
          "         --stack ", { "Ref": "AWS::StackName" },
          "         --resource LaunchConfig",
          "         --region ", { "Ref" : "AWS::Region" },"\n",
          "yum -y install awslogs jq\n",
          "# Inject the CloudWatch Logs configuration file contents\n",
          "cat > /etc/awslogs/awslogs.conf <<- EOF\n",
          "[general]\n",
          "state_file = /var/lib/awslogs/agent-state\n\n",

          "[/var/log/dmesg]\n",
          "file = /var/log/dmesg\n",
          "log_group_name = /var/log/dmesg\n",
          "log_stream_name = {cluster}/{container_instance_id}\n\n",

          "[/var/log/messages]\n",
          "file = /var/log/messages\n",
          "log_group_name = /var/log/messages\n",
          "log_stream_name = {cluster}/{container_instance_id}\n",
          "datetime_format = %b %d %H:%M:%S\n\n",

          "[/var/log/docker]\n",
          "file = /var/log/docker\n",
          "log_group_name = /var/log/docker\n",
          "log_stream_name = {cluster}/{container_instance_id}\n",
          "datetime_format = %Y-%m-%dT%H:%M:%S.%f\n\n",

          "[/var/log/ecs/ecs-init.log]\n",
          "file = /var/log/ecs/ecs-init.log.*\n",
          "log_group_name = /var/log/ecs/ecs-init.log\n",
          "log_stream_name = {cluster}/{container_instance_id}\n",
          "datetime_format = %Y-%m-%dT%H:%M:%SZ\n\n",

          "[/var/log/ecs/ecs-agent.log]\n",
          "file = /var/log/ecs/ecs-agent.log.*\n",
          "log_group_name = /var/log/ecs/ecs-agent.log\n",
          "log_stream_name = {cluster}/{container_instance_id}\n",
          "datetime_format = %Y-%m-%dT%H:%M:%SZ\n\n",

          "[/var/log/ecs/audit.log]\n",
          "file = /var/log/ecs/audit.log.*\n",
          "log_group_name = /var/log/ecs/audit.log\n",
          "log_stream_name = {cluster}/{container_instance_id}\n",
          "datetime_format = %Y-%m-%dT%H:%M:%SZ\n\n",

          "EOF\n",
          "# Set the region to send CloudWatch Logs data to (the region where the container instance is located)\n",
          "sed -i -e \"s/region = us-east-1/region = $(curl http://169.254.169.254/latest/meta-data/placement/availability-zone | sed s'/.$//')/g\" /etc/awslogs/awscli.conf\n",
          "# Grab the cluster and container instance ARN from instance metadata\n",
          "# Replace the cluster name and container instance ID placeholders with the actual values\n",
          "sed -i -e \"s/{cluster}/$(curl -s http://localhost:51678/v1/metadata | jq -r '. | .Cluster')/g\" /etc/awslogs/awslogs.conf\n",
          "sed -i -e \"s/{container_instance_id}/$(curl -s http://localhost:51678/v1/metadata | jq -r '. | .ContainerInstanceArn' | awk -F/ '{print $2}' )/g\" /etc/awslogs/awslogs.conf\n",
          "service awslogs start\n",
          "chkconfig awslogs on\n"
        ]]}

Everything works fine, the file /var/log/cloud-init-output.log didn't give any error, but there is one thing that does not get well executed: in the script we have 3 sed commands that we use to replace :

  • the region in the file /etc/awslogs/awscli.conf
  • the clustername in the file /etc/awslogs/awslogs.conf
  • the container_instance_id in the same file.

The region gets replaced without a problem, but the last two ones are not getting replaced, and when I checked the file, I got spaces instead of {cluster} and {container_instance_id}

Is there anything wrong with the syntax in the script? I tried the same commands directly in the terminal and they worked fine. Any help is really appreciated. Thank you.

如果您将其移动到:: Init并为其创建文件和命令,您可能会得到更好的反馈

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM