Saturday, May 27, 2023

Terraform with AWS

How to setup AWS cli and access for aws account.

1.Create a AWS user. 

Add in access group, suppose you want to give full permission like AdmistratorAccess do like as below.



2. create access key for that user 



3. Install aws cli on your local computer, go to below doc and install according to your OS.

https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html


 

 

4. Configure aws cli Access key

aws configure

 

Done now you are connected with your AWS account via AWS cli.

 

Now Install Terraform

https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli 

after install terraform verify it

terraform --help

now Create 1 folder go inside that folder and start terraform work.

Let suppose you have created 1 terraform script as below, store it inside that folder.

script name suppose - aws_ec2_jenkins_docker_install.tf

provider "aws" {
region = "us-east-2" # Update with your desired region
}

resource "aws_key_pair" "jenkins_keypair" {
key_name = "jenkins-keypair"
public_key = file("/root/.ssh/id_rsa.pub") # Replace with the path to your public key
}

resource "aws_instance" "jenkins_instance" {
ami = "ami-03a0c45ebc70f98ea" # Replace with the desired AMI ID
instance_type = "t2.small" # Replace with the desired instance type
key_name = aws_key_pair.jenkins_keypair.key_name
vpc_security_group_ids = [aws_security_group.jenkins_sg.id]

user_data = <<-EOF
#!/bin/bash
sudo apt-get update
sudo apt-get install -y docker.io

sudo docker run -d -p 8080:8080 -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts

sleep 30 # Wait for Jenkins to start

jenkins_password=$(sudo docker exec $(sudo docker ps -q --filter "ancestor=jenkins/jenkins:lts") cat /var/jenkins_home/secrets/initialAdminPassword)
echo "Jenkins initial admin password: $jenkins_password"
EOF
}

resource "aws_eip" "jenkins_eip" {
instance = aws_instance.jenkins_instance.id
}

resource "aws_security_group" "jenkins_sg" {
name = "jenkins-sg"
description = "Security group for Jenkins"
vpc_id = "vpc-0d67054cd23a8f716" # Replace with the desired VPC ID

ingress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}

egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}

resource "aws_eip_association" "jenkins_eip_association" {
instance_id = aws_instance.jenkins_instance.id
allocation_id = aws_eip.jenkins_eip.id
}

output "jenkins_login_password" {
value = aws_instance.jenkins_instance.user_data
}

output "jenkins_url" {
value = "http://${aws_eip.jenkins_eip.public_ip}:8080"
}

now from inside that folder

run below cmd

terraform init  - it will download all plugin according to your provider

terraform plan - before execution of script you should run plan that will show you infra that will create if you execute that script.

terraform apply - once you apply it will create whole infra you defined inside the script.

now you can go to aws and check.

terraform destroy - it will delete all infra created by terraform apply.



No comments:

Post a Comment