1

I have a Bamboo task that invokes a batch file on a remote EC2. That batch file executes a test, then calls a PowerShell script to parse the test.
Finally I grab the instance Id of our bamboo server and send the results of the test back via ssm.

My problem exists in the PowerShell script when I attempt to send my results. I use "describe-instances" to retrieve the Bamboo Server ID:

Set-AWSCredential -StoreAs default -AccessKey <key> -SecretKey <key>

$instanceid = aws ec2 describe-instances --profile default --region "us-east-1" --filters 'Name=tag:Name,Values=Bamboo Server' --query 'Reservations[*].Instances[*].InstanceId' --output text

Send-SSMCommand -InstanceId $instanceId -DocumentName AWS-RunPowerShellScript -Parameter -Comment

*NOTE: my Bamboo Server resides on a different EC2 than my tests.



So, if I invoke the batch file from my Bamboo Build Plan. The "describe-instances" command does not run correctly.
But, if I run the batch file locally on that remote EC2, the process runs smoothly.

I've tried:
- multiple variations of the "describe-instances" including the Get-EC2Instance and others.
- I believe my permissions are being set correctly. But maybe the "describe-instances" does not catch it. I have tried many variations of setting the creds including aws configure, manually loading from environment variables and from a file.
- I put Start-Sleeps in to ensure I am not having async issues.

It is like the command refuses to run when I invoke it from the Bamboo Task.

I noticed that the tests run in the foreground locally, and in the background when invoked from Bamboo.

Could my problem be a security issue? Like running from Bamboo doesn't allow the commands to run the same? I cannot find any documentation explaining that from AWS or Bamboo.

Any ideas?

demokritos
  • 1,416
  • 2
  • 12
  • 34
J_sdev
  • 341
  • 1
  • 13
  • What's the error you're getting when running from the Bamboo task? – Viccari Jan 29 '18 at 20:25
  • No error. The bamboo task runs the first send-command from the inline Script with no problem. Nor am I seeing any errors when doing a try/catch in my powershell script on my remote ec2. – J_sdev Jan 29 '18 at 20:32
  • Can you please try: ```Get-EC2Instance -Region us-east-1 -Filter @{Name="tag:Name";Values="Bamboo Server"} ``` – demokritos Jan 30 '18 at 10:36

1 Answers1

0

Oh man...

LESSON: Do not mix and match AWS CLI and AWSPowerShell. You get weird results.

First:
I am not 100% sure, but I believe that the credentials are passed differently between the commands. It seems that setting the creds per session with Set-AWSCredential would work. But it does not. So you'll have to stick with AWSPowerShell commands.

Second:
Speaking of AWSPowerShell commands. They can be tricky. I had to come up with a really funky command to capture the Instance Id of the Bamboo Server. I pieced this together from other threads and posts found around the intra-net

$instance = (Get-EC2Instance -Filter @{Name="tag:Name";Value="Bamboo Server" }).Instances | Select-Object InstanceId

Then grab the actual Id by using $instance.InstanceId

EDIT: @Curios - I rejected your edits because: depending on your naming conventions, $instance is perfectly acceptable, but you are more than welcome to name it $Instances in your script. And, you are right, Bamboo Server does refer to all instances with the name "Bamboo Server". The reason I chose to use the tab:Name/Value combination is that our architecture will only have one Bamboo Server, but updates and such could change the instance ID, therefore I need to allow for such changes by grabbing our "Bamboo Server" regardless of it's instance ID. Others my require a unique setup for the commands dependent on their needs.

J_sdev
  • 341
  • 1
  • 13