Automating AWS Infrastructure Deployment Using Jenkins and Terraform: A Step-by-Step Guide
Jenkins with Terraform as Infrastrucutre as code

Automating AWS Infrastructure Deployment Using Jenkins and Terraform: A Step-by-Step Guide


Prerequisites

  1. AWS Account: Ensure you have an AWS account with the necessary permissions to create and manage EC2 instances and other AWS resources.
  2. Terraform Installed: Ensure Terraform is installed on your Jenkins host. You can download it from the Terraform website.
  3. Jenkins Installed: Ensure Jenkins is installed on an EC2 instance or another server. Follow the initial setup, which includes setting up an admin user and installing recommended plugins.
  4. AWS Credentials: Ensure you have AWS credentials configured that Jenkins can use. These credentials should have permission to manage EC2 instances and any other AWS services you plan to use with Terraform.
  5. Repository: Your Terraform code should be stored in a version control system that Jenkins can access, such as GitHub, GitLab, or Bitbucket.

Steps

Step 1: Configure AWS Credentials in Jenkins

  1. Navigate to Jenkins dashboard.
  2. Go to Manage Jenkins > Manage Credentials.
  3. Under the "Stores scoped to Jenkins" section, select Jenkins.
  4. Click on Global credentials (unrestricted).
  5. Select Add Credentials.Kind: AWS CredentialsID: Enter an identifier for your credentials, e.g., aws-terraformAccess Key and Secret Key: Enter your AWS access key and secret key.

  1. Click OK to save.

Step 2: Create Terraform Code

Create a simple Terraform configuration for deploying an EC2 instance. For example, create a file named main.tf in your repository with the following content:

main.tf

provider "aws" {

region = "us-east-1"

}

resource "aws_instance" "example" {

ami = "ami-0c55b159cbfafe1f0"

instance_type = "t2.micro"

}

output "ip" {

value = aws_instance.example.public_ip

}

Step 3: Create a Jenkins Pipeline

  1. Go to Jenkins main dashboard.
  2. Click on New Item.
  3. Enter a name for your job, e.g., Terraform_Deployment.
  4. Select Pipeline and click OK.

In the pipeline configuration:

  • Scroll down to the Pipeline section.
  • In the Pipeline script, you can define your pipeline using the Groovy syntax. Here’s an example:


pipeline {

agent any

environment {

AWS_ACCESS_KEY_ID = credentials('aws-terraform')

AWS_SECRET_ACCESS_KEY = credentials('aws-terraform')

}

stages {

stage('Checkout Code') {

steps {

git 'https://your-repo-url/terraform-code.git'

}

}

stage('Init Terraform') {

steps {

sh 'terraform init'

}

}

stage('Apply Terraform') {

steps {

sh 'terraform apply -auto-approve'

}

}

}

}

Step 4: Run Your Pipeline

  • Go back to the project page and click Build Now to start the pipeline.
  • Monitor the build output to see the results of your Terraform deployment.

Please follow me : https://www.dhirubhai.net/in/jagan-rajagopal/

Free Newsletter Devops Best practise:https://www.dhirubhai.net/newsletters/devops-real-world-practise-7183495093687918592/

Offical page: www.dhirubhai.net/company/awstrainingwithjagan

Website : https://awstrainingwithjagan.com/

Instagram personal page:https://www.instagram.com/awscoachjagan/

Twitter personal page:https://twitter.com/jagan_rajagopal

  • #JenkinsTerraform
  • #AWSIntegration
  • #DevOps
  • #CloudAutomation
  • #InfrastructureAsCode

要查看或添加评论,请登录

社区洞察

其他会员也浏览了