Tuesday, August 30, 2022

Nginx location Regex Expression

 Nginx Regex Expression

Nginx location block allow you to route request to particular location in file system or particular url.

Below example is to show how to route domain name and number to domain.com:port.

server {

  listen 80;

  server_name anup.co.in;

  root /usr/share/nginx/html;

   location ~ "/app/lck/([a-z0-9\-\.]+)/([0-9]+)" {

      return 301 http://$1:$2;



    Above highlighted part will redirect as follows -

http://anup.co.in/app/lck/google.com/8080  --> http://google.com:8080

Friday, March 11, 2022

How to run Docker inside Docker using Dockerfile

 How to run Docker inside Docker using Dockerfile

In some cases we want to run docker command inside docker container, we can do that by mapping docker.sock volume while running container.  The other option is to use your Dockerfile.

1] Here is Dockerfile -

FROM ubuntu:18.04

#Install Docker

RUN apt-get update

RUN apt-get -y install apt-transport-https

RUN apt-get -y install ca-certificates

RUN apt-get -y install curl

RUN apt-get -y install gnupg2

RUN apt-get -y install software-properties-common


RUN curl -fsSL https://download.docker.com/linux/ubuntu/gpg | (OUT=$(apt-key add - 2>&1) || echo $OUT)

RUN add-apt-repository --yes "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"

RUN apt-get update

RUN echo "deb http://security.ubuntu.com/ubuntu xenial-security main" >> /etc/apt/sources.list; apt-get update

RUN apt-get -y install build-essential

RUN apt-get -y install docker-ce docker-ce-cli containerd.io

CMD ["tail", "-f", "/dev/null"]

2] Build docker image.

# docker build -t docker-in-docker:latest .

3] Run docker container from above docker image.
# docker run -d --name dockerINdocker docker-in-docker:latest

4] Enter into docker container and confirm docker version as per below image.

Tuesday, August 24, 2021

Download large file from Google Drive using wget on terminal

 Download large file from Google Drive using wget on terminal

       To download large file from Google Drive use following steps.

1] Share file publicly and Copy share URL.

Example share URL - 


2] Extract Field ID from above share URL as below.


Field ID is - 1tcthANUPNgyho7X-5HPDuUAiEfTfw5

3] Go to terminal and paste following command.

wget --load-cookies /tmp/cookies.txt "https://docs.google.com/uc?export=download&confirm=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id=FIELDID' -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=FIELDID" -O FILENAME && rm -rf /tmp/cookies.txt

Here, Replace FIELDID and FILENAME as per your file.

Let me know how it goes.

Wednesday, March 31, 2021

Nginx Cookbook

 Nginx Cookbook

1] Wildcard for Nginx location

I have multiple API running on server to access them through I have to add multiple location block as below.

My goal is to add single location block for all API's.

server { listen 80; server_name www.anup.co.in; location / { proxy_pass http://localhost:3000; } location /getHighscores { proxy_pass http://localhost:3000/getHighscores; } location /auth/google { proxy_pass http://localhost:3000/auth/google; } location /auth/google/redirect { proxy_pass http://localhost:3000/auth/google/redirect; } location /auth/login/success { proxy_pass http://localhost:3000/auth/login/success; } location /auth/login/failed { proxy_pass http://localhost:3000/auth/login/failed; } location /auth/logout { proxy_pass http://localhost:3000/auth/logout; } }


server { listen 80; server_name www.anup.co.in; location / { proxy_pass http://localhost:3000; } location ~ ^/(.*)$ { proxy_pass http://localhost:3000/$1; } }

Tuesday, August 11, 2020

Azure DevOps Pipeline Runtime parameter Task Condition

 Azure DevOps Pipeline Runtime parameter Task Condition

    This guide explains you how to use Azure DevOps pipeline to pass runtime boolean values and run tasks only if condition is true else skip the task.

- Add following lines at the beginning of your pipeline YAML file


    - qa
    - '*'
    - 'docs/*'
    - '*.md'

    - qa

newrelic_cmd'docker run --entrypoint /bin/mv $(containerRegistry)/$(imageRepository):latest'

  conditionand(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/qa'))
  displayNameRelease to QA
  - jobRelease
  - deploymentDeployToQA
Other Tasks
# Below Task will only be executed if condition is true, default value is
# false in parameter. When you click on run pipeline it will ask you the
# parameter value i.e. installNewRelic if you select then condition becomes
# true and below task will executed else it will be skipped.
# Refer the screenshot below
             displayName'Place newrelic.ini from /usr/share/nginx/html/docroot/profiles/'
             conditionand(succeeded(), eq('${{ parameters.installNewRelic }}', true))
               script: |
                 $(newrelic_cmd) $(docroot)/profiles/corp-qa-newrelic.ini /etc/php/7.3/mods-available/newrelic.ini

Saturday, August 03, 2019

Create VPC Subnet Security Group EC2 ELB using Python Boto3

This script assumes basic knowledge of AWS, Boto3 & Python.
  • AWS Account
  • IAM Role with Access & Secret Key
  • Boto3 Installed & Configured

- Install AWS CLI & Python Boto3 Library in Python using pip, which is package management tool written in Python.
# pip install awscli boto3

- Create user in AWS from AWS console and get the Secret Access Key & Access ID to access AWS services programatically.
# aws configure
- Run script using python command 
# python <script-name>.py

import boto3
import time

ec2 = boto3.resource('ec2')
client = boto3.client('ec2')

#Create VPC
response = client.create_vpc(CidrBlock='',InstanceTenancy='default')

#Assign tags to VPC
client.create_tags(Resources=[response['Vpc']['VpcId']],Tags=[{'Key': 'Name','Value': 'my-drupal-vpc',}])

print('***** VPC Created with ID*********',response['Vpc']['VpcId'])
vpc_id = response['Vpc']['VpcId']

# Creating Internet Gateway for Drupal Web Instance subnets and attaching to VPC
ig = ec2.create_internet_gateway()
client.attach_internet_gateway(InternetGatewayId = ig.id, VpcId=vpc_id)

routetable1_response = client.create_route_table(VpcId=vpc_id)

def create_tag_for_route_table(route_table_number, route_table_name):
    tag = client.create_tags(Resources=[route_table_number['RouteTable']['RouteTableId']],Tags=[{'Key': 'Name','Value': route_table_name}])
    return tag

print('Route Table 1 Created - ',routetable1_response['RouteTable']['RouteTableId'])
route_table1 = ec2.RouteTable(routetable1_response['RouteTable']['RouteTableId'])

# Attach internet gateway to Routetable drupal-rt1 for web instances in subnet 1 and 3
route_table1.create_route(DestinationCidrBlock='', GatewayId=ig.id)

# Attach internet gateway to Routetable drupal-rt1 for web instances in subnet 1 and 3
route_table1.create_route(DestinationCidrBlock='', GatewayId=ig.id)

def create_subnet(cidr, vpc_id, azname):
        subnet_response = client.create_subnet(CidrBlock=cidr, VpcId=vpc_id, AvailabilityZone=azname)
        return subnet_response

def create_tag(subnet_number,subnet_name):
    client.create_tags(Resources=[subnet_number['Subnet']['SubnetId']], Tags=[{'Key': 'Name', 'Value': subnet_name}])

def modify_subnet_attribute(subnet_name):
    client.modify_subnet_attribute(MapPublicIpOnLaunch={'Value': True,}, SubnetId=subnet_name)

#Creating first subnet
subnet1 = create_subnet('', vpc_id, 'us-east-1a')
ec2_subnet1 = subnet1['Subnet']['SubnetId']
print('Subnet 1 is Created with ID - ',ec2_subnet1)

#Associating Route Table 1 to Subnet 1
print('Route table 1 associated with Subnet 1 -',ec2_subnet1)

routetable2_response = client.create_route_table(VpcId=vpc_id)
print('Route Table 2 Created - ',routetable2_response['RouteTable']['RouteTableId'])
route_table2 = ec2.RouteTable(routetable2_response['RouteTable']['RouteTableId'])

# Creating second subnet
subnet2 = create_subnet('', vpc_id, 'us-east-1a')
ec2_subnet2 = subnet2['Subnet']['SubnetId']
print('Subnet 2 is Created with ID - ',ec2_subnet2)

#Associating Route Table 2 to Subnet 2
print('Route table 2 associated with Subnet 2 -',ec2_subnet2)

# Creating third subnet
subnet3 = create_subnet('', vpc_id, 'us-east-1b')
ec2_subnet3 = subnet3['Subnet']['SubnetId']
print('Subnet 3 is Created with ID - ',ec2_subnet3)

#Associating Route Table 1 to Subnet 3
print('Route table 1 associated with Subnet 3 -',ec2_subnet3)

# Creating fourth subnet
subnet4 = create_subnet('', vpc_id, 'us-east-1b')
ec2_subnet4 = subnet4['Subnet']['SubnetId']
print('Subnet 4 is Created with ID - ',ec2_subnet4)

#Associating Route Table 2 to Subnet 4 
print('Route table 2 associated with Subnet 4 -',ec2_subnet4)

def create_security_group(descript, group_name):
    sg1_response = client.create_security_group(Description=descript,GroupName=group_name,VpcId=vpc_id)
    return sg1_response

def create_sg_tag(websg_or_elbsg,sg_group_name):
    sg_tag_response = client.create_tags(Resources=[websg_or_elbsg['GroupId']],Tags=[{'Key': 'Name','Value': sg_group_name}])
    return sg_tag_response

#Create Security Group for Drupal instances which will accept traffic from ALB
web_sg1 = create_security_group('Accept traffic from ALB', 'drupal-web-sg')
sgId = web_sg1['GroupId']
print('Created Security Group for Web Instances -',sgId)

# Create Security for ALB which will accept traffic from Internet
elb_sg1 = create_security_group('Accept traffic from Internet','drupal-elb-sg')
elbsgId = elb_sg1['GroupId']
print('Created Security Group for ELB -',elbsgId)

elb1 = ec2.SecurityGroup(elbsgId)
elb1.authorize_ingress(GroupId=elbsgId,IpPermissions=[{'IpProtocol': 'tcp', 'FromPort': 80, 'ToPort': 80, 'IpRanges': [{'CidrIp': ''}]}])

client.authorize_security_group_ingress(GroupId=sgId, IpPermissions=[{'IpProtocol': '-1','UserIdGroupPairs': [{'GroupId': elbsgId}]}])

#Creating SSH key file for drupal instances
# create a file to store the key locally                                 
outfile = open('drupal-ec2-keypair.pem','w')                                                                                                      
# call the boto ec2 function to create a key pair                        
key_pair = ec2.create_key_pair(KeyName='drupal-ec2-keypair')             
# capture the key and store it in a file                                 
KeyPairOut = str(key_pair.key_material)                                    

# Creating instances for Drupal Infrastructure 
user_data_script = """#!/bin/bash
yum clean all
yum update -y
yum install httpd -y
echo "Hello this is drupal website" >> /var/www/html/index.html
systemctl start httpd
systemctl restart httpd
systemctl enable httpd"""

def create_instances(subnet_name, instance_name):
    web_instance = ec2.create_instances(ImageId='ami-011b3ccf1bd6db744',InstanceType='t2.micro',MinCount=1,MaxCount=1,KeyName='drupal-ec2-keypair',SubnetId=subnet_name,UserData=user_data_script,SecurityGroupIds=[sgId],TagSpecifications=[{'ResourceType': 'instance','Tags': [{'Key': 'Name','Value': instance_name}]}])
    return web_instance

web1_instance = create_instances(ec2_subnet1, 'drupal-web1')
response1 = client.describe_instances()
for reservation in response1["Reservations"]:
     for instance in reservation["Instances"]:
        ec2 = boto3.resource('ec2')
        web1 = ec2.Instance(instance["InstanceId"])
print('Launching web1 instance - ',web1.id)

web2_instance = create_instances(ec2_subnet3, 'drupal-web2')
response2 = client.describe_instances()
for reservation in response2["Reservations"]:
     for instance in reservation["Instances"]:
        ec2 = boto3.resource('ec2')
        web2 = ec2.Instance(instance["InstanceId"])
print('Launching web2 instance - ',web2.id)

# Application Load Balancer Code Starts here

lb = boto3.client('elbv2')
create_lb_response = lb.create_load_balancer(
        ec2_subnet1, ec2_subnet3,
            'Key': 'Name',
            'Value': 'drupal-web-elb'

lbId = create_lb_response['LoadBalancers'][0]['LoadBalancerArn']
print('Successfully created load balancer - ',lbId)

create_tg_response = lb.create_target_group(
tgId = create_tg_response['TargetGroups'][0]['TargetGroupArn']
print('Successfully created target group - ',tgId)
#Create Listner for web elb
listnerId = lb.create_listener(
            'Type': 'forward',
            'TargetGroupArn': tgId

# Register web instances with web-elb
regis_targets = lb.register_targets(TargetGroupArn=tgId,Targets=[{'Id': web1.id,},{'Id': web2.id}])