简体   繁体   English

Windows CloudFormation 脚本、元数据命令未运行

[英]Windows CloudFormation Script, MetaData commands are not running

Below is my CloudFormation template for creating a windows EC2 instance with Java and Tomcat installed on it.下面是我的 CloudFormation 模板,用于创建安装了 Java 和 Tomcat 的 Windows EC2 实例。 However, the nothing from the MetaData is being executed.但是,正在执行 MetaData 中的 nothing。 I login to the created EC2 instance, and none of the specified folders in the metadata can be found.我登录到创建的 EC2 实例,在元数据中找不到任何指定的文件夹。 What is my CloudFormation script missing?我的 CloudFormation 脚本缺少什么?

Thank you谢谢

{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Script to create Windows AMI",
"Metadata": {
    "AWS::CloudFormation::Designer": {
        "94153bdc-589b-4aa8-b859-5e84a1051a50": {
            "size": {
                "width": 60,
                "height": 60
            },
            "position": {
                "x": 280,
                "y": 110
            },
            "z": 1,
            "embeds": []
        }
    }
},
"Parameters": {},
"Rules": {},
"Mappings": {},
"Resources": {
    "JavaTomcatEC2Instance": {
        "Type": "AWS::EC2::Instance",
        "Properties": {
            "ImageId": "ami-06b19063",
            "InstanceType": "t2.micro",
            "KeyName": "WindowsTest"
        },
        "Metadata": {
            "AWS::CloudFormation::Init": {
                "configSets": {
                    "config": [
                        "setup"
                    ]
                },
                "setup": {
                    "Install-Java-Tomcat-set-env-variables-paths": {
                        "files": {
                            "c:\\cfn\\modules\\jdk-8u151-windows-x64.exe": {
                                "source": "https://s3.amazonaws.com/windows-ami-software/jdk-8u151-windows-x64.exe"
                            },
                            "c:\\cfn\\modules\\apache-tomcat-8.5.23.exe": {
                                "source": "https://s3.amazonaws.com/windows-ami-software/apache-tomcat-8.5.23.exe"
                            },
                            "c:\\cfn\\scripts\\Install-Java-JDK.ps1": {
                                "content": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "Set-Location C:\\cfn\\modules;",
                                            ".\\jdk-8u151-windows-x64.exe /s ADDLOCAL=\"ToolsFeature,SourceFeature,PublicjreFeature\"",
                                            "\n"
                                        ]
                                    ]
                                }
                            },
                            "c:\\cfn\\scripts\\Install-Tomcat8.ps1": {
                                "content": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "Set-Location C:\\cfn\\modules;",
                                            ".\\apache-tomcat-8.5.23.exe /S",
                                            "\n"
                                        ]
                                    ]
                                }
                            },
                            "c:\\cfn\\scripts\\Set-Java-Tomcat8-Paths-Homes.ps1": {
                                "content": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "$oldPath=(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Control\\Session Manager\\Environment' -Name PATH).Path;",
                                            "$addedFolder = 'C:\\Program Files\\Java\\jdk1.8.0_151\\bin; C:\\Program Files\\Apache Software Foundation\\Tomcat 8.5\\bin';",
                                            "$newPath = $oldPath +';'+$addedFolder;",
                                            "Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Control\\Session Manager\\Environment' -Name PATH -Value $newPath ;",
                                            "[Environment]::SetEnvironmentVariable('CATALINA_HOME', 'C:\\Program Files\\Apache Software Foundation\\Tomcat 8.5\\', 'Machine');",
                                            "[Environment]::SetEnvironmentVariable('JAVA_HOME', 'C:\\Program Files\\Java\\jdk1.8.0_151\\', 'Machine');",
                                            "Restart-Computer -Force;",
                                            "\n"
                                        ]
                                    ]
                                }
                            },
                            "c:\\cfn\\scripts\\Install-Tomcat8-Service.ps1": {
                                "content": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "Set-Location 'C:\\Program Files\\Apache Software Foundation\\Tomcat 8.5\\bin';",
                                            ".\\service.bat install;",
                                            "Set-Service Tomcat8 -StartupType Automatic;",
                                            "Start-Service Tomcat8;",
                                            "\n"
                                        ]
                                    ]
                                }
                            }
                        },
                        "commands": {
                            "a-Install-Java-JDK": {
                                "command": "powershell.exe -ExecutionPolicy RemoteSigned -Command c:\\cfn\\scripts\\Install-Java-JDK.ps1",
                                "waitAfterCompletion": "30"
                            },
                            "b-Install-Tomcat8": {
                                "command": "powershell.exe -ExecutionPolicy RemoteSigned -Command c:\\cfn\\scripts\\Install-Tomcat8.ps1",
                                "waitAfterCompletion": "30"
                            },
                            "c-Set-Java-Tomcat8-Paths-Homes": {
                                "command": "powershell.exe -ExecutionPolicy RemoteSigned -Command c:\\cfn\\scripts\\Set-Java-Tomcat8-Paths-Homes.ps1",
                                "waitAfterCompletion": "forever"
                            },
                            "d-Install-Tomcat8-Service": {
                                "command": "powershell.exe -ExecutionPolicy RemoteSigned -Command c:\\cfn\\scripts\\Install-Tomcat8-Service.ps1",
                                "waitAfterCompletion": "30"
                            }
                        }
                    }
                }
            },
            "AWS::CloudFormation::Designer": {
                "id": "94153bdc-589b-4aa8-b859-5e84a1051a50"
            }
        }
    }
}

} }

There are 2 things you need to check:您需要检查两件事:

  1. Check if those scripts are getting created on location C:\\cfn\\scripts --IF not then check where is the issue检查这些脚本是否是在 C:\\cfn\\scripts 位置创建的——如果不是,则检查问题出在哪里

  2. If these scripts are created there, then call separately the step "commands" in configSets (line no. 33-34).如果这些脚本是在那里创建的,则单独调用configSets的步骤"commands" (第 33-34 行)。 I suspect that is the problem where the actual scripts are not getting called.我怀疑这是没有调用实际脚本的问题。

Instead of using "setup" block, try something like this.不要使用“设置”块,而是尝试这样的事情。 This is how I have been using.这就是我一直在使用的方式。

"Metadata": {
    "AWS::CloudFormation::Init": {
            "config": {
                "files": {
                    "c:\\cfn\\modules\\jdk-8u151-windows-x64.exe": {
                        "source": "https://s3.amazonaws.com/windows-ami-software/jdk-8u151-windows-x64.exe"
                    },
                    "c:\\cfn\\modules\\apache-tomcat-8.5.23.exe": {
                        "source": "https://s3.amazonaws.com/windows-ami-software/apache-tomcat-8.5.23.exe"
                    },
                    "c:\\cfn\\scripts\\Install-Java-JDK.ps1": {
                        "content": {
                            "Fn::Join": [
                                "",
                                [
                                    "Set-Location C:\\cfn\\modules;",
                                    ".\\jdk-8u151-windows-x64.exe /s ADDLOCAL=\"ToolsFeature,SourceFeature,PublicjreFeature\"",
                                    "\n"
                                ]
                            ]
                        }
                    },
                    "c:\\cfn\\scripts\\Install-Tomcat8.ps1": {
                        "content": {
                            "Fn::Join": [
                                "",
                                [
                                    "Set-Location C:\\cfn\\modules;",
                                    ".\\apache-tomcat-8.5.23.exe /S",
                                    "\n"
                                ]
                            ]
                        }
                    },
                    "c:\\cfn\\scripts\\Set-Java-Tomcat8-Paths-Homes.ps1": {
                        "content": {
                            "Fn::Join": [
                                "",
                                [
                                    "$oldPath=(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Control\\Session Manager\\Environment' -Name PATH).Path;",
                                    "$addedFolder = 'C:\\Program Files\\Java\\jdk1.8.0_151\\bin; C:\\Program Files\\Apache Software Foundation\\Tomcat 8.5\\bin';",
                                    "$newPath = $oldPath +';'+$addedFolder;",
                                    "Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Control\\Session Manager\\Environment' -Name PATH -Value $newPath ;",
                                    "[Environment]::SetEnvironmentVariable('CATALINA_HOME', 'C:\\Program Files\\Apache Software Foundation\\Tomcat 8.5\\', 'Machine');",
                                    "[Environment]::SetEnvironmentVariable('JAVA_HOME', 'C:\\Program Files\\Java\\jdk1.8.0_151\\', 'Machine');",
                                    "Restart-Computer -Force;",
                                    "\n"
                                ]
                            ]
                        }
                    },
                    "c:\\cfn\\scripts\\Install-Tomcat8-Service.ps1": {
                        "content": {
                            "Fn::Join": [
                                "",
                                [
                                    "Set-Location 'C:\\Program Files\\Apache Software Foundation\\Tomcat 8.5\\bin';",
                                    ".\\service.bat install;",
                                    "Set-Service Tomcat8 -StartupType Automatic;",
                                    "Start-Service Tomcat8;",
                                    "\n"
                                ]
                            ]
                        }
                    }
                },
                "commands": {
                    "a-Install-Java-JDK": {
                        "command": "powershell.exe -ExecutionPolicy RemoteSigned -Command c:\\cfn\\scripts\\Install-Java-JDK.ps1",
                        "waitAfterCompletion": "30"
                    },
                    "b-Install-Tomcat8": {
                        "command": "powershell.exe -ExecutionPolicy RemoteSigned -Command c:\\cfn\\scripts\\Install-Tomcat8.ps1",
                        "waitAfterCompletion": "30"
                    },
                    "c-Set-Java-Tomcat8-Paths-Homes": {
                        "command": "powershell.exe -ExecutionPolicy RemoteSigned -Command c:\\cfn\\scripts\\Set-Java-Tomcat8-Paths-Homes.ps1",
                        "waitAfterCompletion": "forever"
                    },
                    "d-Install-Tomcat8-Service": {
                        "command": "powershell.exe -ExecutionPolicy RemoteSigned -Command c:\\cfn\\scripts\\Install-Tomcat8-Service.ps1",
                        "waitAfterCompletion": "30"
                    }
                }
            }
        }
    }

Super old... but anywho!超级旧...但任何人!

cfn-init is never called.从不调用 cfn-init。 Typically this will be done once via UserData通常这将通过 UserData 完成一次

      UserData:
        Fn::Base64:
            !Sub |
          <PowerShell>
            cfn-init.exe -v -s ${AWS::StackName} -r <resourceref> --configsets MyConfigSet --region ${AWS::Region}
          </PowerShell>

Then within your init you'd also configure the cfn-hup service to monitor a couple configuration files.然后在您的 init 中,您还可以配置 cfn-hup 服务来监视几个配置文件。

      AWS::CloudFormation::Init:
        configSets:
          MyConfigSet:
            - configureCfn
        configureCfn:
          files:
            c:\\cfn\\cfn-hup.conf:
              content: !Sub |
                [main]
                stack=${AWS::StackName}
                region=${AWS::Region}
            c:\\cfn\\hooks.d\\cfn-auto-reloader.conf:
              content: !Sub |
                [cfn-auto-reloader-hook]
                triggers=post.update
                path=Resources.UiPathRobot.Metadata.AWS::CloudFormation::Init
                action=cfn-init.exe -v -s ${AWS::StackName} -r <resourceref> --configsets MyConfigSet --region ${AWS::Region}
          services:
            windows:
              cfn-hup:
                enabled: true
                ensureRunning: true
                files:
                  - c:\cfn\cfn-hup.conf
                  - c:\cfn\hook.d\cfn-auto-reloader.conf

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-windows-stacks-bootstrapping.html https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-windows-stacks-bootstrapping.html

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

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