我将此userData
放在userData
资源的property
LaunchConfiguration
中,我按照此AWS Doc中的步骤操作,这是脚本:< / p>
"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"
]]}
一切正常,文件/var/log/cloud-init-output.log
没有给出任何错误,但有一件事没有得到很好的执行:
在脚本中,我们有3个sed
命令用于替换:
region
/etc/awslogs/awscli.conf
clustername
/etc/awslogs/awslogs.conf
container_instance_id
。区域被替换没有问题,但最后两个没有被替换,当我检查文件时,我得到空格而不是{cluster}
和{container_instance_id}
脚本中的语法有什么问题吗?我直接在终端尝试了相同的命令,它们工作正常。任何帮助都非常感谢。谢谢。
答案 0 :(得分:0)
如果您将其移至:: Init并为其创建文件和命令,您可能会获得更好的反馈