The Golden Packer
Tags: cloud, tutorial, terraform, devops
Published: 2022-03-19 10:40:16

## DAY 22 - Building your Golden Image using packer for Terraform  - Day Twenty two

[![Image tweet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jfjzxg64r7q0i8ra6b8t.png)![Image cover](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3aprwbzsqtwtpu8wow79.jpg)](https://twitter.com/intent/tweet?text=how%20to%20Build%20your%20golden%20image%20using%20packer%20terraform%20%2C%20a%20blog%20by%20%40anuvindhs%0A%23cloud%20%23terraform%20%23iac%20%23devops%20%23tutorial%0A%0Ahttps%3A%2F%2Fdev.to%2Faws-builders%2Fthe-golden-packer-5k4%20)


[100 days of Cloud on GitHub](https://github.com/anuvindhs/100daysofcloud) - [Read On iCTPro.co.nz](https://ictpro.co.nz) - [Read on Dev.to](https://dev.to/anuvindhs)



------



How to Integrate  packer with Terraform?

- Packer uses HCL language 
- You Build image
- Then refence the image 

## Lets download and install packer

### Project work space environment

 - I am using VSCode and WSL2 (ubuntu) for this project for AWS environment.

### Install Packer

Download [link](https://www.packer.io/downloads)
As we are using WSL 2 linux so i am downloading via bash script

```BASH
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update && sudo apt-get install packer
```
Create a project folder , am going to name it as **packer**.and **cd** into it.

### Checking packer

![Image Packer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ckgkarjyrdhx6emvlh54.png)
 
## Building a Image

We are going to build a golden image for NGINX
-  Make a file inside the folder , name it as nginx.pkr.hcl
-  Copy Paste below code, Its a HCL code for create a packer image to run **NGINX server** on Ubuntu in ap-southeast-2 region.

```HCL
variable "ami_id" {
	type = string
	default = "ami-0b7dcd6e6fd797935"
}

locals {
	app_name = "nginx"
}

source "amazon-ebs" "nginx" {
	ami_name = "my-nginx-server-${local.app_name}"
	instance_type = "t2.micro"
	region = "ap-southeast-2"
	source_ami = "${var.ami_id}"
	ssh_username = "ubuntu"
	tags = {
		Name = local.app_name
	}
}

build {
	sources = ["source.amazon-ebs.nginx"]
	provisioner "shell"  {
		inline = [
			"sudo apt install nginx -y",
			"sudo systemctl enable nginx",
			"sudo systemctl start nginx"
			
		]
	}
}
```


once you complete the script , build the image using packer

```bash
packer build nginx.pkr.hcl 
```

![Image packer build](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9eas1z6pbta79e6rb2we.png)
![Image build complete](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q2ns5a4621l5cjkh1tqx.png)
 


### Verify the build 

- Go to AWS console and select jump to **EC2 dashboard**.
- Now under the **images** area select **AMI**.
- You will be able to see the packer build AMI
![Image ami image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m12z5fo829zma30fkddw.png)
 

## Deploy a new instance with Terraform

- Lets create a new file in same folder , Name it as **main.tf**
- Copy this code

```HCL
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "3.58.0"
    }
  }
}

provider "aws" {
  profile = "default"
  region  = "ap-southeast-2"
}

data "aws_ami" "packer_image" {
	filter {
			name   = "name"
			values = ["my-nginx-server-nginx"]
		}
	owners = ["self"]
}

resource "aws_instance" "my_server" {
  ami           = data.aws_ami.packer_image.id
  instance_type = "t2.micro"
	tags = {
		Name = "Server-nginx-Packer"
	}
}

output "public_ip" {
  value = aws_instance.my_server.public_ip
}
```
-  Initiate terraform

```HCL
terraform init
```
- Plan the deployment

```HCL
terraform plan
```

if there is no error 

- Deploy your infrastructure

```HCL
terraform apply -auto-approve
```

### Best Practice 

- Build your packer code
- Publish to git , commit
- build image 
- provision image
- Reference your image
- and provision infrastructure
 
🎉Congratulations🎉 you have successfully deployed an EC2 instance with image build on packer. 
 
 
  ----------

**✅Connect with me on [Twitter](https://twitter.com/intent/follow?ref_src=anuvindhs&screen_name=anuvindhs)
 🤝🏽Connect with me on [Linkedin](https://www.linkedin.com/in/anuvindhs)
🧑🏼‍🤝‍🧑🏻 Read more post on [dev.to](https://dev.to/anuvindhs) or  [iCTPro.co.nz](https://ictpro.co.nz)
💻 Connect with me on [GitHub](https://github.com/anuvindhs)**


{% user anuvindhs %}