تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Create Infrastructure Template0:00

In the previous lesson, we created our VBC stack with two security groups for n-instances and load balancers. Now let's create our n-instances. We will start by creating a new file inside the .aws directory. That's where we keep our infrastructure code, and we will name that file infrastructure.yaml. We'll start the file by declaring the AWS template format version, same as before. And for the resources, we're going to start with a resource called ec2-role.

Define EC2 IAM Role0:36

And for the resources, we're going to start with a resource called ec2-role. And I'm going to explain in a bit why we need this. For now, this resource is of type AWS IAM role. And for the properties, we're going to give the role a name that are casts ec2-role. And for the assume role policy document, we are going to set a version 2012-10-17. And then for the statement, we are going to allow one action. And for the principle that we are going to allow this action for,

And then for the statement, we are going to allow one action. And for the principle that we are going to allow this action for, it's going to be a service, and that service is called ec2.amazonaws.com, which is basically any EC2 n-instance. And the action we want to allow is sts:AssumeRole. So here, we are allowing EC2 n-instances to assume this role. We can then allow our Laravel applications running on the n-instance to interact with AWS services without using AWS keys in our .env files.

Add IAM Policy Permissions1:47

running on the n-instance to interact with AWS services without using AWS keys in our environment files. The AWS PHP SDK that is used by Laravel will read the authorization keys automatically from the EC2 n-instance profile. We will create this profile shortly. Now, before we can create the profile, we need to create another resource called instance policy. This one is of type AWS IAM policy. And for the properties, we are going to give the policy a name,

This one is of type AWS IAM policy. And for the properties, we are going to give the policy a name, laracast ec2-policy. And for the document, we are going to set the version to 2012.10.17. And for the statement, we are going to allow all resources to perform these actions. The first action is all interactions with S3. And the second is all interactions with SQS. And we're going to attach this policy to our role here. And we're going to refer to it by using the ref function.

Create Instance Profile2:57

And we're going to attach this policy to our role here. And we're going to refer to it by using the ref function. Now, let's create the instanceProfile. This one is of type AWS IAM instance profile. And for the properties, we are going to give the profile a name, laracast instance profile. And we are going to attach a role to it. So we'll reference the EC2 role that we created. So we attach the policy to the role and then created an instance profile and attached the role to it.

Build EC2 Launch Template3:33

So we attach the policy to the role and then created an instance Profile and attached the role to it. Now, let's create another resource. This one is called LaunchTemplate and is of type AWS EC2 Launch Template. For the properties, we're going to give the LaunchTemplate a name, laracast-launch-template. And then we'll set the LaunchTemplate data. And for that, we are going to set the ImageId. This AMI belongs to the US East 1 region.

And for that, we are going to set the image ID. This AMI belongs to the us-east-1 region and has your Ubuntu 22.04 installed. You will have to use a different AMI if you are using a different region. Now we are going to set the key name, which will be laracast EC2. I've created that key before in the AWS fundamentals course on laracast. Go check this course out if you want to understand more about what we are doing here. Now for the instance type, I'm going to set this to T2.micro and we're going to attach an IAM instance profile.

Now for the instance type, I'm going to set this to T2 micro and we're going to attach an IAM instance profile and we are going to refer to it by name and refer to the instance profile resource we just created. Then for the security group IDs, we are going to attach a single security group and we are going to import this value from the laracast VBC stack that we created in the previous lesson. So laracast VBC EC2 security group, that's the name of the export we set in the previous lesson.

Provision Instances and Deploy5:15

So laracast VBC EC2 security group, that's the name of the export we set in the previous lesson. Now let's create our first instance. So EC2 instance one of type AWS EC2 instance. And for the properties, we're going to set the subnet ID by importing the value from the laracast VBC stack. And then for the launch template, we are going to set the launch template ID by referring to the launch template resource we created in this lesson.

by referring to the launchTemplate resource we created in this lesson. And for the version, we are going to get the launchTemplate latest version number. The getAttribute function here extracts an attribute from a resource. In that case, the resource is the launchTemplate we created. Now let's set a tag for this instance. We're going to give the tag a key of name and the value laracast_one. That's how we are going to see the instance in the AWS dashboard. It will be called laracast_one.

That's how we are going to see the instance in the AWS dashboard. It will be called laracast one. Now let's copy this and create a new instance. We're going to change the resource name. And we're going to place this instance in another subnet, public subnet two. And the same launch template for the tags, we're going to change the value to laracast two. Now let's deploy our stack. We're going to run AWS cloud formation,

Now let's deploy our stack. We're going to run AWS CloudFormation, deploy for the region, it's us-east-1. And the stack name will be laracast-web. For the template file, we are going to set it to .aws/infrastructure.yaml. And for the capabilities, we're going to provide a capability called CAPABILITY_IAM. And another capability called CAPABILITY_NAMED_IAM. CAPABILITY_IAM is needed because we are creating

And another capability called capability named IAM. Capability IAM is needed because we are creating IAM resources in this template. And capability named IAM is needed because we are using custom names for the IAM resource. Now, once the stack is deployed, we will head to the CloudFormation dashboard. And here we can see the status is create complete. Let's check the resources. And here we have our instances, the rule, the policy,

Let's check the resources. And here we have our instances, the rule, the policy, the instance profile and the launch template. All looks good. So now we have two EC2 instances running up onto 22.04 and are located in two public subnets in two different availability zones in the us-east-1 AWS region. So in the next video, we will prepare an important resource that we need before we can create a load balancer.

that we need before we can create a load balancer.

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