Solved

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

Posted on 2016-09-22
12
58 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Exchange server is not supported in any cloud-hosted platform (other than Azure with Azure Premium Storage).
Learn how to PXE Boot both BIOS & UEFI machines with DHCP Policies and Custom Vendor Classes
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

733 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