Cloudformation模板在UserData完成之前完成了部署 [英] Cloudformation template completes deployment before UserData is finished

查看:93
本文介绍了Cloudformation模板在UserData完成之前完成了部署的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在部署的CloudFormation模板中,我正在UserData块中运行一些命令。
以下命令之一启动NICE DCV的会话:
https:/ /aws.amazon.com/hpc/dcv/

In the CloudFormation template I am deploying, I am running a few commands in the UserData block. One of these commands starts up a session for NICE DCV: https://aws.amazon.com/hpc/dcv/

如下所示:

"UserData": {
    "Fn::Base64" : {
        "Fn::Join" : [
        "",
        [
        dcv create-session --type virtual ",
        " --owner ubuntu",
        " --user ubuntu",
        " my-session, "\n",
        "while ! (dcv list-sessions | grep -q 'my-session'); do sleep 1; done\n"
        ]
        ]
     }
}

首先,我使用以下命令创建会话:

First, I create a session with command:

$ dcv create-session --type virtual --owner ubuntu --user ubuntu my-session

然后,我等待检查是否已使用以下命令成功创建会话:

Afterwards, I wait to check whether the session has been created successfully with command:

$ while ! (dcv list-sessions | grep -q 'my-session'); do sleep 1; done

我看到的问题是,我相信CloudFormation模板在完成部署之前 UserData脚本运行完毕。我相信是这样,因为如果我足够快地ssh进入实例,我将看到以下内容:

The issue I am seeing is that I believe the CloudFormation template is completing it's deployment before the UserData script finishes running. I believe this is the case because if I am quick enough and ssh into the instance, I will see something as follows:

$ dcv list-sessions
There are no sessions available
$ dcv list-sessions
There are no sessions available
$ dcv list-sessions
There are no sessions available
$ dcv list-sessions
There are no sessions available
$ dcv list-sessions
Session: 'my-session' (owner: ubuntu)

这表明实例中仍在运行某些东西。

which suggests that something was still running in the instance.

如何确保确实尊重UserData代码?

How can I make sure that the UserData code is actually respected?

推荐答案

以确保CloudFormation模板等待UserData脚本完成,您必须做两件事:

To ensure the CloudFormation template waits for the completion of the UserData script, you must do two things:


  1. 添加 CreationPolicy

在脚本中添加逻辑信号以指示其完成。此自定义逻辑使用 cfn信号实用程序,

Add logic in the script to signal its completion. This custom logic uses the cfn-signal utility, which you might have to install in your instance.






这是如何在实例中安装的模板现在看起来:


Here's how the template looks now:

"UserData": {
    "Fn::Base64" : {
        "Fn::Join" : [
        "",
        [
           "curl --silent --show-error --retry 5 https://bootstrap.pypa.io/get-pip.py | sudo python\n",
           "curl https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz -o /home/ubuntu/aws-cfn-bootstrap.tar.gz\n",
           "tar -xvzf /home/ubuntu/aws-cfn-bootstrap.tar.gz -C /home/ubuntu/\n",
           "rm /home/ubuntu/aws-cfn-bootstrap.tar.gz\n",
           "chmod +x /home/ubuntu/aws-cfn-bootstrap-*/bin/cfn-signal\n",
           "pip install /home/ubuntu/aws-cfn-bootstrap-*\n",
           "dcv create-session --type virtual ",
           " --owner ubuntu",
           " --user ubuntu",
           " my-session, "\n",
           "while ! (dcv list-sessions | grep -q 'my-session'); do sleep 1; done\n"
           "/home/ubuntu/aws-cfn-bootstrap-*/bin/cfn-signal -e $? ",
           " --stack ", { "Ref": "AWS::StackName" },
           " --resource MyInstance" ,
           " --region ", { "Ref" : "AWS::Region" }, "\n"
        ]
        ]
     }
},
"CreationPolicy": {
        "ResourceSignal" : {
            "Count": "1",
            "Timeout": "PT5M"
     }
}






分解脚本。


Breaking the script down.

此操作可获取并安装 cfn信号所需的 aws-cfn 工具集:

This fetches and installs the aws-cfn set of tools, needed for cfn-signal:

"curl --silent --show-error --retry 5 https://bootstrap.pypa.io/get-pip.py | sudo python\n",
"curl https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz -o /home/ubuntu/aws-cfn-bootstrap.tar.gz\n",
"tar -xvzf /home/ubuntu/aws-cfn-bootstrap.tar.gz -C /home/ubuntu/\n",
"rm /home/ubuntu/aws-cfn-bootstrap.tar.gz\n",
"chmod +x /home/ubuntu/aws-cfn-bootstrap-*/bin/cfn-signal\n",
"pip install /home/ubuntu/aws-cfn-bootstrap-*\n",

这是我的自定义脚本,我想确保在云形成完成部署之前完成该脚本。

This is my custom script, which I want to ensure is completed before the cloud formation finishes deployment:

"dcv create-session --type virtual ",
" --owner ubuntu",
" --user ubuntu",
" my-session, "\n",
"while ! (dcv list-sessions | grep -q 'my-session'); do sleep 1; done\n"

最后,我使用 cfn-signal 实用程序来指示脚本终止:

Finally, I use the cfn-signal utility to signal the termination of the script:

"/home/ubuntu/aws-cfn-bootstrap-*/bin/cfn-signal -e $? ",
" --stack ", { "Ref": "AWS::StackName" },
" --resource MyInstance" ,
" --region ", { "Ref" : "AWS::Region" }, "\n"

这篇关于Cloudformation模板在UserData完成之前完成了部署的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆