Prepare AWS Security Groups for WordPress project
Tags: aws, ec2, securitygroups, wordpress
Published: 2020-02-07 00:48:48

---
title: Prepare AWS Security Groups for WordPress project
published: true
date: 2019-12-26 09:53:54 UTC
tags: AWS,EC2,Security Groups,Wordpress
canonical_url: 
---

> **This article is the continuation of [WordPress with Docker, AWS (ECS, Code Pipeline, Load Balancer, RDS, EFS) Complete Series](https://dev.to/saluminati/high-traffic-wordpress-website-with-docker-aws-ecs-code-pipeline-load-balancer-rds-efs-complete-series-43id). For better understating, please start from the beginning of the series.**

**Disclaimer:** We will do a lot of configuration in this article. It may look time consuming but it will lay the foundation for all the communication such as WordPress website with MYSQL and Plugins storage.

We will prepare our security groups for AWS Services such as Load Balancer, RDS (MYSQL database), EFS (for plugins storage) and EC2 instances.

I have created these Groups having security in mind, we should not open any unnecessary port on any service.

Our main agenda is to Place Load Balancer on top to face the outside world (people who want to access our wordpress website), Load Balancer will then send that request to any of the instances hosting the WordPress website and respond with content received from the WordPress EC2 instance.

Other than that, WordPress EC2 instance can talk to RDS (MYSQL), EFS (plugins storage) as per required. Please take a look at the following diagram for better understanding of this kind of architecture.

[![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26105856/FI-1024x842.jpeg)](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26105856/FI.jpeg)

## Create Security Groups

We will create the SGs (security groups) for our services so they can talk to each other on the same VPC.

- SG for Load Balancer
- SG for EC2 instance
- SG for RDS (MYSQL)
- SG for EFS (storage for plugins)
- SG for Bastian host

Login to your AWS console and click on EC2 from the Services menu, we will take notes of the security groups IDS while you create them.

From EC2 console, click on Security groups

![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26100905/001-2.png)

Click on Create Security Group

![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26101001/002-2.png)

Please make sure all the Security Groups you are creating use the same VPC.

### Security Group for Load Balancer

The first Security Group is for the Load Balancer. For outside world, it will enable HTTP (Port 80) in the Inbound Rules. Please select the Inbound tab and click on Add Rule.

[![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26101113/003-2-1024x552.png)](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26101113/003-2.png)

Once this SG is created, we will note down its Group ID. which will be used while creating the EC2 instance (WordPress host) SG.

[![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26101152/004-2.png)](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26101152/004-2.png)

### Security Group for WordPress instance

Click on Create Security Group again, we will now create a security group for our EC2 instance where our WordPress project will be deployed. We’ll add an Inbound Rule which will open all the ports for our Load Balancer ONLY.

For that, Add a rule to Inbound with All TCP as type and Custom as the source. Paste the Group ID of the Load Balancer Security group we have created earlier. This will ensure that no one else can access this instance except the Load Balancer.

[![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26101311/005-1-1024x552.png)](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26101311/005-1.png)

Hit Create and take a note of Group ID of this SG.

### Security Group for Bastian host

We will create an EC2 instance (Bastian host) which we can connect from our local computer to test RDS and EFS or in some cases if we need to connect and troubleshoot our WordPress EC2 instance. This can come in very handy.

This time we will Create a New Security Group with SSH (Port 22) enabled so we can connect to this instance over SSH.

[![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26102849/008-1024x552.png)](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26102849/008.png)

Please create a take a note of Bastian Security Group, we will reference in EFS and RDS SGs

### Security Group for EFS and RDS

Now we will create Security Groups for RDS and EFS (enabling Port 3306 and 2049 respectively) and put Group ID of our WordPress and Bastian as a Custom source.

[![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/27110431/sg-efs-1-1024x414.png)](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/27110431/sg-efs-1.png)

[![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/27110818/sg-rds-1024x436.png)](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/27110818/sg-rds.png)

**Note:** By adding Group ID of Bastian into the Inbound rules of EFS and RDS Security Groups will allow us to configure EFS from Bastian and will also let us connect to RDS (MYSQL) database via Bastian if required.

Most of our work with Security Groups is done here except one more step which is also a good practice for security.

One more tweak and we’re done with SGs I PROMISE.

Please copy the Group ID of the Bastian SG we just created and select the Security Group of our WordPress instance and click on Inbound tab at the bottom. Click Edit to add a new Inbound Rule

[![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26103350/010-1024x318.png)](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26103350/010.png)

Select SSH as type and Custom as Source, now past the Group ID of the Bastian Security Group we created earlier.

![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/27111559/wp-instance-update-1024x380.png)

By doing this, we add an exception for our Bastian instance so it can connect to our WordPress instance if we want to.

# Create Bastian Instance

In the EC2 console, Click on Key pairs and select Create Key Pair.

![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26111428/011.png)

![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26111436/012.png)

Give it a and Download the .PEM file on your computer

![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26111708/013.png)

It is very important to keep this file safe and secure. There is no backup of this file on AWS.

Let’s create our Bastian instance.

Click on Instances from the EC2 console and select Launch instance.

![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26111923/014.png)

![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26111930/015.png)

We are using ubuntu 18 with Free tier. Because it is the bastian host and not doing much except enabling us to connect to it from our local machine and to let us connect to the wordpress instance for troubleshooting or testing services.

[![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26112403/016-1024x293.png)](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26112403/016.png)

![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26112412/017-1024x84.png)

Select free Tier in instance type section and hit configure instance details at the bottom

[![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26113044/instance-type-1-1024x483.png)](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26113044/instance-type-1.png)

In the configure Instance section, make sure you select the same VPC you have used while creating all the Security groups earlier.

[![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26112420/018.png)](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26112420/018.png)

Go to next step i.e Add tags and give your instance a name.

[![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26113358/tags-1024x412.png)](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26113358/tags.png)

In the section below, use the Bastian Security Group you have created earlier which enables port 22 in Inbound rules.

[![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26112429/019-1024x549.png)](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26112429/019.png)

Click on Review and you will see the following screen. Select the Key Pair you have created and it will create the instance for us.

![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26112437/020.png)

If you will go to instances, you will see that your bastian instance has been created.

![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26112517/021-1024x139.png)

# Connect to Bastian Instance

Now **FINALLY** we are about to do something exciting after all this configuration. It’ll only get better from this point **TRUST** me !!

We have done most of the heavy lifting

![](https://media.tenor.com/images/232b0f9956bf78e8e5e10064491d27b1/tenor.gif)

Now open your terminal and go the directory where you have downloaded the Key Pair PEM file. Run the following commands

```shell
# To protect a file against accidental overwriting. 
chmod 400 wwordpress-key.pem
```

Once we’re done with the chmod 400. Time to connect to our bastian instance YASSSS !!!!

Click on your bastian EC2 intance and copy the public DNS.

[![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26114939/022-1-1024x253.png)](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26114939/022-1.png)

Run the following command from your directory where your PEM file is located.

```shell
ssh -i wwordpress-key.pem ubuntu@ec2-3-106-167-129.ap-southeast-2.compute.amazonaws.com
```

Replace **wwordpress-key.pem** and **ec2-3-106-167-129.ap-southeast-2.compute.amazonaws.com** with your Key Pair PEM file name and instance public DNS. You should see the following output on your terminal.

[![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26115828/ssh-ubuntu.png)](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26115828/ssh-ubuntu.png)

We are now connected to our Bastian Instance via SSH.

![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/26122010/cookie.jpg)

#### Previous

**[Dockerize WordPress with themes, plugins and common configuration](https://dev.to/saluminati/dockerize-wordpress-with-themes-plugins-and-common-configuration-2am4)**

#### Next

**[Create IAM (User, Roles and Policies)](https://dev.to/saluminati/create-iam-user-roles-and-policies-pmm)**