Pages

Setup Java Web App in BitBucket

 

How to setup SSH keys in Bitbucket and Setup Java Web App in BitBucket

We have already seen how to set up Java Web Application in GitHub. Now, we will see how to set up a Java Web application in BitBucket as well. The steps for both are very similar.

BitBucket is one of the popular git-based version control systems. BitBucket is very good example for Software-as-a-service, hosted on cloud. Let us learn how to setup a Java web project in BitBucket.

Git is a purely distributed version control system. 
Please find below steps for how to setup SSH keys in Bitbucket from EC2 instance and also how to setup Java Web App using Maven in BitBucket.



Pre-requistes:
If you don't have an bitbucket account, You need to create an account in bitbucket.org.

Implementation steps:

1. after login into bitbucket, click on + and Click on Create Repository



2. enter the repo name as MyBitbucketRepo, make sure it is private, Include read me option - choose yes with template. Click Git as version control system.

3. Click on create repository.
4. now click on your avatar (left bottom of the screen, BitBucket settings-> security--> ssh keys
5. now go to your laptop, open git bash for windows laptop or iTerm (for iMac laptop) and connectto Jenkins EC2 instance using Git bash. 

6. Now type  ssh-keygen (and then simply enter four times, do not give any password)




7. copy the content of public key executing below command in Gitbash.
sudo cat ~/.ssh/id_rsa.pub

8. go to click on Add key from step # 3,  and paste the content of public key. give some name as mySSH key. click on Add key. 

 

9. Now click on repo you created in step # 1. Click on Clone and copy the SSH url. Do not select HTTPS Url. The url will be from your repo you created under Clone link. 

 

10. go back to Gitbash  windows connected to Jenkins EC2, execute the below command. Paste the SSH url you copied above.

git clone <ssh_url_from_bitbucket_repo_you_created>


11. Now after cloning, type ls -al command
12. It will show a folder with repo name..
13. navigate to repo folder by below command: cd mybitbucketrepo

14. use below command to create Java project using Maven. Maven is the build for creating and building java projects.
mvn archetype:generate -DgroupId=com.dept.app -DartifactId=MyWebApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

15. type ls -al to see the new folder called MyWebApp
16. type  below command to see the newly created project: git status



17. git add  *


18. Perform below command to commit the changes to local repository first.
git commit -m "my first project check-in to bitbucket"

19. Now perform below command to push from local repository to remote repository.
git push

Set up your first project in GitHub

 

How to setup SSH keys | How to setup Repo in GitHub and Setup Java Project in GitHub | How to add a project in GitHub

GitHub is one of the popular git-based version control systems. GitHub is very good example for Software-as-a-service, hosted on cloud. Let us learn how to create SSH keys and securely connect from EC2 instance. We will also learn how to create a Java Project using Maven and Upload Java Project into GitHub.

Git is a purely distributed version control system. 



Pre-requistes:
1. GitHub account
2. Git client installed
3. Connect to your Jenkins EC2 instance

Step # 1 - Create private repository in GitHub.com

Click on New to create a new repo 
Also choose initialize with repository with a README option.

Step # 2 Create SSH keys from your source machine

Login to your Jenkins EC2 instance using Git bash. make sure you are connected to Ec2 instance by ssh url. Type  ssh-keygen (and then simply enter four times, do not give any password). ssh-keygen will generate two keys - public keys and private keys. We need to upload the public keys into GitHub.


copy the content of public key by executing below command in Git bash:
sudo cat ~/.ssh/id_rsa.pub


Step # 3 - Upload SSH Keys in GitHub

Go to GitHub, click on Settings.
Copy SSH keys Key section and then click on Add SSH key.


Step # 4 - Clone Repo locally

Now click on the Cat icon on your top left to go back to repo you just created.
Click on the repo you created in step # 1, click on Code, under SSH
 




Copy the SSH url and go to your EC2 instance using git bash and perform below command:

git clone <paste the ssh url >

type yes to connect

After you clone, you should see something below:







type below command to list the directory
ls -al
The above command should list the directory you have cloned.

Now go into repo folder(red circled like above) you have cloned.
cd MyApplicationRepo

Step # 5 - Create Java Web App using Maven
use below maven command to create Java Web App using Maven. Maven is the build for creating and building java projects. 

mvn archetype:generate -DgroupId=com.dept.app -DartifactId=MyWebApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

Step # 6 - Push Java Web App using Git commands into GitHub

type below command to see the new folder called MyWebApp
ls -al

type  below command to see the newly created project in red color
git status

Now add the newly created folder by executing below command:
git add  *

Now make sure all the files are moved to staging area, when you execute below command:
git status


Perform below command to commit the changes to local repository first.
git commit -m "my first project check-in to GitHub"

Now perform below command to push from local repository to remote repository. 

git push

 

Verify code changes in GitHub

Now to GitHub and click on the repo, you will see your commits.

connect to an EC2 instance from Windows or Mac

 

How to connect to an EC2 instance from Windows or Mac

Pre-requistes:

1. SSH keys(for e.g., myEc2Jenkins.pem) already downloaded in your local machine, preferably in downloads folder.
2. Your EC2 instance is up and running in AWS
3. For windows laptop, you need to use Git bash. You can install Git by downloading from this URL - https://git-scm.com/downloads

Steps to connect to EC2 instance:

1. Go to AWS console.
2. Click on EC2, click on running instances link





3. Select the checkbox of EC2 you would like to connect to.
4. Click on Action. Copy the url from SSH which looks like below:
For e.g.
ssh -i "mykey.pem" ubuntu@dns_name.compute.amazonaws.com



For windows machine(and also for Mac))

1. Open Git bash
2. type pwd
It may print your directory in git bash
3. Now type cd downloads
4. make sure you have keys in the downloads directory by typing the below command:
ls *.pem

The above command should list all the pem files in the downloads directory.

Note: Only for Macbook pro laptops! 
You need to perform additional step to give read permissions for the *.pem file 
chmod 400 myJenkinsEc2KKey.pem. 

5. Now paste the url from step # 4 above
ssh -i "mykey.pem" ubuntu@dns_name.compute.amazonaws.com
type yes

6. now you should be in AWS cloud, screen should tell you something like this:

Last login: Sun month 18 19:01:21 2021 from 64.183.186.78
ubuntu@ip-172-31-26-96:~$

How To Secure AWS Account Using Multi-Factor Authorization

 

AWS MFA enable - How to enable Multi Factor Authentication (MFA) for your AWS account? - Enable MFA to AWS account - Secure AWS Account

Now that we've created out AWS account, we are going to learn how to enable Multi-Factor Authorization (MFA) for our accounts to secure it. It is very important to secure your AWS account especially for those who are creating new AWS account, as hackers may be potentially targeting these accounts.

Let us see the how to secure AWS accounts by enabling multi factor authentication.

1. Login to AWS.
2. Click on your user name. Click on My Security Credentials.












3. Continue to security credentials.









4. Click on Multi-factor authentication, click on Activate MFA









5. Now choose A Virtual MFA device










6.  Click on Next step









7. Now download google authenticator on your smart phone
8. After downloaded, click on + scan bar code. Scan the above bar code.
you need to enter first number and wait until it expires, enter second number in code 2 and then click on Activate Virtual MFA.


















9. Once entered both numbers, click on Activate Virtual MFA.

This is how you secure AWS account by enabling MFA.

Introduction to continuous integration: Jenkins

  Jenkins setup - Install Java, Jenkins, Maven, Tomcat on Ubuntu EC2 - How to install Java, Jenkins, Maven, Tomcat on Ubuntu EC2 Please foll...