繁体   English   中英

无法使用CloudFormation模板SSH进入EC2实例

[英]Not able to SSH into the EC2 Instance with CloudFormation Template

我想从容器实例启动时开始执行任务。因此,我遵循了实例启动文档中的启动任务,文档提供了MIME多部分用户数据脚本。 我创建了一个云形成模板,以使用MIME多部分用户数据脚本启动实例。

EC2资源已使用Cloud形成模板创建,但是我无法通过SSH进入该实例,也无法从EC2管理控制台获得系统日志。

CloudFormation模板

{
    "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的建议在您的结构模板中指定密钥文件和安全组。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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