hashicorp / packer

Packer is a tool for creating identical machine images for multiple platforms from a single source configuration.
http://www.packer.io
Other
15.04k stars 3.32k forks source link

Packer poweshell provider ignoring lines! #10784

Open adamdaniel2993 opened 3 years ago

adamdaniel2993 commented 3 years ago

Hi I'm trying to build a windows server 2019 AMI using packer, everything was fine but when I try to install AWS-Codedeploy this line " Read-S3Object -BucketName aws-codedeploy-us-east-1 -Key latest/codedeploy-agent.msi -File c:\temp\codedeploy-agent.msi" is being ignored by packer...

This is my pkr.hcl

variable "ami_name" {
  type    = string
  default = "nm-base"
}

locals { timestamp = regex_replace(timestamp(), "[- TZ:]", "") }

source "amazon-ebs" "nm_base_image" {
  access_key    = "my-key"
  ami_name      = "NM-BASE ${local.timestamp}"
  associate_public_ip_address = true
  communicator = "winrm"
  winrm_username = "Administrator"
  winrm_port     = 5986
  winrm_use_ssl  = true
  winrm_insecure = true
  user_data_file = "./win_bootstrap.txt"
  region        = "us-east-1"
  instance_type = "t2.medium"
  secret_key    = "secret-key"
  token = "Token"
  source_ami_filter {
    filters = {
      name                = "Windows_Server-2019-English-Full-Base-*"
      root-device-type    = "ebs"
      virtualization-type = "hvm"
    }
    most_recent = true
    owners      = ["801119661308"]
  }
   tags = {
    build      = "packer"
    build_date = "{{timestamp}}"
    OS_Version = "Windows 2019"
    Base_AMI_Name = "NM_BASE_IMAGE"
}

}
build {
  sources = ["source.amazon-ebs.nm_base_image"]

 provisioner "powershell" {
    inline           = [
      "Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))",
      "choco.exe install dotnet4.8 -y",
      "C:/ProgramData/Amazon/EC2-Windows/Launch/Scripts/InitializeInstance.ps1 -Schedule",
      "C:/ProgramData/Amazon/EC2-Windows/Launch/Scripts/SysprepInstance.ps1 -NoShutdown",
      "Import-Module AWSPowerShell",
      "New-Item -ItemType Directory C:/temp",
      "Set-AWSCredential -AccessKey mycredentials -SecretKey my secret-key ; Read-S3Object -BucketName aws-codedeploy-us-east-1 -Key latest/codedeploy-agent.msi -File c:\\temp\\codedeploy-agent.msi",
      "c:\\temp\\codedeploy-agent.msi /quiet /l c:\\temp\\host-agent-install-log.txt",
      "Get-Service -Name codedeployagent"

]
  }
}

** This is exactly what is happening **

After create the temp directory is not executing the Read-S3Object -BucketName aws-codedeploy-us-east-1 -Key latest/codedeploy-agent.msi -File c:\temp\codedeploy-agent.msi", command

   amazon-ebs: Parent            :
    amazon-ebs: Exists            : True
    amazon-ebs: Root              : C:\
    amazon-ebs: Extension         :
    amazon-ebs: CreationTime      : 3/18/2021 6:09:07 PM
    amazon-ebs: CreationTimeUtc   : 3/18/2021 6:09:07 PM
    amazon-ebs: LastAccessTime    : 3/18/2021 6:09:07 PM
    amazon-ebs: LastAccessTimeUtc : 3/18/2021 6:09:07 PM
    amazon-ebs: LastWriteTime     : 3/18/2021 6:09:07 PM
    amazon-ebs: LastWriteTimeUtc  : 3/18/2021 6:09:07 PM
    amazon-ebs: Attributes        : Directory
    amazon-ebs: Mode              : d-----
    amazon-ebs: BaseName          : temp
    amazon-ebs: Target            : {}
    amazon-ebs: LinkType          :
    amazon-ebs:
==> amazon-ebs: Stopping the source instance...
    amazon-ebs: Stopping instance
==> amazon-ebs: Waiting for the instance to stop...
==> amazon-ebs: Creating AMI NM-BASE-1616090619 from instance i-057da28f7bf96b2bf
    amazon-ebs: AMI: ami-06eb9797622d384ac
==> amazon-ebs: Waiting for AMI to become ready...
==> amazon-ebs: Terminating the source AWS instance...
==> amazon-ebs: Cleaning up any extra volumes...
==> amazon-ebs: No volumes to clean up, skipping
==> amazon-ebs: Deleting temporary security group...
==> amazon-ebs: Deleting temporary keypair...
nywilken commented 3 years ago

Hi there @adamdaniel2993 I formatted your description a bit so that it is easier to read. Can you include the DEBUG logs PACKER_LOG=1 packer build my.pkr.hcl so that we can get a better sense of what is happening.

adamdaniel2993 commented 3 years ago

@nywilken here are the logs, I changed Read-S3Object -BucketName aws-codedeploy-us-east-1 -Key latest/codedeploy-agent.msi -File c:\temp\codedeploy-agent.msi I'm downloading the file manually and then trying to install it but is ignoring the command that install codedeploy, C:\Users\Administrator\codedeploy-agent.msi /quiet /l C:\Users\Administrator\host-agent.txt why? I don't know Packer_log.txt