Solved

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

Posted on 2016-09-22
12
78 Views
Last Modified: 2016-09-22
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
Comment
Question by:Tessando
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 4
12 Comments
 
LVL 12

Expert Comment

by:Gary Dewrell
ID: 41811294
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
 
LVL 12

Expert Comment

by:Gary Dewrell
ID: 41811295
Chang txt to .pdf
0
 
LVL 24

Expert Comment

by:NVIT
ID: 41811296
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
Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

 

Author Comment

by:Tessando
ID: 41811332
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
 
LVL 12

Expert Comment

by:Gary Dewrell
ID: 41811336
Did you try the one I provided?

 --name _%date:~-4,4%%date:~-7,2%%date:~-10,2%_openvpn.pdf
0
 
LVL 24

Accepted Solution

by:
NVIT earned 500 total points
ID: 41811337
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
 
LVL 12

Expert Comment

by:Gary Dewrell
ID: 41811357
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
 

Author Comment

by:Tessando
ID: 41811569
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
 
LVL 24

Expert Comment

by:NVIT
ID: 41811598
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
 

Author Comment

by:Tessando
ID: 41811622
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
 

Author Closing Comment

by:Tessando
ID: 41811624
Great solution. Worked perfectly in combination with the existing script I used. Thanks!
0
 
LVL 24

Expert Comment

by:NVIT
ID: 41811651
> 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

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many software programs on offer that will claim to magically speed up your computer. The best advice I can give you is to avoid them like the plague, because they will often cause far more problems than they solve. Try some of these "do it…
Configuring Remote Assistance for use with SCCM
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question