Setting up an AWS infrastructure using Terraform
SETUP YOUR PROFILE / SOFTWARE
- Firstly, you would need to create an IAM user (ishan_tf in my case). Note down the Access Key ID and Secret Access Key (you will need it to configure the profile on was cli).
- Create a key pair and a security group that allows inbound SSH and HTTP.
- Download the AWS cli and Terraform onto your system.
Use #aws configure --profile ishan_tf (profilename) and paste your access keys here.
Windows users can add the terraform path to environment variables, where as Linux users can simply unzip and #chmod +x terraform #mv terraform /usr/bin
NOW THAT WE HAVE OUR SYSTEM SET UP, LETS CODE!
(Create a .tf extension file)
- Create an S3 bucket and configure its policy to public-read.
- Configure Jenkins to get images from github to S3 bucket.
We need the S3 publisher plugin
Configure it
Create a job to transfer contents to the s3 bucket.
- We need to trigger this after our bucket is created.
- Create a CloudFront distribution.
- Remember to use the domain generated for your image src in HTML.
- Create an EC2 instance and install services like httpd PHP git.
- Create and attach EBS to it.
- Partition, format, and mount it to /var/www/html/ to make the data persistent. Also, clone the developers' code to the folder.
- We can also output or store the IP of the instance here.
- Now that we have set up the infrastructure code, let's run it.
#terraform init (initialize a working directory containing Terraform configuration files.)
#terraform validate (validates the configuration files in a directory).
#terraform apply (apply the changes required to reach the desired state of the configuration).
- And here it is!