Monday, February 26, 2018

redirect-http-https-on AWS ELB

Apache

The rewrite rule for an Apache backend is similar to the following in .htaccess:




RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]

 
 
Nginx

The rewrite rule for an Nginx backend in the ngnix.conf file is similar to the following:
Note: Applies to versions nginx/1.10.3 (Ubuntu) and nginx/1.12.1 (Amazon Linux).


server {
      listen         80;
      server_name    www.example.org;
      if ($http_x_forwarded_proto = 'http') {            
  return 301 https://$server_name$request_uri$http_x_forwarded_proto;
        }
}
Ec2-instance-AutoEIP-Assign-for-whitelistip

#!/bin/sh
# Region in Which instance is running
EC2_REGION='us-east-1'
AWS_ACCESS_KEY='XXXXXXXXXXX'
AWS_SECRET_ACCESS_KEY='XXXXXXXXXXXXXXXXXXX'

#Instance ID captured through Instance meta data
InstanceID=$(/usr/bin/curl -s http://169.254.169.254/latest/meta-data/instance-id)

#Query free Elastic IP and write all allocated ID  aws ec2 describe-addresses --query 'Addresses[?AssociationId==null]' --output text | perl -lne 'print if /(\w+\-){1}\w+/'

aws ec2 describe-addresses --query 'Addresses[?AssociationId==null]' --output text | perl -lne 'print $& if /(\w+\-){1}\w+/' > /opt/EIP/output.txt
Allocate_ID=$(head -n 1 /opt/EIP/output.txt)

#Assigning Elastic IP to Instance
aws ec2 associate-address --instance-id $InstanceID --allocation-id $Allocate_ID
Wednesday, December 20, 2017

AWS Cli IMP commands

Describe Load Balancer

aws elb describe-load-balancers --load-balancer-names lbname --output text|grep INSTANCES|awk '{print $2}' > filename.txt

Describe intance  with particular instnace type

aws ec2 describe-instances --filters "Name=instance-type,Values=m5.large"

aws ec2 describe-instances

Describe aws account all volume  and there information

aws ec2 describe-volumes --query 'Volumes[*].{ID:VolumeId,Tag:Tags}'

Get Instance ID information
wget -q -O - http://169.254.169.254/latest/meta-data/instance-id

Get instance public ip

aws ec2 describe-instances --instance-id $p | grep PublicIpAddress | grep -o -P "\d+\.\d+\.\d+\.\d+" | grep -v '^10\.' >> filename.txt










Configure httpd server status enable and configure on 800 port.

Then while use aws cli configure shell script and set in cron which send data to cloudwatch after every few min.

#!/bin/bash

logger "Apache Status Started"

export AWS_CREDENTIAL_FILE=/opt/aws/credential-file-path.template
export AWS_CLOUDWATCH_HOME=/opt/aws/apitools/mon
export AWS_PATH=/opt/aws
export AWS_AUTO_SCALING_HOME=/opt/aws/apitools/as
export AWS_ELB_HOME=/opt/aws/apitools/elb
export AWS_RDS_HOME=/opt/aws/apitools/rds
export EC2_AMITOOL_HOME=/opt/aws/amitools/ec2
export EC2_HOME=/opt/aws/apitools/ec2
export JAVA_HOME=/usr/lib/jvm/jre
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/aws/bin:/root/bin

SERVER=`wget -q -O - http://169.254.169.254/latest/meta-data/instance-id`
#echo SERVER=$SERVER
BUSYWORKERS=`wget -q -O - http://localhost:800/server-status?auto | grep BusyWorkers | awk '{ print $2 }'`
#echo BUSYWORKERS=$BUSYWORKERS
IDLEWORKERS=`wget -q -O - http://localhost:800/server-status?auto | grep IdleWorkers | awk '{ print $2 }'`
#echo IDLEWORKERS=$IDLEWORKERS

/opt/aws/bin/mon-put-data --metric-name httpd-busyworkers --namespace "EC2: HTTPD" --dimensions "InstanceId=$SERVER" --unit Count --value $BUSYWORKERS

/opt/aws/bin/mon-put-data --metric-name httpd-idleworkers --namespace "EC2: HTTPD" --dimensions "InstanceId=$SERVER" --unit Count --value $IDLEWORKERS

logger "Apache Status Ended with $SERVER $BUSYWORKERS $IDLEWORKERS"


we can setup Alaram for scale server once Busyworks reach at MinSpareServers or above more than 5 min.  as well as scale down once Busyworks less down MinSpareServers more than 5 min.

Detail and brief information we can found on

http://blog.domenech.org/2012/11/aws-cloudwatch-custom-metric-for-apache.html
While try to convert  C4,M4,T2,R3 instance in C5 instance basically we get below error.

Error starting instances
Enhanced networking with the Elastic Network Adapter (ENA) is required for the 'c5.xlarge' instance type. Ensure that your instance 'i-0de56fe4bb5f3ba27' is enabled for ENA.

So we have to follow below steps.

modinfo ena   this will show ena support is on or not if not then do yum update and reboot instance

ethtool -i eth0        check ena module is loaded

configure aws cli on another instance from that instance your can query your instance status

aws ec2 describe-instances --instance-ids i-040b1236aXXXXX --query 'Reservations[].Instances[].EnaSupport'

configure aws cli on another instance from that instance your can query your AMI status 

aws ec2 describe-images --image-id ami-2XXXXX --query 'Images[].EnaSupport'

command to enable ena support

aws ec2 modify-instance-attribute --instance-id i-040bXXXX--ena-support

Backup your instance and create AMI for safer side.

First run yum update on your instance which help to install ena support driver then shutdown instance and query for ena support if value come null means need to modify instance and enable ena support.  if value come true means you can upgrade instance as C5.

For ena support AMI first create instance and make ena support and then take new AMI.
When we got /bin/bash^M: bad interpreter: error means script created on windows system via editor

(Windows made script run in linux /bin/bash^M: bad interpreter: No such file or directory [duplicate])

in that case we have to run below and replace

Run following command in terminal
sed -i -e 's/\r$//' scriptname.sh
Then try
./scriptname.sh
It should work.
Wednesday, September 27, 2017

Schedule Automated Amazon EBS Snapshots Using CloudWatch Events

https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/TakeScheduledSnapshot.html

Step 1: Create a Rule

Create a rule that takes snapshots on a schedule. You can use a rate expression or a cron expression to specify the schedule. For more information, see Schedule Expressions for Rules.
To create a rule
  1. Open the CloudWatch console at https://console.aws.amazon.com/cloudwatch/.
  2. In the navigation pane, choose Events, Create rule.
  3. For Event Source, do the following:
    1. Choose Schedule.
    2. Choose Fixed rate of and specify the schedule interval (for example, 5 minutes). Alternatively, choose Cron expression and specify a cron expression (for example, every 15 minutes Monday through Friday, starting at the current time).
  4. For Targets, choose Add target and then select EC2 Create Snapshot API call.
  5. For Volume ID, choose an EBS volume.
  6. Choose Configure details.
  7. For Rule definition, type a name and description for the rule.
  8. For AWS permissions, choose the option to create a new role. This opens the IAM console in a new tab. The new role grants the built-in target permissions to access resources on your behalf. Choose Allow. The tab with the IAM window closes.
  9. Choose Create rule.