Infrastructure as a code on AWS using Terraform
Objective:
Create an instance on AWS that acts as a web-server. Use S3 and Cloudfront for the assets. The infrastructure should be purely created through code with all it's components like key, security groups, EBS, CloudFront, S3, Instance, Servers installation, etc.
Scenario:
- The graphic designer has finally created all the assets related to the web project, that files has to be deployed to S3 which will be used as a origin for Cloudfront, after the successful deployment of files a mail should be sent to the developer along with the Cloudfront URL, so that the local uri's can to replaced by the Cloudfront url.
- After the developer has added the url's the code should be commited and the instance,ebs creation should start automatically. After the successful completion of the project the website url should be mailed to the team.
Solution
- Create two jobs for designer and developer on Jenkins having the respective terraform code
- Make post commit hooks to automatically push and create a custom triggers for the Jenkins job
- Create a custom mail code for the respective team
- (Optional) Job 3 to destroy the created infrastructure after use
JOB1:
The tricky part here was to upload all the files to S3 as terraform by default allows only one file to be uploaded in a singe block of code resource using the key attribute as the unique file name. I have used the for each concept here, Stack Overflow to the rescue.
Now after this developer has to just replace the img/ string in the file with the received received cloudfront url, this can be done using sed or awk command. It depends on the project structure in my case after testing the webpage locally I have added "mycfurl::" as as string to be replaced by the actual cloudfront url.
JOB2:
In this tasks I was not able to do the ssh to the instance using the key created by the terraform code, as the connection requires the private key value which I wasn't able to find, I figured it out that ".private_key_pem" attribute gives the private key value.
Job3:
This is an optional job/steps which can be used to destroy the complete infrastructure created by job 1 and job 2 in a single go after the use.
Github Links:
Designer side:
Developer Side:
The GitHub contains all the web-project code, assets, terraform code along with the python mail script.
Comment you question if you have any, I will be happy to help. 🙂
Highly appreciated