Execute shell scrip from Git in specific EC2 instance - amazon-web-services

I have a shell script. Per rule in my organization we should save all the code in GitHub and use it from there. How can I run this script in a specific set of EC2 instances from GitHub using Jenkins?

You may consider the use of AWS Systems Manger to run shell commands on your EC2 instance.
https://docs.aws.amazon.com/systems-manager/latest/userguide/execute-remote-commands.html
You can find a sample use case here: https://ujjwalbhardwaj.me/post/trigger-actions-inside-ec2-instance-on-cloudwatch-events-using-aws-systems

Related

How to run a command in CMD that will run on all AWS ec2 instances I have created?

I was wondering, is it possible and how to run a command from local PC which will then be ran on every ec2 instance I have created?
You can run common administrative tasks on multiple EC2 instances using AWS Systems Manager Run Command. Note there are some pre-requisites for using this.

Is it possible to run a command from one ec2 instance that executes that command onto another ec2 instance?

Right now I am testing to see if I am able to write
touch test.txt
simply to another ec2 instance.
I have looked into both ssh and ssm but I do not understand where to begin the code. Any ideas to remotely send commands?
If you want to send a command remotely you can make use of the AWS run command functionality of SSM.
To do this you will need to ensure that you’re both running SSM agent and have a valid IAM role setup on the remote instance. The getting started section should help that.
Finally you can call the remote instance using the send-command function. Either create your own document or use the existing ‘AWS-RunShellScript’ document.

Powershell Automation in ec2

We are trying to automate running of multiple powershell scripts in AWS EC2 instance. Any suggestion how this can be done. These execution has to be sequential.
In order to configure state on a fresh EC2 instance, you can use 'User Data' in order to:
a. Fully provision and configure state using powershell, see:
http://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2-instance-metadata.html#instancedata-add-user-data
b. Install an agent for a configuration management tool such as Puppet, Chef, Ansible (as mentioned in the comment above). An example of provisioning stock Window's AMI's can be followed here: http://blog.rolpdog.com/2015/09/manage-stock-windows-amis-with-ansible.html
If you wish to trigger scripts on an existing Amazon EC2 instance (or a whole fleet of instances), also consider Running PowerShell Commands or Scripts with Amazon EC2 Run Command.

Finding the services running on multiple aws ec2 instances

Is there a command in the AWS cli to get a list of services running on my ec2 instances?
Normally I log into each ec2 instance individually, and check using Linux commands such as "netstat" or "ps -eaf".
There are around 400 instances, so if I do this manually it takes quite a lot of time. If any AWS commands are there to find the services running without actually logging into the instance, it would be great.
Thanks in advance.
The AWS Command Line Interface is a tool to manage your AWS services. You cannot use AWS-CLI to manage OS-level service. With AWS-CLI, you can control multiple AWS services from the command line and automate them through scripts.
Hope it helps :-)
You might have some luck with EC2 Run Command. From their docs:
Run Command provides a simple way of automating common administrative tasks
like executing Shell scripts and commands on Linux [...]
Run Command allows you to execute these commands across multiple
instances and provides visibility into the results

Automating Git pull process on a ec2 ubuntu instances

I am running a couple of ubuntu ec2 instances, I want to run an automation script which will pull the code from Github whenever a new instance is booted from the AMI. The thing is presently I am sshing to the server and run the command git pull origin master and it will ask for password key.
How do I automate this process? So after booting the new instance from a AMI this script should:
Run
Pull the code and also the submodule
Create couple of files and configure it
Please help me to achieve it.
Thanks
This will probably take some time and configuring, but this might set you on the right path.
First, setup your ssh keys, so that you can automatically pull from a repo, without a password. Outlined here: https://help.github.com/articles/generating-ssh-keys
Next, create a startup script to issue the 'pull' command from Github. Here: https://help.ubuntu.com/community/UbuntuBootupHowto
Then save your AMI, When you start a new EC2 AMI, the script should run, pulling in your Github changes.
Also to note, make sure gits remote path is using SSH, if it is HTTPS, it will ALWAYS ask for a password.
Your best best would be to utilize the fact the Ubuntu utilizes CloudInit within its canonical image.
Using CloudInit, you can pass scripts (i.e. shell scripts) to execute at various start up stages as EC2 user-data.
It would be very easy for your to make your GIT command line sequence execute from such a script. He is link to documentation, which includes examples.
https://help.ubuntu.com/community/CloudInit
Create a user-password access to your ubuntu instance. Replicate this particular instance if you need multiple. Now you are free of the key access. If you need to automate a process in that instance cron it or send the script via ssh to that instance and let the cron to find and run it.