I am trying to get the status of few EC2 instance using AWS CLI
aws ec2 describe-instance-status --instance-ids xxxxxx yyyyyyyy zzzzzzzz
But in the output It only lists the instances which are running and not which are stopped.
How can i view the instances state even they are running or stopped
There are different ways to check the instance-state.
Use:
describe-instance-status.
You can use describe-instance-status: The describe-instance-status command of the AWS Command Line Interface (CLI) is used to retrieve the status of one or more Amazon Elastic Compute Cloud (EC2) instances. By default, it returns the status of running instances. To include stopped instances in the returned status information, you can use the --include-all-instances option.
Here is an example of how you can use this option:
$ aws ec2 describe-instance-status --include-all-instances
the above command will return the status information for both running and stopped instances.
Note that you can also filter the returned results using the --filters option. For example, you can use the following command to only retrieve the status of stopped instances:
$aws ec2 describe-instance-status --filters "Name=instance-state-code,Values=80"
Note: here 80 is the code for stopped instances and 16 for running ones. Below one will give you the info about stopped instance .
$ aws ec2 describe-instance-status --include-all-instances --filters Name=instance-state-name,Values='stopped'
You can get both the running as well as stooped instance-state while including the option --include-all-instances as used in the below given example...
$ aws ec2 describe-instance-status --include-all-instances --filters Name=instance-state-name,Values='*' --query 'InstanceStatuses[*].{InstanceId: InstanceId, State: InstanceState.Name}' --profile lab--output table
------------------------------------
| DescribeInstanceStatus |
+----------------------+-----------+
| InstanceId | State |
+----------------------+-----------+
| i-0a4209dkc6549a2ea | running |
| i-09379cj420ed015f2 | running |
| i-0c9e1100de0105ed6 | stopped |
| i-0f57b147ea9124344 | running |
| i-02e4cbcbe10cb5e79 | stopped |
+----------------------+-----------+
describe-instances.
You can check the instance-state-name regardless of the state of the instance (pending | running | shutting-down | terminated | stopping | stopped ) while using with describe-instances.
$ aws ec2 describe-instances --query 'Reservations[].Instances[*].{InstanceType: InstanceType, InstanceId: InstanceId, State: State.Name}' --profile lab --output table
----------------------------------------------------
| DescribeInstances |
+----------------------+----------------+----------+
| InstanceId | InstanceType | State |
+----------------------+----------------+----------+
| i-0a4209dkc6549a2ea | t3.xlarge | running |
| i-09379cj420ed015f2 | t2.small | running |
| i-0c9e1100de0105ed6 | m5.xlarge | stopped |
| i-0f57b147ea9124344 | c6i.xlarge | running |
| i-02e4cbcbe10cb5e79 | t1.micro | stopped |
+----------------------+----------------+----------+
However, you can get a specific state of an instance using --filers and --query, for example, if you are looking for only a stopped instance then you can use like below...
$ aws ec2 describe-instances --filters Name=instance-state-name,Values=stopped --query 'Reservations[].Instances[*].{InstanceType: InstanceType, InstanceId: InstanceId, State: State.Name}' --profile lab --output table
----------------------------------------------------
| DescribeInstances |
+----------------------+----------------+----------+
| InstanceId | InstanceType | State |
+----------------------+----------------+----------+
| i-0c9e1100de0105ed6 | m5.xlarge | stopped |
| i-02e4cbcbe10cb5e79 | t1.micro | stopped |
+----------------------+----------------+----------+
In a similar fashion, you can check only the running instance …
$ aws ec2 describe-instances --filters Name=instance-state-name,Values=running --query 'Reservations[].Instances[*].{InstanceType: InstanceType, InstanceId: InstanceId, State: State.Name}' --profile lab --output table
----------------------------------------------------
| DescribeInstances |
+----------------------+----------------+----------+
| InstanceId | InstanceType | State |
+----------------------+----------------+----------+
| i-0a4209dkc6549a2ea | t3.xlarge | running |
| i-09379cj420ed015f2 | t2.small | running |
+----------------------+----------------+----------+
Note:
Further, I personally like to dictionary format along with tabular output(--output table) while using AWS CLI to get the values in nice tabular form which is easy for readability, hence the same here to get the values of the instance-state in a hash form (i.e., a dictionary or associative array), you I have combined that with the --query to get a nicer readable output.
Yes, if you pass the --include-all-instances option to the command it will also show stopped instances
From AWS documentation about the describe-instance-status command:
--include-all-instances | --no-include-all-instances (boolean)
When true , includes the health status for all instances. When false, includes the health status for running instances only.
Default: false
Related
as MIG instances in GCP keep getting scaled up and scaled down and instance gets new name every-time it scaled up and also MIG don’t have static IP. So I am trying Prometheus Pushgateway to get metrics. But after setting up everything I am getting metrics of scaled down instances also along with scaled up instance. And scaled down instance metric is in form of straight line. Now I don't want metrics of scaled down instance and also if there is any way that can automatically remove stop instances metrics only but don’t disturb running instance.
I have created .sh script which can check stopped instances and don't send metrics but it is not working. i have created crontab to check script but it is also not working.
#!/bin/bash
Get the list of active MIG instances
active_instances=$(gcloud compute instances list --filter="status=RUNNING" --format="value(name)")
PUSHGATEWAY_SERVER=http://Address (which I have mentioned in actual script)
Get the list of existing metrics from the push gateway
existing_metrics=$(curl $PUSHGATEWAY_SERVER/metrics 2>/dev/null | grep "node-exporter" | awk '{print $NF}')
Loop through each active instance and scrape its metrics using Prometheus
for instance in $active_instances; do
NODE_NAME=$instance
Check if the instance is part of a MIG and if it is still running
if gcloud compute instances describe $NODE_NAME --format="value(mig,status)" | grep -q "mig"; then
instance_status=$(gcloud compute instances describe $NODE_NAME --format="value(status)")
if [ "$instance_status" == "RUNNING" ]; then
# Collect and push the metrics
curl -s localhost:9100/metrics | curl --data-binary #- $PUSHGATEWAY_SERVER/metrics/job/node-exporter/instance/$NODE_NAME
else
# If the instance is not running, delete its metrics from the push gateway
metric_to_delete=$(echo $existing_metrics | grep "$NODE_NAME")
if [ -n "$metric_to_delete" ]; then
curl -X DELETE $PUSHGATEWAY_SERVER/metrics/job/node-exporter/instance/$NODE_NAME
fi
fi
fi
done
I have node + express backend which is called by my angular frontend (two separate domains).
In my backend, I am doing an API call to some other Third Party API.
My backend is working fine when I deploy it locally, but on deploying it to aws, the request times out (CORS Issue?). I checked issues with my deployment, and it successfully deployed, and I am able to make calls to endpoints where I am not calling the third party - API.
I also checked the API, it works when I call it manually or directly from my frontend.
Any idea what could be the issue?
Yes, this is a CORS issue.
The most common way to handle this is to add a Reverse Proxy (Nginx for example) to have a single domain that both servers the application (static files) and exposes the Backend (API).
+----------+
| |
| Client |
| |
+----+-----+
|
| myDomain.com
|
[INTERNET]
|
|
v :80
+--------+-------+
/* | | /api
+----------+ Revese Proxy +---------+
| | | |
| +----------------+ |
| |
| |
| :8080 | :3000
| |
+----+----------+ +---------+-----+
| | | |
| Angular | | Backend |
| (webserver) | | (API server) |
| | | |
+---------------+ +---------------+
Any request to myDomain.com will be checked. If the path contains /api the request will be passed to the Backend. any other request will be passed to the webserver that contains the SPA static file.
This means that you will need to add the /api prefix to your client API calls (see https://angular.io/guide/build#proxying-to-a-backend-server)
Note: You can achieve the same using sub-domains instead of paths (myDomain.com/api will become api.myDomain.com/). You just need to be able to support it in your deployment.
My organization has two GitLab CI runners spun up on DigitalOcean. These runners have the exact same system configuration, Docker servers on Ubuntu.
The only difference between these two servers is how they are assigned to the GitLab (gitlab.com) projects:
Runner 1 is assigned on a project-by-project basis
Runner 2 is assigned to the top-level group owning all projects
Visual for clarity:
+ Group
|
+--- Runner 2
|
|--+ Sub-Group 1
| |
| +--+ Project 1.1
| | |
| | +--- Runner 1
| |
| +--+ Project 1.2
| |
| +--- Runner 1
|
+--+ Sub-Group 2
|
+--+ Project 2.1
| |
| +--- Runner 1
|
+--+ Project 2.2
|
+--- Runner 1
The only runner actually picking up jobs is Runner 1. Runner 2 appears as an "Available Group Runner" in the CI/CD settings for all projects, but not their second-level subgroups.
Is there a better way to do this? We'd rather avoid having to assign the runners on a per-project basis, but the group runner functionality is not working for us with the current configuration.
Our ideal configuration:
+ Group
|
+--- Runner 2
|
|--+ Sub-Group 1
| |
| +--- Runner 1
| |
| +--+ Project 1.1
| |
| +--+ Project 1.2
|
+--+ Sub-Group 2
|
+--+ Project 2.1
|
+--+ Project 2.2
To the best of my knowledge and research, GitLab does not support this functionality. GitLab Group Runners appear to only be available to projects directly within the scope of the group in question and not to any subgroups.
Hopefully this functionality is addressed in the future. Until then, I will continue to add Runners on a per-repo basis.
I found a script online a few months ago that I changed and came up with the solution below.
It uses EC2-Describe-Instances and uses Perl to collect the Instance Names, IP address and updates Route53.
It works but its a bit in-efficient, I'm more of a .Net programmer and I am a little out of my depth so hopefully someone can help or point me in the right direction.
What I am thinking is I want it to save a copy of EC2-Describe-Instances from last time it ran and then get a fresh copy. Compare the differences and then only run the Route53 update for Instances that have changed IP. Any Ideas?
#!/bin/bash
set root='dirname $0'
ec2-describe-instances -O ###### -W ##### --region eu-west-1 |
perl -ne '/^INSTANCE\s+(i-\S+).*?(\S+\.amazonaws\.com)/
and do { $dns = $2; print "$1 $dns\n" }; /^TAG.+\sName\s+(\S+)/
and print "$1 $dns\n"' |
perl -ane 'print "$F[0] CNAME $F[1] --replace\n"' |
grep -v '^i-' |
xargs --verbose -n 4 -I myvar /bin/sh -c '{ /usr/local/bin/cli53 rrcreate -x 300 contoso.com 'myvar'; sleep 1; printf "\n\n"; }'
--edit--
Basically what I need is a way to compare a saved file with the output of EC2-Describe-Instances and then only return lines that contain differences to be fed back into the rest of the code.
Something like:
ChangedLines(File.txt, "ec2-describe-instances -O ###### -W ##### --region eu-west-1") | perl......
If
File 1 =
ABC
DEF
GHI
JKL
Output =
ABC
DEF
GHJ
JKL
Return =
GHJ
Example of EC2-Descibe-Instances
PROMPT> ec2-describe-instances
RESERVATION r-1a2b3c4d 111122223333 my-security-group
INSTANCE i-1a2b3c4d ami-1a2b3c4d ec2-203-0-113-25.compute-1.amazonaws.com ip-10-251-50-12.ec2.internal running my-key-pair 0 t1.micro YYYY-MM-DDTHH:MM:SS+0000 us-west-2a aki-1a2b3c4d monitoring-disabled 184.73.10.99 10.254.170.223 ebs paravirtual xen ABCDE1234567890123 sg-1a2b3c4d default false
BLOCKDEVICE /dev/sda1 vol-1a2b3c4d YYYY-MM-DDTHH:MM:SS.SSSZ true
RESERVATION r-2a2b3c4d 111122223333 another-security-group
INSTANCE i-2a2b3c4d ami-2a2b3c4d ec2-203-0-113-25.compute-1.amazonaws.com ip-10-251-50-12.ec2.internal running my-key-pair 0 t1.micro YYYY-MM-DDTHH:MM:SS+0000 us-west-2c windows monitoring-disabled 50.112.203.9 10.244.168.218 ebs hvm xen ABCDE1234567890123 sg-2a2b3c4d default false
BLOCKDEVICE /dev/sda1 vol-2a2b3c4d YYYY-MM-DDTHH:MM:SS.SSSZ true
I need to capture the lines in which the IP address has changed from the previous run.
It sounds like your actual goal is to update Amazon Route 53 for newly launch Amazon EC2 instances. There's a few different approaches you could take.
List instances launched during a given period
Use the AWS Command-Line Interface (CLI) to list instances that were recently launched. I found this example on https://github.com/aws/aws-cli/issues/1209:
aws ec2 describe-instances --query 'Reservations[].Instances[?LaunchTime>=`2015-03-01`][].{id: InstanceId, type: InstanceType, launched: LaunchTime}'
Modified for your needs:
aws ec2 describe-instances --query 'Reservations[].Instances[?LaunchTime>=`2015-03-01`][].{id: InstanceId, ip: PrivateIpAddress}' --output text
Let the instance update itself
Thinking a different way, why not have the instances update Amazon Route 53 themselves? Use a start script (via User Data) that calls the AWS CLI to update Route 53 directly!
Instances can retrieve their IP address via instance metadata:
curl http://169.254.169.254/latest/meta-data/public-ipv4/public/
Then call aws route53 change-resource-record-sets to update records.
i'm trying to run the Red5 Media Server on my Live Server (Win Server 2008 R2).
installation worked, i've entered localhost with port 5080, but when trying to start the Red5 service i'm getting this error:
Windows could not start the Red5 service on Local Computer.
Error 1067: The process terminated unexpectedly.
things i already tried (without success):
run red5 setup as administrator
run red5 service as administrator
open port 5080 in firewall
turn off iis before trying to start the service
any ideas what could be wrong? thanks
UPDATE:
i've checked the logs:
(i already set the JAVA_HOME user environment variable and rebooted) .. no success yet
STATUS | wrapper | 2012/10/09 13:36:42 | --> Wrapper Started as Service
STATUS | wrapper | 2012/10/09 13:36:42 | Java Service Wrapper Community Edition 32-bit 3.3.6
STATUS | wrapper | 2012/10/09 13:36:42 | Copyright (C) 1999-2009 Tanuki Software, Ltd. All Rights Reserved.
STATUS | wrapper | 2012/10/09 13:36:42 | http://wrapper.tanukisoftware.org
STATUS | wrapper | 2012/10/09 13:36:42 |
STATUS | wrapper | 2012/10/09 13:36:43 | Launching a JVM...
FATAL | wrapper | 2012/10/09 13:36:43 | Unable to execute Java command. The system cannot find the file specified. (0x2)
FATAL | wrapper | 2012/10/09 13:36:43 | "java" -Xrs -XX:+AggressiveOpts -XX:+DisableExplicitGC -Djava.net.preferIPv4Stack=true -Dlogback.ContextSelector=org.red5.logging.LoggingContextSelector -Dcatalina.useNaming=true -Dpython.home=lib -Xverify:none -Xms256m -Xmx768m -Djava.library.path="lib" -classpath "lib/wrapper.jar;conf;boot.jar" -Dwrapper.key="35GMYkROEBTyRT4O" -Dwrapper.port=32000 -Dwrapper.jvm.port.min=31000 -Dwrapper.jvm.port.max=31999 -Dwrapper.pid=2420 -Dwrapper.version="3.3.6" -Dwrapper.native_library="wrapper" -Dwrapper.service="TRUE" -Dwrapper.cpu.timeout="10" -Dwrapper.jvmid=1 org.tanukisoftware.wrapper.WrapperSimpleApp org.red5.server.Bootstrap
ADVICE | wrapper | 2012/10/09 13:36:43 |
ADVICE | wrapper | 2012/10/09 13:36:43 | ------------------------------------------------------------------------
ADVICE | wrapper | 2012/10/09 13:36:43 | Advice:
ADVICE | wrapper | 2012/10/09 13:36:43 | Usually when the Wrapper fails to start the JVM process, it is because
ADVICE | wrapper | 2012/10/09 13:36:43 | of a problem with the value of the configured Java command. Currently:
ADVICE | wrapper | 2012/10/09 13:36:43 | wrapper.java.command=java
ADVICE | wrapper | 2012/10/09 13:36:43 | Please make sure that the PATH or any other referenced environment
ADVICE | wrapper | 2012/10/09 13:36:43 | variables are correctly defined for the current environment.
ADVICE | wrapper | 2012/10/09 13:36:43 | ------------------------------------------------------------------------
ADVICE | wrapper | 2012/10/09 13:36:43 |
FATAL | wrapper | 2012/10/09 13:36:43 | Critical error: wait for JVM process failed
Try to run the red5.bat to get more details about the error.
I was running into the same problem as you and managed to resolve it I thought I'd share:
In my case I needed to add a reference to the bin-folder of the Java Runtime Environment under the PATH environment variable. (Start --> Computer; Properties --> Advanced System Settings --> tab "advanced"; Environment Variables --> system variabeles; PATH variable.
Click edit and add the path to the bin folder of your Java installation.