30 April, 2020

Access Denied when calling the CreateInvalidation operation on AWS CLI.

If you are getting access denied when calling the CreateInvalidation operation on AWS CLI, it must be a permission issue for that user. 

In this post, I am using the Jenkins pipeline to build and pushing the artifacts into S3. I am using CloudFront for Content Delivery Network (CDN) and hosting my web site in Route 53. 

When I am trying to do the CloudFront Distribution invalidate the cache from CLI, I am getting this below error. I thought to add some screenshot to get more visibility, so added below.

Error Log:-

A client error (AccessDenied) occurred when calling the CreateInvalidation operation: User: arn:aws:iam::xxxxxxxxxxx:user/yyyy is not authorized to perform: cloudfront:


The below command I am using from AWS CLI :

aws configure set preview.cloudfront true
aws cloudfront create-invalidation --distribution-id UJH89JKKMOVY340 --paths "/*"    



Resolution:-

Add the "CreateInvalidation" permission to that user. Below are the steps to add the permission.


  • Goto Identity and Access Management (IAM) 
  • Goto Users and find your username, here for me its "Jenkins"
  • Then add a new "Add Inline policy", below the screenshot.

  • Now add the below policy into the JSON policy editor. Below the screenshot.

Policy JSON:-


{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditoro",
            "Effect": "Allow",
            "Action": "cloudfront:CreateInvalidation",
            "Resource": "arn:aws:cloudfront::17088938460999:distribution/UJH89JKKMOVY340"
        }
    ]
}


Sample Screenshot:-




Now, it's working fine. I can see the Jenkins logs below.

Jenkins Success Log:-

; perhaps you meant to use ‘PATH+EXTRA=/something/bin’?
+ aws configure set preview.cloudfront true
[Pipeline] sh
Warning: JENKINS-41339 probably bogus PATH=/var/lib/jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/node-v10.16.3-linux-x64/bin:/var/lib/jenkins/tools/hudson.model.JDK/JDK8-152/bin:$PATH:/usr/local/bin:$MAVEN_HOME/bin:/usr/local/bin:/var/lib/jenkins/tools/hudson.tasks.Maven_MavenInstallation/mvn/bin:/usr/sbin:/usr/bin:/sbin:/bin; perhaps you meant to use ‘PATH+EXTRA=/something/bin’?
+ aws cloudfront create-invalidation --distribution-id UJH89JKKMOVY340 --paths '/*'
{
    "Invalidation": {
        "Status": "InProgress", 
        "InvalidationBatch": {
            "Paths": {
                "Items": [
                    "/*"
                ], 
                "Quantity": 1
            }, 
            "CallerReference": "cli-1588239578-85708"
        }, 
        "Id": "I3HILN71CKWOV4", 
        "CreateTime": "2020-04-30T09:39:38.919Z"
    }, 
    "Location": "https://cloudfront.amazonaws.com/2019-03-26/distribution/UJH89JKKMOVY340/invalidation/I3HILN71CKWOV4"
}




27 April, 2020

How to get access key id and secret access key of amazon user.

This key combination (i.e. access key id and secret access key)  of aws will be useful everywhere when you need to access your aws services (Example - S3, EC2, etc). Its very simple to get the credentials for your user.



  • Goto to Identity and Access Management (IAM)
  • Click on Users (Left side of the page under Access Management)
  • Then click on your user name from the user list. You will be seeing the below screen.


  • Click on "Security and credentials" and click on "Create access key" to creating the new access key and download the .csv file as per the below screenshot.


  • Now you can see your access key and secret access key looks like as below.
    • Access key ID - AAKIASPHRAQWAKKLAR87
      Secret access key - sZ+7JJKd++UjfjfuueFJV9pXXVDOv48xiBbm



13 July, 2019

Find all palindrome strings in a java array.


In this article, we will see how to find all palindrome strings from an array. This is a very frequently asked questions in interview.  Also there is a question to find all palindrome number from an array.  Find few more collection interview question.  


PalindromeStrings.java


package com.javadevelopersguide.lab.basic;
/**
 * This program illustrates find all palindrome strings from array.
 *
 * @author manoj.bardhan
 *
 */
public class PalindromeStrings {
public static void main(String[] args) {
String stringArray[] = { "eye", "jdg", "javadevelopersguide", "aabaa", "hello", "pip" };
for (int i = 0; i < stringArray.length; i++) {
printOnlyPalindrom(stringArray[i]);
}
}
private static void printOnlyPalindrom(String str) {
String oldString = str;
StringBuilder builder = new StringBuilder(str);
if (builder.reverse().toString().equals(oldString)) {
System.out.println(oldString + " is a Palindrome.");
}
}
}


Output -

Find all possible palindromes in an array

In this article, we will see how to find all palindrome from an array. This is a very basic questions in interview, the interviewer will ask the same questions in different way. So, its good to know all possible questions from palindrome. Also there is a question to find all palindrome number from a list.  Find few more collection interview question.  Today we will see how to check a number is palindrome or not. 


FindAllPalindrome.java

package com.javadevelopersguide.lab.basic;
/**
 * This program illustrates find all palindrome from array.
 *
 * @author manoj.bardhan
 *
 */
public class FindAllPalindrome {
public static void main(String[] args) {
int numberArray[] = { 120, 990, 121, 777, 808, 1280 };
for (int i = 0; i < numberArray.length; i++) {
printOnlyPalindrom(numberArray[i]);
}
}
private static void printOnlyPalindrom(int number) {
int finalNumber = 0;
int oldNumber = number;
// Repeat the loop until the number became zero.
while (number != 0) {
// Get the First Digit (i.e. 1)
int firstDigit = number % 10;
// Get the Result number.
finalNumber = (finalNumber * 10) + firstDigit;
// Now get the remaining digits , after finding the first digit
number = number / 10;
}
// Now compare the finalNumber and oldNumber both are same or not.
if (finalNumber == oldNumber)
System.out.println(finalNumber + " is a Palindrome.");
}
}

Output -

121 is a Palindrome.
777 is a Palindrome.
808 is a Palindrome.


Find the number is palindrome or not


In this article, we will see how to check if a number is palindrome or not. This is a very basic questions in interview. But, you never know about what kind of question the interviewer will ask. So, better you prepare for every certain questions. Also there is a question to find all palindrome number from a list.  Find few more collection interview question.  Today we will see how to check a number is palindrome or not. 


Palindrom.java


package com.javadevelopersguide.lab.basic;
/**
 * This program illustrates how to check a number is palindrome or not.
 *
 * @author manoj.bardhan
 *
 */
public class Palindrom {
public static void main(String[] args) {
int number = 121;
int temp = number;
int finalNumber = 0;
// Repeat the loop until the number became zero.
while (number != 0) {
// Get the First Digit (i.e. 1)
int firstDigit = number % 10;
// Get the Result number.
finalNumber = (finalNumber * 10) + firstDigit;
// Now get the remaining digits , after finding the first digit
number = number / 10;
}
// Now compare the finalNumber and number both are same or not.
if (finalNumber == temp) {
System.out.println("This number is a Palindrome.");
} else {
System.out.println("This number is not a Palindrome.");
}
}
}


Output -

This number is a Palindrome.