-
-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
packer: add content and hands on (#667)
- Loading branch information
Showing
5 changed files
with
140 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:]", "") | ||
} |