-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add Packer configuration for building Docker images with Ansib…
…le provisioning
- Loading branch information
Dave Arnold
committed
Aug 16, 2024
1 parent
f13095e
commit 1a6bbd5
Showing
1 changed file
with
87 additions
and
0 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| packer { | ||
| required_plugins { | ||
| # Amazon plugin for Packer | ||
| docker = { | ||
| source = "github.com/hashicorp/docker" | ||
| version = "~> 1" | ||
| } | ||
| # Ansible plugin for Packer | ||
| ansible = { | ||
| version = "v1.1.1" | ||
| source = "github.com/hashicorp/ansible" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| variable ansible_dir { | ||
| type = string | ||
| default = "./" | ||
| } | ||
|
|
||
| variable project_name { | ||
| type = string | ||
| default = "my-project" | ||
| } | ||
|
|
||
| # ecr_repository_name | ||
| data amazon-parameterstore ecr_repository_name { | ||
| name = "/image-pipeline/${var.project_name}/ecr_repository_name" | ||
| } | ||
|
|
||
| data amazon-parameterstore aws_region { | ||
| name = "/image-pipeline/${var.project_name}/region" | ||
| } | ||
|
|
||
| data amazon-parameterstore aws_account_id { | ||
| name = "/image-pipeline/${var.project_name}/aws_account_id" | ||
| } | ||
|
|
||
| data amazon-parameterstore playbook { | ||
| name = "/image-pipeline/${var.project_name}/playbook" | ||
| } | ||
|
|
||
| data amazon-parameterstore source_image { | ||
| name = "/image-pipeline/${var.project_name}/source_ami" | ||
| } | ||
|
|
||
| data amazon-parameterstore image_tag { | ||
| name = "/image-pipeline/${var.project_name}/image_tag" | ||
| } | ||
|
|
||
| locals { | ||
| aws_account_id = data.amazon-parameterstore.aws_account_id.value | ||
| aws_region = data.amazon-parameterstore.aws_region.value | ||
| tag = data.amazon-parameterstore.image_tag.value | ||
| ecr_repo = data.amazon-parameterstore.ecr_repository_name.value | ||
| } | ||
|
|
||
| source "docker" "docker" { | ||
| image = data.amazon-parameterstore.source_image.value | ||
| commit = true | ||
| } | ||
|
|
||
| build { | ||
| sources = ["source.docker.docker"] | ||
|
|
||
| # Provisioner to run the Ansible playbook | ||
| provisioner "ansible" { | ||
| command = "/root/.local/bin/ansible-playbook" | ||
| playbook_file = "${var.ansible_dir}/${data.amazon-parameterstore.playbook.value}" | ||
| roles_path = "${var.ansible_dir}/roles" | ||
| ansible_env_vars = ["ANSIBLE_STDOUT_CALLBACK=yaml", "ANSIBLE_NOCOLOR=True"] | ||
| } | ||
|
|
||
| post-processor "docker-tag" { | ||
| repository = local.ecr_repo | ||
| tag = local.tag | ||
| } | ||
|
|
||
| post-processor "docker-push" { | ||
| ecr_login = true | ||
| // login_username = "AWS" | ||
| // login_password = data.amazon-secretsmanager.aws_secret_key.secret_string | ||
| login_server = "${local.aws_account_id}.dkr.ecr.${local.aws_region}.amazonaws.com" | ||
| repository = var.project_name | ||
| tag = local.tag | ||
| } | ||
| } |