I am using Terraform to spin up an EC2-instance where I have defined some user_data. In the user_data I download AWS CLI and set up the config file and credentials file. I then want to grab a file from my s3 bucket. This is what I've written so far:
data "template_file" "scripts" {
template = <<EOF
<powershell>
$dlurl = "installpathtoawscli.msi"
$installerPath = Join-Path $env:TEMP (Split-Path $dlurl -Leaf)
Invoke-WebRequest $dlurl -OutFile $installerPath
Start-Process -FilePath msiexec -Args "/i $installerPath /passive" -Verb RunAs -Wait
mkdir C:\temp
New-Item -Name 'b.bat' -Path 'C:\temp' -Value 'aws s3 cp s3://mybucketname/myfile.extension C:\temp\extension > C:\temp\test.txt'
New-Item -Name 'a.bat' -Path 'C:\Users\Administrator' -Value 'mkdir C:\Users\Administrator\.aws
echo [default]>> C:\Users\Administrator\.aws\credentials
echo aws_access_key_id = MY_ACCESS_KEY>> C:\Users\Administrator\.aws\credentials
echo aws_secret_access_key = MY_SECRET_KEY>> C:\Users\Administrator\.aws\credentials
echo [default]>> C:\Users\Administrator\.aws\config
echo region = MY_REGION>> C:\Users\Administrator\.aws\config
echo output = MY_OUTPUT>> C:\Users\Administrator\.aws\config'
C:\Users\Administrator\a.bat
C:\temp\b.bat
</powershell>
EOF
}
b.bat is executed and a txt file is created, however, it is empty and no file is grabbed from the s3 bucket. If I try to execute it manually (by RDPing to the instance) it grabs the file. Any ideas why this is happening?
I found the solution after several days of trying.
The AWS.exe location was not being registered in the PATH environment yet. Restarting the command prompt would do the trick, however, Terraform can't do this, thus explaining why it worked when I did it manually.
I had to provide the full path for AWS.exe in order to make it work e.g. C:\Program Files\Amazon\AWSCLI\bin\aws.exe or C:\Program Files\Amazon\AWSCLIV2\aws.exe depending on the version of AWS CLI.
Related
I want to use plastic SCM in Docker windows containers.
I searched in Plastic and found Linux containers docker file
SO I want to try my own docker file for that I need to know the command to download the SCM installation file using PowerShell and install it without the GUI (arguments to pass for installation) via Powershell
This is the script I'm currently using for my windows image. I will warn you that it does take ~5-10 minutes to install during the build process, but everything works great besides that. It works pretty simply, it creates a temp folder and uses the URL for the download to download the installer there, then runs the installer, and finally deletes the temp folder.
#This installs plastic
$tempFolder = "C:\Temp"
$plasticURL = "https://www.plasticscm.com/download/downloadinstaller/10.0.16.5882/plasticscm/windows/client"
$installerName = "plasticinstalling.exe"
New-Item $tempFolder -ItemType Directory -Force -ErrorAction Stop | Out-Null
$installerLocation = (Join-Path -Path $tempFolder -ChildPath $installerName -ErrorAction Stop)
Invoke-WebRequest -UseBasicParsing -Uri $plasticURL -OutFile $installerLocation -ErrorAction Stop
Start-Process -FilePath $InstallerLocation -ArgumentList "--mode","unattended" -NoNewWindow -Wait -PassThru
Remove-Item -Recurse $tempFolder -Force -ErrorAction Ignore
Then, in my docker file I just call the script:
RUN powershell -Command C:\Scripts\installPlastic.ps1
Hope this helped and feel free to reach out with more questions.
I am trying to create a folder that has '.' in the name using following command in Powershell without success:
New-Item -ItemType Directory -Force -Path "MyDocuments.pictures"
It silently fails, without throwing any exception or error.
I also have administrator privileges when running this command.
Path is not the Folder itself but the Folder in which you want to create your folder. This works for me:
$DirName = "MyDocuments.pictures"
New-Item -ItemType Directory -Path "C:\temp" -Name $DirName | Out-Null
Get-Item "C:\temp\$DirName"
I have a testcase where I am running an instance of Ruby inside a windows docker container. I should stress the ruby script works fine outside of docker. When run inside the docker container the script also works fine, except when the target of the Ruby file's access is on a mounted volume. In which case I get an error. I can for the testcase of course work around this issue by copying the file, but that would cause complications for the real script that found this problem.
Here's an example transcript, cbh_test is the mounted volume
PS C:\> echo ""> bob
PS C:\> cp cbh_test\failer.rb .
PS C:\> C:\Ruby25-x64\bin\ruby.exe .\failer.rb
PS C:\> cd cbh_test
PS C:\cbh_test> echo ""> bob
PS C:\cbh_test> C:\Ruby25-x64\bin\ruby.exe .\failer.rb
Traceback (most recent call last):
./failer.rb: Invalid argument # rb_readlink - C:/cbh_test (Errno::EINVAL)
Docker instance invoked by:
docker run -it -v c:\Users\me\work\fred:c:\cbh_test bob/failer powershell
Dockerfile:
# escape=`
FROM microsoft/windowsservercore:ltsc2016
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
COPY install_ruby.ps1 c:
RUN powershell c:\install_ruby.ps1
failer.rb:
def main(args)
fragment = "bob"
$tmp = File.open(File.join(File.dirname(__FILE__), fragment), 'r+').read
end
main($ARGV)
install_ruby.ps:
$RUBY_VERSION = "2.5.3-1"
$RUBY_RELEASE = "2.5.3-1"
$url = ('https://github.com/oneclick/rubyinstaller2/releases/download/rubyinstaller-{0}/rubyinstaller-{1}-x64.exe'-f $RUBY_VERSION, $RUBY_RELEASE);
$exe = "ruby-install.exe"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;
Invoke-WebRequest -Uri $url -OutFile $exe;
$args = "/silent /tasks='assocfiles,modpath'"
Start-Process -FilePath $exe -ArgumentList $args -PassThru -Wait
I tried for several days and the only solution I was able to find to this problem was to use ltsc2019.
I'm using Docker for Windows and building the docker image with a Dockerfile like this:
FROM mydockerhublogin/win2k16-ruby:1.0
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
RUN powershell -Command \
$ErrorActionPreference = 'Stop'; \
New-Item "HKLM:\Software\WOW6432Node\ExampleCom" -Force ; \
New-ItemProperty "HKLM:\Software\WOW6432Node\ExampleCom" -Name MenuLastUpdate -Value "test" -Force
RUN powershell -Command \
$ErrorActionPreference = 'Stop'; \
New-Item "HKLM:\Software\ExampleCom" -Force ; \
New-ItemProperty "HKLM:\Software\ExampleCom" -Name MenuLastUpdate -Value "test" -Force
# Run ruby script when the container launches
CMD ["C:/Ruby23-x64/bin/ruby.exe", "docker_ruby_test.rb"]
Note that I am adding some registry entries to the Windows registry which the code inside the container will access. While this method of adding registry entries is fine for a few entries, my requirement is to add dozens of entries required for my windows application. Is there a way to do this in a more concise manner?
Try creating a file for the your registry entry and copy that inside the container.
Then try running Invoke-Command -ScriptBlock {regedit /i /s C:\shared\settings.reg}
The following is the only way I could getting working using 4.8-windowsservercore-ltsc2019
COPY Registry/ChromeConfiguration.reg .
RUN reg import ChromeConfiguration.reg
I have seen examples like this for creating and running a script via ebextensions config:
files:
"C:\\scripts\\whoami.ps1":
content: |
date | out-file -append c:\scripts\whoami.log
whoami | out-file -append c:\scripts\whoami.log
hostname | out-file -append c:\scripts\whoami.log
get-module | out-file -append c:\scripts\whoami.log
commands:
whoami:
command: powershell.exe -ExecutionPolicy Bypass -Command "C:\\scripts\\whoami.ps1"
ignoreErrors: false
waitAfterCompletion: 5
That's fine, but can I just include a script in ebextensions and refer to it instead of having to define its content in a config?
for example I would have
myapp
--.ebextensions
----myconfig.config
----myscript.ps1
And then "myconfig.config" would copy "myscript.ps1" somewhere and run it.
How can I do this?
Edit:
So I put this command in a config to see what the current path is:
commands:
whereami:
command: dir > c:\whereami.log
ignoreErrors: false
waitAfterCompletion: 5
Its c:\windows\system32 which makes sense because it runs in the context of SYSTEM.
Is there no self referential method/keyword I can use to point to a script I have located in the beanstalk project I upload?
The options that worked for me (thanks to littleforest).
Create a folder structure inside of the web deployment package and refer to it with container_commands:
package/myfolder/myscript.ps1
container_commands:
relativeprogrampath:
command: "myfolder/myscript.ps1"
Create it in a parent folder and refer to it:
example folder structure:
parentfolder/myfolder/myscript.ps1
parentfolder/package/<the web deployment package>
container_commands:
relativeprogrampath:
command: "..\myscript.ps1"
If you place the myscript.ps1 file in the .ebextensions folder next to the config files, you can run it from a container command like this:
container_commands:
00-myscript:
command: powershell.exe -ExecutionPolicy Bypass -Command ".\\.ebextensions\\myscript.ps1"
waitAfterCompletion: 0