AWS Windows Server 2012R2 AMI + UserData运行

时间:2017-10-05 20:38:11

标签: powershell amazon-web-services packer

我的打包程序脚本如下:

{
    "description": "APLP GOLD IMAGE {{isotime \"20060102\"}}",
    "min_packer_version": "0.11.0",
    "variables": {
        "aws_region": "us-east-2",
        "aws_vpc": "xxxxxx",
        "aws_subnet": "xxxxxx"
    },
    "builders": [
        {
            "type": "amazon-ebs",
            "profile": "nonprod",
            "ami_name": "APLP (encrypted) {{isotime \"20060102\"}}",
            "ami_description": "APLP Server2012R2 x86_64 HVM EBS (encrypted) {{isotime \"20060102\"}}",
            "instance_type": "t2.micro",
            "region": "{{user `aws_region`}}",
            "vpc_id": "{{user `aws_vpc`}}",
            "subnet_id": "{{user `aws_subnet`}}",
             "source_ami_filter": {
                "filters": {
                    "name": "Windows_Server-2012-R2_RTM-English-64Bit-Base-*",
                    "virtualization-type": "hvm"
                },
                "owners": ["801119661308"],
                "most_recent": true
            },
            "communicator": "winrm",
            "user_data_file":"./SetUpWinRM.ps1",
            "winrm_username": "Administrator",
            "winrm_use_ssl": true,
            "winrm_insecure": true,
            "ami_virtualization_type": "hvm",
            "tags": {
                "Name": "APLP Server2012R2 Gold",
                "OS": "Windows Server",
                "OSVER": "2012R2"
            },
            "encrypt_boot": true,
            "launch_block_device_mappings": [
                {
                  "device_name": "/dev/sda1",
                  "volume_size": 100,
                  "volume_type": "gp2",
                  "delete_on_termination": true
                }
            ]
        }
    ],
    "provisioners": [
        {
            "type": "powershell",
            "scripts": [
                "sysprep.ps1",
                "ec2config.ps1",
                "defrag.ps1",
                "disableuac.ps1"
            ]
        }
    ]
}

我在构建中的用户数据只配置WINRM,它已在网上显示了几个地方,效果很好。我还有几个运行的powershell provisoner任务,特别是运行:

$EC2SettingsFile = "C:\\Program Files\\Amazon\\Ec2ConfigService\\Settings\\Config.xml"
$xml = [xml](get-content $EC2SettingsFile)
$xmlElement = $xml.get_DocumentElement()
$xmlElementToModify = $xmlElement.Plugins
foreach ($element in
    $xmlElementToModify.Plugin) {
    if ($element.name -eq
        "Ec2SetPassword") {
        $element.State = "Enabled"
    }
    elseif ($element.name -eq
        "Ec2SetComputerName") {
        $element.State = "Enabled"
    }
    elseif ($element.name -eq
        "Ec2HandleUserData") {
        $element.State = "Enabled"
    }
    elseif ($element.name -eq
        "Ec2DynamicBootVolumeSize") {
        $element.State = "Enabled"
    }
}
$xml.Save($EC2SettingsFile)

创建映像后,我甚至从快照创建了一个卷,以确保Config.xml当前配置为运行userdata。我遇到的问题是userdata永远不会运行,它与我使用来自packer的AMI构建的新实例绑定。如果我使用AWS clean AMI,我的userdata运行正常,当我使用自定义AMI运行相同的用户数据时,它不会。

我的理解是,我在configservice中对config.xml进行更改的原因是userdata再运行一次。部署AMI后,我还可以在Windows内部启动ec2config UI,并在启动时单击运行userdata,一切都按预期运行。

非常感谢任何帮助。

0 个答案:

没有答案