无法使用Cloudformation

时间:2017-12-05 13:06:57

标签: powershell amazon-web-services amazon-ec2 amazon-cloudformation aws-code-deploy

我正在尝试将codedeploy-agent.msi部署到ec2实例(win 2012)。它位于私有子网后面,但NAT gatewayS3 endpoint,但powershell.exe -Command Read-S3Object -BucketName aws-codedeploy-us-west-2 -Key latest/codedeploy-agent.msi -File codedeploy-agent.msi,我测试了此"WorkerInstance": { "Type": "AWS::EC2::Instance", "Metadata": { "AWS::CloudFormation::Init": { "config": { "commands": { "00-download-host-agent": { "command": { "Fn::Join": [ "", [ "powershell.exe -Command \"Read-S3Object ", "-BucketName aws-codedeploy-us-west-2 ", "-Key latest/codedeploy-agent.msi ", "-File codedeploy-agent.msi\"" ] ] }, "cwd": "C:/cfn", "waitAfterCompletion" : 0 }, "01-install-host-agent": { "command": "C:\\cfn\\codedeploy-agent.msi /quiet /l C:\\cfn\\host-agent-install-log.txt", "ignoreErrors": "true", "waitAfterCompletion" : 0 }, "02-signal-ready": { "command": { "Fn::Join": [ "", [ "\"C:\\Program Files\\Amazon\\cfn-bootstrap\\cfn-signal\"", " -e 0 \"", "\"" ] ] } } }, "services": { "windows": { "codedeploy-agent": { "enabled": "true", "ensureRunning": "true", "commands": [ "01-install-host-agent" ] } } } } } }, "Properties": { "DisableApiTermination": "false", "InstanceInitiatedShutdownBehavior": "stop", "IamInstanceProfile": { "Ref": "IAMRole" }, "ImageId": "ami-c55089bd", "InstanceType": "t2.medium", "KeyName": "mykey", "Monitoring": "true", "Tags": [{ "Key": "CodeDeployGroup", "Value": { "Fn::Join": ["-", ["app", { "Ref": "EnvType" }, { "Ref": "EnvVersion" }, "CodeDeployGroup" ]] } }, { "Key": "Name", "Value": { "Fn::Join": ["-", ["App", { "Ref": "EnvType" }, { "Ref": "EnvVersion" }, "Worker" ]] } } ], "NetworkInterfaces": [{ "DeleteOnTermination": "true", "Description": "Primary network interface", "DeviceIndex": 0, "SubnetId": "subnet-70234568", "GroupSet": ["sg-8affd7", "sg-fdffsfsd4"] } ] } } 正在运行。代理正在通过powershell从ec2实例下载。

但是,使用下面的cloudfromation脚本,可以在未安装代理的情况下创建实例。没有c:\ cfn文件夹,并且缺少cfn-init.log文件。可能是什么问题???

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="ke.lesson1.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

2 个答案:

答案 0 :(得分:2)

命令看起来很好。您可以尝试为powershell命令指定执行策略。这个CFN模板适合我:

        "WorkerInstance" : {
            "Type" : "AWS::EC2::Instance",
            "Metadata" : {
                "AWS::CloudFormation::Init" : {
                    "config" : {
                        "commands" : {
                            "00-download-host-agent" : {
                                "command" : {"Fn::Join" : [ "", [
                                    "powershell.exe -executionpolicy remotesigned -Command \"Read-S3Object ",
                                    "-BucketName aws-codedeploy-us-west-2 ",
                                    "-Key latest/codedeploy-agent.msi ",
                                    "-File codedeploy-agent.msi\""
                                ]]},
                                "cwd" : "C:/cfn",
                                "waitAfterCompletion" : 0
                            },
                            "01-install-host-agent" : {
                                "command" : "C:\\cfn\\codedeploy-agent.msi /quiet /l C:\\cfn\\host-agent-install-log.txt",
                                "ignoreErrors" : "true",
                                "waitAfterCompletion" : 0
                            },
                            "02-signal-ready" : {
                                "command" : {
                                    "Fn::Join" : [ "", [
                                        "\"C:\\Program Files\\Amazon\\cfn-bootstrap\\cfn-signal\"",
                                        " -e 0 \"",
                                        { "Ref" : "WaitHandle" },
                                        "\""
                                    ]]
                                },
                                "waitAfterCompletion" : 0
                            }
                        },
                        "services" : {
                            "windows" : {
                                "codedeploy-agent" : {
                                    "enabled" : "true",
                                    "ensureRunning" : "true",
                                    "commands" : [ "01-install-host-agent" ]
                                }
                            }
                        }
                    }
                }
            },

答案 1 :(得分:1)

我不确定,为什么它会起作用。我最终通过将其作为userdata脚本来实现它。

"UserData": {
                    "Fn::Base64": {
                            "Fn::Join": ["", ["<script>\n", "mkdir c:\\cfn\n", "mkdir c:\\cfn\\log\n",
                                "powershell.exe Read-S3Object -BucketName aws-codedeploy-us-west-2/latest -Key codedeploy-agent.msi -File c:\\cfn\\codedeploy-agent.msi\n",
                                "c:\\cfn\\codedeploy-agent.msi /quiet /l c:\\cfn\\host-agent-install-log.txt\n",
                                "c:\\\"Program Files\"\\Amazon\\cfn-bootstrap\\cfn-init.exe -s ", {
                                    "Ref": "AWS::StackName"
                                }, " --region ", {
                                    "Ref": "AWS::Region"
                                }, " > c:\\cfn\\log\\cfn-call-log 2>&1", "</script>"]]                      
                    }
                },

这将安装代理以及启用和启动服务。