PackerでWebServer(AMI)を作成してみた

2021.11.12

Introduction:

Packer is an open source tool for creating identical machine images for multiple platforms from a single source configuration.

use: creating immutable infrastructure

Comparison between Mutable and immutable infrastructure:

Pro:

Mutable Immutable
Fix problems more quickly. Rather than needing to create a new server from scratch, IT staff gets to know each server on a "personal" level, making diagnoses faster. Discrete versioning means tracking and rollbacks are much easier. IT department can keep tabs on each new server or virtual machine as it is deployed.
Updates are usually faster and can be adapted to each individual server. Testing is easier to run thanks to the consistency in configurations between different servers.
The infrastructure can more precisely fit the specific needs of the applications that are running on the server.Predictable state since the infrastructure is never modified, reducing complexity Safe thread code in a multi-threaded environment meaning mutation is almost nonexistent.
Eliminates configuration drift, since there are no changes, there is no drift.

Cons:

Mutable Immutable
Technical issues are difficult to diagnose or reproduce because each server has a unique configuration, a phenomenon often known as configuration drift. The infrastructure is completely unable to be modified in-place. In the event of a zero-day vulnerability, for example, all servers with the same configuration must receive a security update.
Changes to the server are not necessarily documented, making version tracking more difficult. Improved agility and dynamism of immutable infrastructure can sometimes be misaligned with traditional IT security practices.
Increased risk and complexity in production workloads if updates don't apply properly. Debugging is a difficult due to the unanticipated state.
Update failures are more likely due to variety of unexpected reasons (network connectivity, unresponsive repos, DNS offline, and the list goes on).Overhead associated with copying array data from one location to another. Externalizing data instead of writing data to the local disk.

Mutable infrastructure:

Develop -> Deploy -> Configure

Immutable infrastructure:

Develop -> Configure -> Deploy

I Tried:

Mac installation:

brew install packer

to verify

packer --version

Building your 1st AMI :

https://www.packer.io/docs/builders/amazon Bellow is the json script for creating AMI using static credentials

for adding credentials we can also use:

  • Environment variables
  • Shared credentials file
  • EC2 Role
{
 "builders": [
     {
       "type": "amazon-ebs",
       "region": "ap-northeast-1",
       "source_ami": "ami-00000000",
       "instance_type": "t2.micro",
       "ami_name": "DevelopersIO-try",
       "ssh_username":  "ec2-user",
       "access_key": "your access key",
       "secret_key": "your secret key"
     }
  ],
     "provisioners":[
       {
         "type": "shell",
         "inline": [
             "sleep 30",
             "sudo amazon-linux-extras install -y nginx1",
             "sudo systemctl start nginx"
             ]
        },
       {
          "type": "file",
          "source": "index.html",
          "destination": "/tmp/"
       },
       {
          "type": "shell",
          "inline": "sudo cp /tmp/index.html /usr/share/nginx/html/"
        }
    ]
}

html file(index.html) if you are following along html file is in same directory

</pre>
<div>
<div><!DOCTYPE html></div>
<div><html lang="en"></div>
<div><head></div>
<div><meta charset="UTF-8"></div>
<div><meta name="viewport" content="width=device-width, initial-scale=1.0"></div>
<div><title>DevelopersIoPacker</title></div>
<div></head></div>
<div><body></div>
<div>
<h1>linuxWebserver</h1>
</div>
<div></body></div>
<div></html></div>
<div><span style="font-size: 13.28px;">

To execute

packer build packerfilename.json

You can check the AMI in your management console and use it for instance creation

Resources:

https://eplexity.com/blog/benefits-of-immutable-infrastructure/

https://www.packer.io/docs/builders/amazon

Conclusion:

We created a AMI which can be used to deploy Amazon linux instance with NGINX and html file using packer

Hope you enjoyed learning!