Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/abdelrhman95/terraform-jenkins
A Terraform Infrastructure as Code (IAC) project to create and manage development and production environments with AWS, and Integration with Jenkins for automated provisioning
https://github.com/abdelrhman95/terraform-jenkins
ci-cd jenkins-pipeline terraform
Last synced: about 2 months ago
JSON representation
A Terraform Infrastructure as Code (IAC) project to create and manage development and production environments with AWS, and Integration with Jenkins for automated provisioning
- Host: GitHub
- URL: https://github.com/abdelrhman95/terraform-jenkins
- Owner: abdelrhman95
- Created: 2023-10-03T13:40:36.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2023-10-15T18:32:54.000Z (about 1 year ago)
- Last Synced: 2024-11-22T21:13:56.184Z (about 2 months ago)
- Topics: ci-cd, jenkins-pipeline, terraform
- Language: HCL
- Homepage:
- Size: 15.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Here is a README file for the infrastructure as a code project with Terraform:
# Infrastructure as Code with Terraform
This project demonstrates using Terraform to manage infrastructure as code and deploy resources to AWS.
## Overview
- Create dev and prod workspaces with Terraform
- Store environment variables in .tfvars files
- Modularize network resources in a shared module
- Deploying across multiple regions US East (N. Virginia) and EU Central (Frankfurt) regions
- Printing EC2 data with local-exec
- Building a Jenkins image with Terraform
- Creating a Jenkins pipeline to deploy environments
- Sending email notifications via AWS Simple Email Service(SES) on state changes
- Lambda function to trigger emails## Prerequisites
- AWS account
- Terraform CLI
- Git and GitHub account
- Docker to build Jenkins image## Steps
1. Configure AWS credentials
2. Initialize Terraform workspaces
- By default, you are on the default workspace
``` yaml
terraform list workspace
```
- Create `Dev` and `Prod` workspaces
```yaml
terraform workspace new prod
terraform workspace new Dev
```
3. Define .tfvars files for each env
- `dev.tfvars`, `prod.tfvars` with env-specific values
4. Create Network module in `module\network`
5. Use `terraform apply` with `-var-file` to deploy dev and prod
```yaml
terraform apply -var-file=dev.tfvars #for the "dev" environment
terraform apply -var-file=prod.tfvars #for the "prod" environment
```
6. Use `local-exec` provisioner to print bastion IPs
```yaml
provisioner "local-exec" {
command = "echo ${self.public_ip} is the public IP of the Bastion Instance > inventory.txt"
}
```7. Commit code to new GitHub repo
9. Build Jenkins server with Terraform installed
- `Dockerfile` file `docker build -t jenkins-terraform:latest . `
10. Build image and create Jenkins container
- run jenknins container
```yaml
docker run -d -p 8080:8080 -p 50000:50000 --name my-jenkins jenkins-terraform:latest
```
- open the container's shell
```yaml
docker exec -it my-jenkins bash
```
- Set jenkins admin password
```yaml
cat /var/jenkins_home/secrets/initialAdminPassword
```
11. In jenkins you need to configure AWS credinals
- Go to manage jenkins
- Select Credentials
- Press the global Hyperlink
- Choose "Add Credentials"
- Choose "Secret text" as the kind
- enter your AWS Access Key ID and then name it
- repeat the same steps for the AWS Secret Access Key
12. Create Jenkinsfile pipeline
- Add the code in the `pipeline.groovy` file.
- Accept parameter for env file
- Run Terraform init, plan, apply
-
13. Verify SES in AWS console14. Create Lambda function to send SES emails
- Navigate to IAM and create a new role
- Add the AmazonS3FullAccess and AmazonSESFullAccess permission policies to the role
- Navigate to S3 and create a bucket with default configurations.- Navigate to Lambda and create a new function
- choose python
- select the created role15. Configure trigger on state file changes to invoke Lambda
- Add a trigger for S3 and choose the created S3 bucket from the Bucket dropdown menu.
- add code to lambda function from `lambda-email.py` file16. Make changes and verify emails are sent
## Repository Structure
├── main.tf - Terraform configuration for AWS providers and remote state
├── variables.tf - Define the input variables for your network module
├── dev.tfvars - Environment variables for dev
├── prod.tfvars - Environment variables for prod
├── network - Network module to create VPCs, subnets, routes, etc
├── servers - EC2 instances
├── outputs.tf - Define any outputs you want to expose from the network module
├── README.mdThis covers the core infrastructure as code project requirements. The Jenkins pipeline and other automation can be added to make the project more robust.