Skip to content

Commit

Permalink
packer: add content and hands on (#667)
Browse files Browse the repository at this point in the history
  • Loading branch information
tungbq authored Jul 30, 2024
1 parent ccb111d commit a78f95b
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,14 @@ We cover a wide range of DevOps topics in our content library, explore them unde
<td>Nginx</td>
<td><a href="./topics/nginx/">nginx</a></td>
<td>📖 <a href="https://nginx.org/en/docs/">nginx.org/en/docs</a></td>
<td>✔️ <a href="./topics/nginx/basic/">nginx basic</a></td>
<td>✔️ <a href="./topics/nginx/basic/">Nginx basic</a></td>
</tr>
<tr>
<td><img width="32" src="https://www.datocms-assets.com/58478/1638283616-packer.svg"></td>
<td>Packer</td>
<td>coming-soon</td>
<td><a href="./topics/packer/">packer</a></td>
<td>📖 <a href="https://www.packer.io/">www.packer.io</a></td>
<td>⏩ coming-soon</td>
<td>✔️ <a href="./topics/packer/basic/">Packer basic</a></td>
</tr>
<tr>
<td><img width="32" src="https://www.hashicorp.com/_next/static/media/vault_on-dark.97792f64.svg"></td>
Expand Down
40 changes: 40 additions & 0 deletions topics/packer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## 1. What is Packer?

### Overview

- Packer is a tool that lets you create identical machine images for multiple platforms from a single source template.
- Packer can create golden images to use in image pipelines.

### Official website documentation of Packer

- https://www.packer.io/

## 2. Prerequisites

- None

## 3. Installation

### How to install Packer?

- https://developer.hashicorp.com/packer/install

## 4. Basics of Packer

### Packer getting started

- Begginner's Guide: https://developer.hashicorp.com/packer/tutorials

### Packer Hands on

- See: [basic](./basic/)

## 5. More...

### Packer cheatsheet

- None

### Recommended Books

- None
40 changes: 40 additions & 0 deletions topics/packer/basic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Build an Ubuntu machine image on AWS with Packer

## Prerequisites

- AWS account
- Packer installed
- Authenticate to AWS
```bash
export AWS_ACCESS_KEY_ID="<YOUR_AWS_ACCESS_KEY_ID>"
export AWS_SECRET_ACCESS_KEY="<YOUR_AWS_SECRET_ACCESS_KEY>"
```
- Doc: https://developer.hashicorp.com/packer/integrations/hashicorp/amazon#iam-task-or-instance-role

## Init

```bash
packer init .
```

## Build

```bash
packer build aws-ubuntu.pkr.hcl

# ...
# ==> Wait completed after 5 minutes 15 seconds
# ==> Builds finished. The artifacts of successful builds are:
# --> learn-packer.amazon-ebs.ubuntu: AMIs were created:
# us-west-2: ami-xxxxyyyyzzzztttt
```

## Verify

- Go to AWS Console `us-west-2` (The region we build packer AMI): https://us-west-2.console.aws.amazon.com/ec2/home?region=us-west-2#Images:visibility=owned-by-me
- You now can see your AMI with name: `learn-packer-linux-aws-redis-<timestamp>`
![](./assets/ami-on-aws.png)

## Cleanup

- Once you dont want to use the AMI anymore, follow https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/deregister-ami.html to delete it.
Binary file added topics/packer/basic/assets/ami-on-aws.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 57 additions & 0 deletions topics/packer/basic/aws-ubuntu.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
packer {
required_plugins {
amazon = {
version = ">= 1.2.8"
source = "github.com/hashicorp/amazon"
}
}
}

source "amazon-ebs" "ubuntu" {
ami_name = "${var.ami_prefix}-${local.timestamp}"
instance_type = "t2.micro"
region = "us-west-2"
source_ami_filter {
filters = {
name = "ubuntu/images/*ubuntu-jammy-22.04-amd64-server-*"
root-device-type = "ebs"
virtualization-type = "hvm"
}
most_recent = true
owners = ["099720109477"]
}
ssh_username = "ubuntu"
}

build {
name = "learn-packer"
sources = [
"source.amazon-ebs.ubuntu"
]

provisioner "shell" {
environment_vars = [
"FOO=hello world",
]
inline = [
"echo Installing Redis",
"sleep 30",
"sudo apt-get update",
"sudo apt-get install -y redis-server",
"echo \"FOO is $FOO\" > example.txt",
]
}

provisioner "shell" {
inline = ["echo This provisioner runs last"]
}
}

variable "ami_prefix" {
type = string
default = "learn-packer-linux-aws-redis"
}

locals {
timestamp = regex_replace(timestamp(), "[- TZ:]", "")
}

0 comments on commit a78f95b

Please sign in to comment.