Showing posts with label Amazon Web Services. Show all posts
Showing posts with label Amazon Web Services. Show all posts

Amazon S3 bucket policy to grant access to particular bucket

{
  "Id": "Policy1422882024097",
  "Statement": [
    {
      "Sid": "Stmt1422881897244",
      "Action": [
        "s3:ListBucket"
      ],
      "Effect": "Allow",
      "Resource": "arn:aws:s3:::testbucket"
    },
    {
      "Sid": "Stmt1422881932883",
      "Action": [
        "s3:DeleteObject",
        "s3:GetObject",
        "s3:PutObject"
      ],
      "Effect": "Allow",
      "Resource": "arn:aws:s3:::testbucket/*"
    },
    {
      "Sid": "Stmt1422871876524",
      "Action": [
        "s3:GetBucketLocation",
        "s3:ListAllMyBuckets"
      ],
      "Effect": "Allow",
      "Resource": "arn:aws:s3:::*"
      }
  ]
}

You can apply this policy on user.

Using Amazon EC2 command line interface

You can easily manage your Amazon AWS resource like Amazon EC2 instances, EBS volumes & VPC, etc.. through command line tools.

Before you start using these tools, it needs dependency softwares to run.

In this example, I am going to show you AWS command line tool configuration on Linux.

Prerequisites:
This tools requires Java 1.6 or later. Also we need to make sure that JAVA_HOME is set. You can refer this link to install/configure JAVA on linux.

Download and Install AWS command line tools.

1. Download AWS tools using below URL.

wget http://s3.amazonaws.com/ec2-downloads/ec2-api-tools.zip

2. Extract it at some location. For example, I will extract these tools at /home/ec2 directory.

3. Once you extract zip file at /home/ec2 folder, you need to set EC2_HOME variable now. You can set this variable in bashrc or in /etc/profile file.

export EC2_HOME=/home/ec2
export PATH=$PATH:$EC2_HOME/bin

4. Now you should be able to use tools. But its good to configure access_key and secret_key before we use these tools. As these tools requires them to authenticate. So you can include them in bashrc or /etc/profile.

export AWS_ACCESS_KEY=your-aws-access-key-id
export AWS_SECRET_KEY=your-aws-secret-key

5. Once you are done with all above command, you can check "ec2-describe-regions". This command will show you one or more region which are available to you.

Note: You need to have proper privileges associated with Access key to perform action on resources.

Give public access to Amazon S3 bucket

Giving each individual file public access becomes cumbersome in Amazon S3. I wanted to give public access to all files in Amazon S3 bucket, I was doing that manually for each file through shell script. But later on I found the way to give permission on bucket it self, so that it can make all files "public" in bucket. You can do it following below steps.


  1. Do right click on bucket -> Properties.
  2. Click on "Edit Bucket Policy". Add below policy and click on "save" button. Done!!!

Using "X-Forwarded-For" in Apache

This post gone be helpful if your incoming traffic is coming from NATed IP and you want to get the actual IP instead of the NATed one for some reason.

Before few days, I created two web server and one load balancer in Amazon EC2. Load balancer was forwarding traffic to two web servers. That was working fine, but due to nature of Amazon ELB we could not get the actual Source IP from where our site was being accessed.

So by googling, we found that using "X-Forwarded-For" we can get the original source's IP Address and we can utilize it for further use(may be for troubleshooting or for Geocoding or for webalizer to create informative graphs). It was just a 3 line of code, which did a trick. You can use that in apache configuration.

Monitoring JMX port in Amazon EC2 hosted server

Recently I have started work on Amazon EC2 servers. At some point I got requirement to monitor JMX port of server which was hosted on Amazon EC2. I followed the steps which was previously posted in my blog. But it did not help. I tried changing CATALINA_OPTS parameters as well as I opened all ports in Amazon "Security Group". After all I got help from Feil Figg blog. So below are steps to achieve it.

(1) First download catalina-jmx-remote.jar file as per tomcat's version and keep it in tomcat\lib folder.

(2) Now open tomcat/conf/server.xml file and add below line into it.

<Listener className="org.apache.catalina.mbeans.JmxRemoteLifecycleListener" rmiRegistryPortPlatform="8999" rmiServerPortPlatform="8999"/>



(3) Now open tomcat/bin/setenv.sh file and add below java parameters.



JAVA_OPTS="${JAVA_OPTS} -Djava.rmi.server.hostname=ec2-xx-xxx-x-xxx.compute-1.amazonaws.com -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false"

(4) Now restart tomcat. You need to open port 8999 in Amazon "Security Group".

Hope this will help.