Hey there! Here we are going to launch a website on an EC2-instance and to make the storage persistent we will use Amazon EFS. But we’ll do all this using terraform code. In case you want to use Amazon EBS then you can check this out.
Before we begin, we need to set-up the provider in terraform. You can find that step here. So, let’s start now.
Step1: Create a Security Group
Here we are going to create a security group using aws_security_group resource. You can give
vpc_id accordingly. Then we set ingress/inbound rules. I have added rules for SSH, HTTP and NFS(since we are integrating with EFS) request. For egress/outbound rules, I haven’t set any particular restriction.
Step2: Create a EFS File System and Mount Targets
Terraform provides an Elastic File System (EFS) File System resource
aws_efs_file_system, which has many options like
encrypted, etc, which you can fill accordingly. Next is the EFS mount target. To access your Amazon EFS file system in a VPC, you create one or more mount targets in the VPC. You can create one mount target in each Availability Zone in an AWS Region for better availability. Here we have shown terraform code to create a mount target in one of the subnets. Note that an Amazon EFS file system can only have mount targets in one VPC at a time.
Step 3: Launching Instance and mounting EFS to that Instance
We are going to launch an instance with the resource
us-east-1 region (as specified inside the provider block) using EC2 service of AWS. You can select
instance_type according to your requirements. Here we have used the security group created in step 1. Also, we have used the pre-created key (to know more click here). Next, since we want to set this instance as web-server, so we are going to install the required software in this instance using
provisioner. Also, we have mounted the EFS to
var/www/html directory. And here we are working remotely so we use
remote-exec provisioner in this case. But before that, we’ll set-up a connection to the remote instance using the connection (you remember we add SSH type in ingress rules of the security group).
Step 4: Cloning the Source Code
By now we have mounted the EFS to the
var/www/html directory. Now, cloning the code in this directory.
That code contains the dynamic content for which we’ll not use Cloudfront Service. Now we’ll create an S3 bucket and put the static content of the website in that bucket. Then we’ll create CloudFront distribution for the static content of the website and we’ll replace the URL of the image with the URL from the CloudFront for the image using
sed command. And luckily all this is exactly same what we did while using EBS with Instance. So head on to that article, using https://medium.com/@me_kabhi/creating-complete-infrastructure-on-aws-using-terraform-d660f1fe007f.
That’s all for this story. Thanks for stopping by. Until next time!
You can find the complete code at:
GitHub - Abhishekkr3003/AWS_Tasks
Have to create/launch Application using Terraform Create the key and security group which allow the port 80. Launch EC2…
Let’s connect over Linkedin: