Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 102
  • Last Modified:

Appending Date in CMD File when Creating an Image using AWS CLI

I'd like to add a date to the name of the AMI's that I am generating using a batch file (.cmd file) and the AWS CLI.

I need to do this because I get the error "when calling the CreateImage operation: AMI name openvpn is already in use" when I run the script.

I figured a way to do this would be to append a date to the name.

Here's the line of code that I'm using:

aws ec2 create-image --instance-id i-0*****b2f915***** --name "openvpn" --description "auto-openvpn" --no-reboot

Open in new window


And here is where I'd like to have the date inserted:

aws ec2 create-image --instance-id i-0*****b2f915***** --name "DATE_openvpn" --description "auto-openvpn" --no-reboot

Open in new window


Is this possible to do with the AWS CLI?

Thank you for your help.
0
Tessando
Asked:
Tessando
  • 4
  • 4
  • 4
1 Solution
 
Gary DewrellSenior Network AdministratorCommented:
I have not played with AWS CLI but if it allows environment variables then this should work.
For name use:

_%date:~-4,4%%date:~-7,2%%date:~-10,2%_openvpn.txt
0
 
Gary DewrellSenior Network AdministratorCommented:
Chang txt to .pdf
0
 
NVITCommented:
REM The next line sets the following DateTime variables: DT_Day, DT_DayOfWeek, DT_Hour, DT_Minute, DT_Month, DT_Quarter, DT_Second, DT_WeekInMonth, DT_Year
for /f "delims=" %%a in ('wmic.exe Path Win32_LocalTime GET * /value') do (for /f "delims=" %%b in ("%%a") do set DT_%%b)
for %%a in (DT_Month DT_Day DT_Hour DT_Minute DT_Second) do (if !%%a! LSS 10 set %%a=0!%%a!)

set Timestamp=%DT_Year%%DT_Month%%DT_Day%_%DT_Hour%%DT_Minute%

aws ec2 create-image --instance-id i-0*****b2f915***** --name "%Timestamp%_openvpn" --description "auto-openvpn" --no-reboot

Open in new window

0
 The Evil-ution of Network Security Threats

What are the hacks that forever changed the security industry? To answer that question, we created an exciting new eBook that takes you on a trip through hacking history. It explores the top hacks from the 80s to 2010s, why they mattered, and how the security industry responded.

 
TessandoIT AdministratorAuthor Commented:
Thanks Guys - When it comes to Nvit's addition to the script, it looks like Windows is able to pull the year/month/etc but the AWS CLI doesn't like it.

Here's the error I'm getting now:

An error occurred (InvalidAMIName.Malformed) when calling the CreateImage operation: AMI names must be between 3 and 128 characters long, and may contain letters, numbers, '(', ')', '.', '-', '/' and '_'

Is there any other way to call that time/date variable other than "%Timestamp%", which seems to be the characters that the AWS CLI doesn't like?

Thank you again for your assistance.
0
 
Gary DewrellSenior Network AdministratorCommented:
Did you try the one I provided?

 --name _%date:~-4,4%%date:~-7,2%%date:~-10,2%_openvpn.pdf
0
 
NVITCommented:
Try this:

@echo off
setlocal enabledelayedexpansion

REM The next line sets the following DateTime variables: DT_Day, DT_DayOfWeek, DT_Hour, DT_Minute, DT_Month, DT_Quarter, DT_Second, DT_WeekInMonth, DT_Year
for /f "delims=" %%a in ('wmic.exe Path Win32_LocalTime GET * /value') do (for /f "delims=" %%b in ("%%a") do set DT_%%b)
for %%a in (DT_Month DT_Day DT_Hour DT_Minute DT_Second) do (if !%%a! LSS 10 set %%a=0!%%a!)

set Timestamp=%DT_Year%%DT_Month%%DT_Day%_%DT_Hour%%DT_Minute%

aws ec2 create-image --instance-id i-0*****b2f915***** --name "%Timestamp%_openvpn" --description "auto-openvpn" --no-reboot

Open in new window

0
 
Gary DewrellSenior Network AdministratorCommented:
You could use a batch file to create a new batch file with the proper command line and then call it. Something like this:
 
Create a batch name startaws.bat  (or anything you want). Inside the batch should be these two lines.

echo    aws ec2 create-image --instance-id i-0*****b2f915***** --name _%date:~-4,4%%date:~-7,2%%date:~-10,2%_openvpn.pdf  --description "auto-openvpn" --no-reboot  > awsbatch.bat
awsbatch.bat

When you run the startaws.bat it will create a new batch file named awsbatch.bat with the proper command line, parsing the environment variables.

Then it will run the new batch file it created.
0
 
TessandoIT AdministratorAuthor Commented:
This worked! Thank you, NVIT. Now I need to figure out how to set this up as a Scheduled Task and I'm good to go. Is there anything in that batch file that would prevent running it programmatically? This works perfectly as expected in the User Interface. Thanks again for your fast and great suggestions.
0
 
NVITCommented:
It should work fine. It seems similar to your question regarding running it in Scheduled Task here: https://www.experts-exchange.com/questions/28963507/CMD-File-Runs-Fine-By-Itself-But-Not-As-Scheduled-Task.html
0
 
TessandoIT AdministratorAuthor Commented:
Yep, you got it, Nvit. As it turns out, this was "worse" than user error. Someone had terminated the instance I was trying to make an AMI for - which explains why it didn't work. I was able to successfully run this from a Scheduled Task. Thanks again to everyone who helped me get this one quickly resolved! I appreciate the help.
0
 
TessandoIT AdministratorAuthor Commented:
Great solution. Worked perfectly in combination with the existing script I used. Thanks!
0
 
NVITCommented:
> Someone had terminated the instance I was trying to make an AMI for - which explains why it didn't work.
Ouch!

Glad to help, Tessando. Have a good day/night...
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

  • 4
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now