Amazon web services 无法使用CloudFormation模板SSH到EC2实例
我想在容器实例启动时启动一个任务,所以我遵循了提供MIME多部分用户数据脚本的this。我创建了一个云形成模板,用MIME多部分用户数据脚本启动一个实例 EC2资源已使用云形成模板创建,但我无法通过SSH连接到该实例,也无法从EC2管理控制台查看系统日志 云信息模板Amazon web services 无法使用CloudFormation模板SSH到EC2实例,amazon-web-services,amazon-ec2,ssh,amazon-cloudformation,Amazon Web Services,Amazon Ec2,Ssh,Amazon Cloudformation,我想在容器实例启动时启动一个任务,所以我遵循了提供MIME多部分用户数据脚本的this。我创建了一个云形成模板,用MIME多部分用户数据脚本启动一个实例 EC2资源已使用云形成模板创建,但我无法通过SSH连接到该实例,也无法从EC2管理控制台查看系统日志 云信息模板 { "AWSTemplateFormatVersion" : "2010-09-09", "Description" :" ECS instance", "Parameters" : { },
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" :" ECS instance",
"Parameters" : {
},
"Resources" :{
"EC2Instance":{
"Type" : "AWS::EC2::Instance",
"Properties" : {
"SecurityGroupIds":["sg-16021f35"]
"ImageId" : "ami-ec33cc96",
"UserData":{
"Fn::Base64":{
"Fn::Join":[
"\n",
[
{
"Fn::Join":[
"",
[
"Content-Type: multipart/mixed; boundary=",
"==BOUNDARY=="
]
]
},
"MIME-Version: 1.0",
"--==BOUNDARY==",
{
"Fn::Join":[
"",
[
"Content-Type: text/upstart-job; charset=",
"us-ascii"
]
]
},
"#!/bin/bash",
"# Specify the cluster that the container instance should register into",
"echo ECS_CLUSTER=Demo >> /etc/ecs/ecs.config",
"# Install the AWS CLI and the jq JSON parser",
"yum install -y aws-cli jq",
"#upstart-job",
{
"Fn::Join":[
" ",[
"description",
"Amazon EC2 Container Service (start task on instance boot)"
]
]
},
{
"Fn::Join":[
" ",[
"author",
"Amazon Web Services"
]
]
},
"start on started ecs",
"script",
"exec 2>>/var/log/ecs/ecs-start-task.log",
"set -x",
"until curl -s http://localhost:51678/v1/metadata",
"do",
"sleep 1",
"done",
"# Grab the container instance ARN and AWS region from instance metadata",
"instance_arn=$(curl -s http://localhost:51678/v1/metadata | jq -r '. | .ContainerInstanceArn' | awk -F/ '{print $NF}' )",
"cluster=$(curl -s http://localhost:51678/v1/metadata | jq -r '. | .Cluster' | awk -F/ '{print $NF}' )",
"region=$(curl -s http://localhost:51678/v1/metadata | jq -r '. | .ContainerInstanceArn' | awk -F: '{print $4}')",
"# Specify the task definition to run at launch",
"task_definition=ASG-Task",
"# Run the AWS CLI start-task command to start your task on this container instance",
"aws ecs start-task --cluster $cluster --task-definition $task_definition --container-instances $instance_arn --started-by $instance_arn",
"end script",
"--==BOUNDARY==--"
]
]
}
},
"IamInstanceProfile":"ecsInstanceRole",
"InstanceType":"t2.micro",
"SubnetId":"subnet-841103e1"
}
}
},
"Outputs" : {
}
}
MIME多部分用户数据:
Content-Type: multipart/mixed; boundary="==BOUNDARY=="
MIME-Version: 1.0
--==BOUNDARY==
Content-Type: text/x-shellscript; charset="us-ascii"
#!/bin/bash
# Specify the cluster that the container instance should register into
cluster=your_cluster_name
# Write the cluster configuration variable to the ecs.config file
# (add any other configuration variables here also)
echo ECS_CLUSTER=$cluster >> /etc/ecs/ecs.config
# Install the AWS CLI and the jq JSON parser
yum install -y aws-cli jq
--==BOUNDARY==
Content-Type: text/upstart-job; charset="us-ascii"
#upstart-job
description "Amazon EC2 Container Service (start task on instance boot)"
author "Amazon Web Services"
start on started ecs
script
exec 2>>/var/log/ecs/ecs-start-task.log
set -x
until curl -s http://localhost:51678/v1/metadata
do
sleep 1
done
# Grab the container instance ARN and AWS region from instance metadata
instance_arn=$(curl -s http://localhost:51678/v1/metadata | jq -r '. | .ContainerInstanceArn' | awk -F/ '{print $NF}' )
cluster=$(curl -s http://localhost:51678/v1/metadata | jq -r '. | .Cluster' | awk -F/ '{print $NF}' )
region=$(curl -s http://localhost:51678/v1/metadata | jq -r '. | .ContainerInstanceArn' | awk -F: '{print $4}')
# Specify the task definition to run at launch
task_definition=my_task_def
# Run the AWS CLI start-task command to start your task on this container instance
aws ecs start-task --cluster $cluster --task-definition $task_definition --container-instances $instance_arn --started-by $instance_arn --region $region
end script
--==BOUNDARY==--
您需要按照上面jarmod的建议在您的表单模板中指定密钥文件和安全组。您需要按照上面jarmod的建议在表单模板中指定密钥文件和安全组。我在您的CloudFormation模板中没有看到任何安全组。除非打开端口22,否则无法SSH到EC2实例。请参阅和。已更新模板。我已经添加了安全组,在启动堆栈时将端口22打开给公众(0.0.0.0/0)。有趣的是,当我通过向导(管理控制台)启动实例时,它工作得很好。我想我在cloud formation template中的用户数据中缺少了一些东西从模板中剥离用户数据,看看是否可以让基本的SSH到EC2正常工作。问题可能不是用户数据。请在重新引入userdata之前解决这个问题。我在CloudFormation模板中没有看到任何安全组。除非打开端口22,否则无法SSH到EC2实例。请参阅和。已更新模板。我已经添加了安全组,在启动堆栈时将端口22打开给公众(0.0.0.0/0)。有趣的是,当我通过向导(管理控制台)启动实例时,它工作得很好。我想我在cloud formation template中的用户数据中缺少了一些东西从模板中剥离用户数据,看看是否可以让基本的SSH到EC2正常工作。问题可能不是用户数据。在重新引入userdata之前修复这个问题。从cloudformation文档开始,它不是必需的。无论如何,我已经在模板中给出了安全组。从cloudformation文档来看,它不是必需的。无论如何,我已经在模板中指定了安全组。