Link to home
Start Free TrialLog in
Avatar of Tessando
TessandoFlag for United States of America

asked on

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.
Avatar of Gary Dewrell
Gary Dewrell
Flag of United States of America image

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
Chang txt to .pdf
Avatar of NVIT
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

Avatar of Tessando

ASKER

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.
Did you try the one I provided?

 --name _%date:~-4,4%%date:~-7,2%%date:~-10,2%_openvpn.pdf
ASKER CERTIFIED SOLUTION
Avatar of NVIT
NVIT
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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.
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
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.
Great solution. Worked perfectly in combination with the existing script I used. Thanks!
> 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...