Gitbash error, during attach EBS volume to EC2 - amazon-ec2

My Jenkins Master working on Windows Server and want to run a shell script with GitBash.
I want to attach the EBS volume to EC2.
When I run "awscli" command in GitBash, I am getting a weird invalid parameter.
$ aws ec2 attach-volume --device /dev/sdf --volume-id vol-0d5736e1b8be30e92 --instance-id i-036540a7e7bbf712b
An error occurred (InvalidParameterValue) when calling the AttachVolume operation: Value (C:/Program Files/Git/dev/sdf) for parameter device is invalid. C:/Program Files/Git/dev/sdf is not a valid EBS device name.

Finally, I found the problem.
aws ec2 attach-volume --volume-id vol-00817f61e233134fb --instance-id i-036540a7e7bbf712b --device '//dev\sdf'
response
{
"AttachTime": "2021-02-03T10:08:39.661000+00:00",
"Device": "/dev/sdf",
"InstanceId": "i-036540a7e7bbf712b",
"State": "attaching",
"VolumeId": "vol-00817f61e233134fb"
}

Related

How to run command on EC2 instance after starting session manager on it using bash script?

I am writing bash script to install missing patches on ec2 instance using session manager.
I can start the session using script but I am not sure how can I run command on it using script?
instanceid = "i-098xxxx"
echo $instanceid
echo "instance id"
# Creating AMI
echo "-------------------------------Creation AMI-----------------------------------------"
aws ec2 create-image --instance-id $instanceid --name "test ami" --description "Delete when
testing" --no-reboot --profile xyz
# Start Session Manager
aws ssm start-session --target $instanceid --profile xyz
# Command to check for critical security patches and update/install it on ec2 instance
echo "-------------------------------installing missing patches-----------------------------
------------"
sudo yum update-minimal --sec-severity=critical,important --bugfix
I want to run sudo yum update-minimal --sec-severity=critical,important --bugfix command in ec2 instance using script.
Can someone guide me on this?
As an alternative to using run-command, if for some reason you need to use a session, you can do this by using the AWS-StartNonInteractiveCommand document, for example:
aws ssm start-session \
--document-name 'AWS-StartNonInteractiveCommand' \
--parameters '{"command": ["sudo yum -y update amazon-ssm-agent"]}' \
--target "$instanceid"
To see the other parameters accepted by this document, run
aws ssm describe-document --name AWS-StartNonInteractiveCommand
The AWS Systems Manager Session Manager provides an SSH-like connection via a web browser.
However, if you wish to automate the execution of commands on instances, you should use the AWS Systems Manager Run Command, which can run commands on a single instance or hundreds of instances, and return results from each run.
I think AWS Systems Manager is best way to resolve this case.
https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager.html

How to Start or Stop an existing EC2 instance from a Jenkins Job

I have a Jenkins Job to start and stop AWS EC2 instances.
Probably the profile is misconfigured and I'm stuck at this :
botocore.exceptions.ProfileNotFound: The config profile xxxx could not be found
Using this command through Execute Shell:
aws ec2 stop-instances --region $AWS_DEFAULT_REGION --profile $AWS_PROFILE --instance-ids $INSTANCE
Any suggestions will be appreciated to modify the job or resolve this error.
Please check the profile name you passed in the command. It should be configured in your credentials file, usually found at this location ~/.aws/credentials
You can follow this guide (https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html) to set up profile.
Best of luck

ECS CLI key pair error when calling up

I am trying to use Amazon's ECS cli to create a cluster. I keep getting the error:
reason="The key pair 'my-key-pair' does not exist" resourceType="AWS::AutoScaling::LaunchConfiguration"
I have also run:
ecs-cli configure profile --profile-name grantspilsbury --access-key foo --secret-key bar
ecs-cli configure --cluster cluster_test --region us-east-1 --config-name myclusterconfig
I have added my-key-pair to ECS and to EC2.
The full log is:
~ $ ecs-cli up --keypair my-key-pair --capability-iam --size 2 --instance-type t2.small --force
INFO[0002] Created cluster cluster=default region=us-east-1
INFO[0003] Waiting for your CloudFormation stack resources to be deleted...
INFO[0003] Cloudformation stack status stackStatus="DELETE_IN_PROGRESS"
INFO[0038] Waiting for your cluster resources to be created...
INFO[0038] Cloudformation stack status stackStatus="CREATE_IN_PROGRESS"
INFO[0101] Cloudformation stack status stackStatus="CREATE_IN_PROGRESS"
INFO[0164] Cloudformation stack status stackStatus="CREATE_IN_PROGRESS"
ERRO[0197] Failure event reason="The key pair 'my-key-pair' does not exist" resourceType="AWS::AutoScaling::LaunchConfiguration"
FATA[0197] Error executing 'up': Cloudformation failure waiting for 'CREATE_COMPLETE'. State is 'ROLLBACK_IN_PROGRESS'
I ran into the same issue. My issue was I was giving it the full path to the pem file rather than the name of the key on EC2 (link to Ohio region). Turning
ecs-cli up --keypair /home/me/keyPair.pem --capability-iam --size 2 --instance-type t2.medium --cluster-config ec2-tutorial --force
into
ecs-cli up --keypair keyPair --capability-iam --size 2 --instance-type t2.medium --cluster-config ec2-tutorial --force
works as long as there is a key pair on EC2 named keyPair
My problem was that I was passing the filename (keypair.pem) instead of the name of the keypair on AWS. Make sure you pass the keypair name as you see on AWS and not the name of the file.
It's possible that your key is in a different region from the one where you're attempting to create the image. Jeff's answer gave me the clue that my keypair was in the default instance (Ohio), but I was creating my instance in my local region.
I was following this tutorial until I found the same issue:
https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-cli-tutorial-ec2.html
It had nothing to do with the region, the problem was my Key-Pair wasn't uploaded to the ECS. Maybe it is obvious, but if you are AWS beginner like me, the tutorial instructions are not clear about this.
Previous step: Create Key-Pair
If you haven't already done it, first you have to create the Key-Pair that is stored on a ".pem" file. Following this instructions with the AWS Console, was quite easy: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html
Solution:
The ".pem" file has to be uploaded. Can be done using the AWS console, following the menu:
EC2 > Network & Security > Key Pairs > "Create Key Pair"
Menu URL: https://eu-central-1.console.aws.amazon.com/ec2/v2/home?region=eu-central-1#CreateKeyPair:
(Be careful about the 2 regions defined on this URL)
As it is described on:
https://www.cloudbooklet.com/how-to-add-new-user-with-key-pair-in-aws-ec2/
That means that keypair name does not exist in your EC2 account.
Simply create it with s per the aws guide

Windows instances on spot

Here is my json file
# cat s6.json
{
"ImageId": "ami-0b33d91d",
"InstanceType": "i2.xlarge",
"KeyName": "xxx"
}
And I can use this command...
# aws ec2 request-spot-instances --spot-price "1.03" --instance-count 1 --type "one-time" --launch-specification file://s6.json
The above command works as expected. But if I change the Image ID to windows ami-ab33d3bd I get this error...
An error occurred (InvalidInput) when calling the RequestSpotInstances
operation: Unsupported product.
I can however request a regular on-demand instance without any problem. So this command works...
# aws ec2 run-instances --image-id ami-ab33d3bd --count 1 --instance-type i2.xlarge --key-name xxx
Does it mean that Windows instances are not available on spot?
From EC2-Spot FAQs:
Q. Which operating systems are available as Spot instances?
Linux/UNIX and Windows Server are available. Windows Server with SQL
Server is not currently available.
The AMI ami-ab33d3bd is a Windows Server 2008 with SQL Enterprise which is not supported for Spot.

Is it OK to fsfreeze --unfreeze directly after create-snapshot returns?

Assume /data is the mount point for an EXT4 file system that occupies a whole EBS volume.
To create a backup, the file system is freezed and a snapshot is created.
root#ubuntu:~# fsfreeze --freeze /data
root#ubuntu:~# aws ec2 create-snapshot --volume-id vol-1234567 --description "/data snapshot"
{
"SnapshotId": "snap-27c5ebdf",
"Description": "/data snapshot",
"VolumeId": "vol-1234567",
"State": "pending",
"VolumeSize": 250,
"OwnerId": "744275636941",
"StartTime": "2014-11-04T15:17:19.000Z",
"Tags": []
}
root#ubuntu:~#
Is it at this point OK to run
root#ubuntu:~# fsfreeze --unfreeze /data
and start writing files under /data?
In other words, is it OK to start using an EBS volume directly after aws ec2 create-snapshot returns, although the state of the snapshot might be pending?
Yes, it is OK.
Here is a quote from
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-creating-snapshot.html
You can remount and use your volume while the snapshot status is pending.

Resources