در حال بارگذاری ...

Load Balancer Overview0:00

In this course, we are going to serve a Laravel application behind an AWS application load balancer. A load balancer serves as the single entry point to our application from the internet. It distributes incoming traffic across multiple targets. These targets could be EC2 instances or Lambda functions. In this course, we will distribute traffic across two EC2 instances located in different availability zones. This increases the availability of our application, and it also allows us to easily add or remove instances at any time.

Starting VPC Template0:34

This increases the availability of our application, and it also allows us to easily add or remove instances at any time to increase or decrease our capacity. We will use CloudFormation to deploy our infrastructure on AWS, and we are going to start by creating a template for a VPC with two public subnets. We will first define the AWS template format version and set it to 2010-09-09. And then we will start by adding the resources we need for the VPC. The first resource is going to be called VPC,

And then we will start by adding the resources we need for the VBC. The first resource is going to be called VBC, and it's going to be of type AWS EC2 VBC. For the properties, we are going to define the CIDR block for the VBC, 10.0.0.0/16, and also we're going to enable DNS support and enable DNS host names. If you're not familiar with all of this, we have a course on Laracasts that explains all the fundamentals of AWS.

Internet Gateway and Routes1:43

we have a course on Laravel that explains all the fundamentals of AWS. Go check it out to understand all these attributes. Now, for the next resource, we are going to call it InternetGateway, and this one is of type AWS EC2 Internet Gateway. And then the next resource is going to be called InternetGatewayAttachment. The type will be AWS EC2 VPC Gateway Attachment. And for the properties, we are going to set InternetGatewayID and set it to a reference to the InternetGateway.

And for the properties, we are going to set InternetGatewayId and set it to a reference to the InternetGateway that we just created. And then we are going to set the VpcId and refer to the Vpc resource. Now, let's create a new resource called PublicRouteTable. This one is of type AWS EC2 Route Table. And for the properties, we just need to set the VpcId, and we'll do that by referencing to the Vpc resource. The next resource is going to be called DefaultPublicRoute.

and we'll do that by referencing to the VBC resource. The next resource is going to be called DefaultPublicRoute. This one is of type AWS EC2 Route, and this one will be depending on the InternetGatewayAttachment resource. For the properties, we are going to set the RouteTableID by referring to the PublicRouteTable and the destination CIDR block 0.0.0.0/0 and then the GatewayID, which we will reference to the InternetGateway resource. Next, we are going to create a resource called PublicSubnet1.

Creating Public Subnets3:25

which we will reference to the Internet Gateway resource. Next, we are going to create a resource called PublicSubnet1. This one is of type AWS EC2 Subnet. And for the properties, we are going to set the VPC ID and the availability zone, we will set it to us-east-1a. That's an availability zone inside the us-east-1 region. And then for the CIDR block, we are going to set it to 10.0.0.0/24. Then we're going to set the Map Public IP on Launch property to true so that the instances created inside this subnet get a public ID.

Then we're going to set the Map Public IP on Launch property to true so that the instances created inside this subnet gets a public ID. Next, we are going to copy this resource into a new one, and we're going to rename it to Public Subnet 2. The type is an EC2 subnet and the VPC references to the VPC. For the availability zone, we are going to use us-east-1b instead of us-east-1a, so that the subnet is in a different availability zone than Public Subnet 1. We are also going to change the CIDR block to 10.0.1.0/24.

than Public Subnet 1. We are also going to change the CIDR block to 10.0.1.0/24. And we are going to set Map Public IP on Launch to true, similar to the Public Subnet 1 resource. Now let's create a Public Subnet Route Table Association resource. This one is of type AWS EC2 Subnet Route Table Association. And for the properties, we are going to set the Route Table ID by referring to the Public Route Table resource and the subnet ID, which will refer to the Public Subnet 1 resource.

Defining Security Groups5:19

and the subnet ID, which will refer to the Public Subnet 1 resource. Now let's copy this resource and rename it to Public Subnet Route Table Association 2. This one is going to refer to the Public Subnet 2 subnet. That's all regarding the VPC with its public subnets. Let's now create two security groups, one for our EC2 instances and one for the load balancer. So we will create a resource called EC2 Security Group. This one will be of type AWS EC2 Security Group.

So we will create a resource called EC2SecurityGroup. This one will be of type AWS EC2 Security Group. For the properties, we are going to set the VPC ID and we are going to give our group a description, access to EC2 instances, and then we are going to set the security group ingress rules. For the first rule, we are going to set the IP protocol to minus one to refer to all protocols. And then we are going to set sourceSecurityGroupID and reference it to the loadBalancerSecurityGroup,

And then we are going to set sourceSecurityGroupId and reference it to the loadBalancerSecurityGroup, which is a security group we are going to create in a bit. This rule simply says that all traffic coming from resources inside the loadBalancerSecurityGroup to all protocols should be accepted inside this security group. Now let's create another rule, this one for the IP protocol TCP, for the CIDR 0.0.0.0, which is the public internet. And this one is going to be from port 22 to port 22.

for the CIDR IP 0.0.0.0, which is the public internet. And this one is going to be from port 22 to port 22. We are simply saying here that resources inside this security group should accept traffic on port 22 from anywhere from the internet. We are going to use this rule so that we can SSH into the instances inside the EC2 Security Group. You can change the CIDR IP to your IP range so only you can access the instances through port 22. Now let's copy the security group and create a new one. We are going to rename this to loadBalancerSecurityGroup,

Now let's copy the security group and create a new one. We are going to rename this to loadBalancerSecurityGroup, and we are going to change the description to access to loadBalancer. For the security group and grassroots, we are going to remove this, and we are going to change this one to accept traffic on port 80, and then copy the rule and add a new one to accept traffic on port 443. These two rules will allow traffic for HTTP and HTTPS requests to the loadBalancers inside the security group. Now we are done with the resources, let's configure the output of our stack.

Stack Outputs and Deploy8:28

Now we are done with the resources, let's configure the output of our stack. For the first output, it's going to be called VBC, and the value of this output is going to be, is going to refer to the VBC that we are creating. We are also going to export this output so we can use it in other cloud formation stacks. We are going to name this export with the stack name dash VBC. The stack name here will be replaced with the stack name that we are going to create.

The stack name here will be replaced with the stack name that we are going to create. Now let's create another output, this one is for public subnet 1, and it refers to the public subnet 1 resource, and we are going to name it stack name - public subnet 1. And then we are going to create another output, public subnet 2, refer to public subnet 2, and rename it to public subnet 2. Now let's create another output for the EC2 security group, and refer to the security group,

Now let's create another output for the EC2 security group, and refer to the security group, and rename it to stackName dash EC2 security group. And then another one for the loadBalancer security group, the value will refer to the loadBalancer security group resource, and we are going to rename the export to stackName dash to stackName dash loadBalancer security group. That's all we need in this template. Let's give it another look to make sure that everything is correctly configured.

Let's give it another look to make sure that everything is correctly configured. So I have a typo here, this should be capital, and another typo here, this should be support, and then I can see that EC2 here is not correctly set, just let's just make sure that it's always EC2 and capital letters. And I think that's it, I think we're good to go. Oh, here, this should be aligned like this. And that's it, I think we're good. Let's now go to our terminal and deploy this stack.

And that's it, I think we're good. Let's now go to our terminal and deploy this stack. We're going to use the AWS CLI and run the AWS cloudformation deploy command, the region is going to be us-east-1, and the stack name will be laracast-vbc. For the template file, we are going to set it to .aws-vbc.yaml. It will take a moment for the stack with all its resources to be created. And once it's done, we go check the cloudformation dashboard. And here we can see the status is CREATE_COMPLETE. Let's check the output.

And here we can see the status is create complete. Let's check the output. And we can see all the output we configured in the template. In the next video, we will start creating some instances.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟