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

Define VPC in CloudFormation0:36

We will start by building a network that has one public subnet and one firewall or security group as AWS calls it. And we will use CloudFormation to speed up the process of creating our resources. So let's get started. Here I have a fresh Laravel application running on version 10.0.3 of the framework. The first thing I am going to do is create a new JSON file for the CloudFormation stack that we want to create on AWS. We will add an object named resources and then create our first resource. We will name this one VPC. For this VPC, we need to define a CIDR block or the range of IP addresses that we want.

We will name this one VBC. For this VBC, we need to define a CIDR block or the range of IP addresses that we want to allocate for this network. We will also set the EnableDNSSupport and the EnableDNSHostNames properties to true so servers or instances as AWS calls it in this network get assigned a DNS hostname. Next, let's create a new resource for the InternetGateway. This will allow instances inside the network to have access to the public Internet. Then we add an InternetGatewayAttachment resource. This one will attach the InternetGateway to the VBC. We do that by referring to the InternetGateway in the InternetGatewayId property and the

Add routes and security group2:51

And define the RouteTableId, DestinationBlock, CIDRBlock, and GatewayId. This route basically routes all traffic to the Internet Gateway. Then we move to associate the RouteTable to the public subnet. For that we need to define the RouteTableId and the SubnetId. Then we need to create a security group resource. This is the firewall as we mentioned earlier. And give that group a description. For the rules in this security group, we will provide an array of objects, with one rule for now that will allow traffic on the TCP protocol for port 22 from anywhere in the Internet.

Create stack in AWS Console4:03

Some providers take care of all this behind the scenes. Now let's head to the AWS Dashboard and create our stack. We will go to the CloudFormation Service Dashboard. Click on Create Stack. Choose Create with New Resources. Template is ready. So we choose Upload Template File. Choose the file. Locate our Laravel application. And then select the stack.json file we just created.

Locate our Laravel application. And then select the stack.json file we just created. Click on Next. Give our stack a name, Laracosts. Then click on Next, Next, and Submit. Now our stack creation is in progress. We will keep refreshing here until all the resources in the stack have been created. And here we go, the stack creation is complete and all our resources were created successfully. Now let's go back to our code editor and update the template. This time we are going to add an EC2 instance resource.

Add EC2 instance resource5:05

Now let's go back to our code editor and update the template. This time we are going to add an EC2 instance resource. This instance will use a specific image ID that has Ubuntu 22.04 installed. It's a T2 micro instance with 1 CPU core and 1 GB of RAM. We attach a key pair named Laracosts EC2. That's what we will use to SSH into the instance later on. We also define the subnet ID we want to place the instance inside, as well as the security group we will attach. Now let's use the AWS CLI to deploy our stack update. So we go to the terminal, and run AWS CloudFormation deploy, give our stack name Laracosts, and

Deploy update with AWS CLI5:47

Now let's use the AWS CLI to deploy our stack update. So we go to the terminal, and run AWS CloudFormation deploy, give our stack name Laracosts, and then the template file stack.json. Now this command will update our CloudFormation stack and add the EC2 instance. And while the stack is being updated, let's do a quick recap. We created a network, or a VPC, that is connected to the public internet through an internet gateway. Attached a security group to it that allows traffic on port 22 from any device connected to the internet. And we created an instance within this network that has Ubuntu 22.04 installed.

SSH into the instance6:33

to the internet. And we created an instance within this network that has Ubuntu 22.04 installed. Now let's go check on our stack and see if the updates were completed. We refresh. And here we go. Our instance was created successfully. Let's click on it, open the instance view, and grab the public IP address. Then we go to the terminal to ssh to the instance. And then we run ssh -i, which is the flag for the identity file. Provide the name of the key pair file.

And then we run ssh -i, which is the flag for the identity file. Provide the name of the key pair file. Use the user ubuntu, add, and then paste the instance public IP address. And here we go. We are now connected to the instance on port 22 as the system user named ubuntu. The instance is running Ubuntu 22.04. Perfect. With our server up and running, now it's time to install the software needed to run our Laravel applications. And that's what we are going to do in the next lesson.

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