Shantanu's Blog

Corporate Consultant

February 08, 2016

 

Locking AWS Vault for 7 years

Once you have created a vault in Glacier, goto "Settings" and choose "Vault Lock" as shown in this image.



This policy will not allow anyone to delete a file from "Business" vault for 7 years.

{
"version": "2012-10-17",
"statement": [
    {
"effect": "Deny",
"Principal": {"AWS": "*" },
"Action": "glacier:DeleteArchive",
"Resource": "arn:aws:glacier:us-east-1:account-number-12digit-without-dash:vaults/Business",
"Condition" {"NumericLessThanEquals": {"glacier:ArchiveAgeInDays": "2555"}}
     }
             ]
}

You can change the condition to lock the valuts tagged as "LegalHold"

"Condition": {"StringEquals": {"glacier":ResourceTag/LegalHold": "True"}}



It is highly recommended to create another AWS account for such long-term vaults so that you can cancell the account itself if you no longer need those files in the vault anymore.

Labels: , ,


February 02, 2016

 

Starting with docker

1) Docker can be easily installed if you are using Amazon Linux. Here are the steps to install and run docker.

sudo yum update -y

sudo yum install -y docker

sudo service docker start

# applies on for AWS
sudo usermod -a -G docker ec2-user

2) Let's download a sample application from github and build it as docker image.

git clone https://github.com/awslabs/ecs-demo-php-simple-app

cd ecs-demo-php-simple-app

cat Dockerfile

docker build -t shantanuo/amazon-ecs-sample .

3) You can login to docker hub and push your image.

docer login

docker push shantanuo/amazon-ecs-sample


4) now you can pull it down and "activate" the contents floating in the docker image.

docker pull shantanuo/amazon-ecs-sample

docker run -p 80:80 shantanuo/amazon-ecs-sample

Labels: , , , ,


January 30, 2016

 

Scrapping structured data using python

It is very difficult to find a bug if we are using bugzilla. For e.g. visit this page...

https://bugzilla.mozilla.org/query.cgi

Choose "ALL" in status and "Socorro" in Product to search for a bug. The bug database is also accessible using API and the URL will look something like this...

https://bugzilla.mozilla.org/rest/bug?product=Socorro&component=Backend&include_fields=status,id,resolution,summary

We can programatically access this page using 4 lines of python code:

url='https://bugzilla.mozilla.org/rest/bug'
params={'product': u'Socorro', 'component': u'Backend', 'include_fields': 'status,id,resolution,summary'}

import requests
bugs=requests.get(url, params=params).json()

Let's try to add this bugs list to pandas dataframe.

import pandas as pd
pd.DataFrame(bugs)

This will create a row for each bug, but all the fields are merged into one. We need separate column for each one like status, id, title so that we can manage, sort and export data.

documents = []
for bug in bugs['bugs']:
    documents.append({
        'url': 'https://bugzilla.mozilla.org/show_bug.cgi?id={}'.format(bug['id']),
        'title': bug['summary'],
        'id': bug['id'],
        'status': bug['status'],
        'popularity': bug['id'],
        'group': 'Socorro',
        'resolution': bug['resolution']
    })

We created a list of dictionaries called documents. This object can be imported in pandas without any problem.

import pandas as pd
pd.DataFrame(documents)

Once we have all the data, it can be easily exported to another service as shown here...

https://gist.github.com/shantanuo/f9f4a595b7bee10e1f36

Labels: , ,


January 28, 2016

 

Adding a record to dynamoDB using API

Lambda Function:

import boto3

client = boto3.resource('dynamodb')
table = client.Table('minfo')

def lambda_handler(event, context):
    item = {
        'marker': int(event['mar']),
        'latitude': str(event['late']),
        'longitude': str(event['long'])}
    table.put_item(Item=item)   
    return event['mar']

Link the above lambda function name for e.g. "dyno" to the API gateway.
It is important to correctly specify the mapping for API get method execution request:

{
"mar": "$input.params('mar')",
"late": "$input.params('late')",
"long": "$input.params('long')"
}

Once deployed, the URL will look something like this...

https://t4pwupa9vc.execute-api.us-east-1.amazonaws.com/stag2/?mar=8&long=865&late=56

The latitude and longitude are saved to dynamoDB database with key as "mar".
Python code is short (10 lines) and readable. Creating API gateway takes a few minutes and the cost is minimum.

Labels: , , ,


January 23, 2016

 

Upload a file to S3

This lambda code will copy the given URL to S3

import boto3
import os
import urllib2

bucket = 'billingac'

s3 = boto3.resource('s3')
s3_client = boto3.client('s3')

def lambda_handler(event, context):
    url = event['url']
    key = os.path.basename(url)
    response = urllib2.urlopen(url)
    zipcontent= response.read()

    with open("/tmp/log.zip", 'w') as f:
        f.write(zipcontent)

    s3_client.upload_file('/tmp/log.zip', bucket, key)
   
We can link this function to API gateway so that we can access it from the browser something like this...

https://12o3jwx4pi.execute-api.us-east-1.amazonaws.com/stag/?url=https://github.com/shantanuo/easyboto/archive/master.zip

https://12o3jwx4pi.execute-api.us-east-1.amazonaws.com/stag/?url=http://oksoft.blogspot.in/2016_01_01_archive.html

Labels: ,


Archives

June 2001   July 2001   January 2003   May 2003   September 2003   October 2003   December 2003   January 2004   February 2004   March 2004   April 2004   May 2004   June 2004   July 2004   August 2004   September 2004   October 2004   November 2004   December 2004   January 2005   February 2005   March 2005   April 2005   May 2005   June 2005   July 2005   August 2005   September 2005   October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006   January 2007   February 2007   March 2007   April 2007   June 2007   July 2007   August 2007   September 2007   October 2007   November 2007   December 2007   January 2008   February 2008   March 2008   April 2008   July 2008   August 2008   September 2008   October 2008   November 2008   December 2008   January 2009   February 2009   March 2009   April 2009   May 2009   June 2009   July 2009   August 2009   September 2009   October 2009   November 2009   December 2009   January 2010   February 2010   March 2010   April 2010   May 2010   June 2010   July 2010   August 2010   September 2010   October 2010   November 2010   December 2010   January 2011   February 2011   March 2011   April 2011   May 2011   June 2011   July 2011   August 2011   September 2011   October 2011   November 2011   December 2011   January 2012   February 2012   March 2012   April 2012   May 2012   June 2012   July 2012   August 2012   October 2012   November 2012   December 2012   January 2013   February 2013   March 2013   April 2013   May 2013   June 2013   July 2013   September 2013   October 2013   January 2014   March 2014   April 2014   May 2014   July 2014   August 2014   September 2014   October 2014   November 2014   December 2014   January 2015   February 2015   March 2015   April 2015   May 2015   June 2015   July 2015   August 2015   September 2015   January 2016   February 2016  

This page is powered by Blogger. Isn't yours?