Solved

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

Posted on 2016-09-22
12
70 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Configuring Remote Assistance for use with SCCM
Windows 10 Creator Update has just been released and I have it working very well on my laptop. Read below for issues, fixes and ideas.
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

729 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